Topic: Adding Accounts to Users [SOLVED]

I am trying to add a Account to a user

but i am having a problem listing the accounts in the new user view


Users Controller

class UsersController < ApplicationController
    before_filter :authenticate_user!

  def index
    @user = User.all
  end
  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
    @account = Account.all # i think this line should give me all the accounts 
  end

  def create
    @user = User.new(params[:account])
    if @user.save
      flash[:success] = "User Created"
      redirect_to @user
    else
      render 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:success] = "User Updated"
      redirect_to @account
    else
      render 'edit'
    end
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User Destroyed."
    redirect_to user_path
  end


end

View

<%= provide(:title, 'New User') %>
<!-- Main content -->
<section role="main" id="main">
  <div class="with-padding">
    <%= form_for(@user) do |f| %>
    <table class="simple-table responsive-table">
      <tbody>
        <tr>
          <td scope="row">
          <%= f.label :email %>
          </td>
          <td><%= f.email_field :email %></td>
        </tr>
        <tr>
          <td scope="row">
          <%= f.label :username %>
          </td>
          <td><%= f.text_field :username %></td>
        </tr>
        <tr>
          <td scope="row">
          <%= f.label :password %>
          </td>
          <td><%= f.password_field :password %></td>
        </tr>
        <tr>
          <td scope="row">
          <%= f.label :password_confirmation %>
          </td>
          <td><%= f.password_field :password_confirmation %></td>
        </tr>
        <tr>
          <td scope="row">
          <%= f.label :first_name %>
          </td>
          <td><%= f.text_field :first_name %></td>
        </tr>
        <tr>
          <td scope="row">
          <%= f.label :last_name %>
          </td>
          <td><%= f.text_field :last_name %></td>
        </tr>
        <tr>
          <td scope="row">
           Account
          </td>
          <td>
            This needs to be a Selct Box
            <% @account.each |a| do %>
            a.name
            <% end %>
          </td>
        </tr>
        <tr>
 <tr>
          <td colspan=2><%= f.submit "Create User" , :class => "button" %> &nbsp;&nbsp; <%= link_to "Cansel", users_path, :class => "button" %></td>
        </tr>
      </tbody>
    </table>
    <% end %>
  </div>
</section>
<!-- End main content -->

The Error that i am having
SyntaxError in Users#new

  syntax error, unexpected keyword_do_block
');             @account.each |a| do
                                    ^
/Users/moiseszaragoza/Sites/sample/app/views/users/new.html.erb:66: syntax error, unexpected keyword_ensure, expecting $end

Last edited by moiseszaragoza (2012-05-31 14:02:05)

Re: Adding Accounts to Users [SOLVED]

To start, your syntax is wrong for your loop. That block should be:

<% @account.each do |a| %>
    <%= a.name %>
<% end %>

Try that and we'll see if you get any more errors.

Last edited by matthewvb (2012-05-31 11:13:45)

matthewvb

Re: Adding Accounts to Users [SOLVED]

i was able to add

<%= f.collection_select(:account_id, Account.all, :id, :name, :prompt => true)%>

and this saved me the problem to have to loop.
i just have to find out how to sort this

Re: Adding Accounts to Users [SOLVED]

Do the sort in the controller when you make the call:

@accounts = Account.order("name")

In your collection_select, put @accounts instead of the Account.all

matthewvb

Re: Adding Accounts to Users [SOLVED]

Thanks for all the help but its still not working properly

my controller

class UsersController < ApplicationController 
  def new
    @accounts = Account.order("name")
    @user = User.new
  end
end 

view

<%= form_for(@user) do |f| %>
  <%= f.collection_select(:account_id,@accounts, :id, :name, :prompt => true)%>
<% end %>

Output

<select id="user_account_id" name="user[account_id]"><option value="">Please select</option>
<option value="3">B Account</option>
<option value="1">G Account</option>
<option value="2">G Account2</option>
<option value="4">S Account</option>
<option value="5">T Account</option>
<option value="6">aaa First Account</option>
<option value="7">zzz Last Account</option></select>

Last edited by moiseszaragoza (2012-05-31 13:47:46)

Re: Adding Accounts to Users [SOLVED]

Few questions:

1) Accounts have names, right?
2) In the user's table, do you store the account name or id?
3) Do you have your associations set up in your models? (has_many / belongs_to)

matthewvb

Re: Adding Accounts to Users [SOLVED]

i store both

here are my models

Users

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :first_name, :last_name, :account_id
  # attr_accessible :title, :body
  belongs_to :account


  validates_uniqueness_of :username, presence: true, :case_sensitive => false


  validates :first_name, presence: true 
  validates :last_name, presence: true 
  validates :password, presence: true
  validates :password_confirmation, presence: true
end

Account

class Account < ActiveRecord::Base
  attr_accessible :name, :account, :reference
  validates_uniqueness_of :name, presence: true, :case_sensitive => false
  has_many :users
  validates :account, presence: true
  validates :reference, presence: true
end

Last edited by moiseszaragoza (2012-05-31 15:12:07)

Re: Adding Accounts to Users [SOLVED]

What's the error you're getting now and which line does it refer to?

matthewvb

Re: Adding Accounts to Users [SOLVED]

There is no error
it just display in the wrong order

<select id="user_account_id" name="user[account_id]"><option value="">Please select</option>
<option value="3">B Account</option>
<option value="1">G Account</option>
<option value="2">G Account2</option>
<option value="4">S Account</option>
<option value="5">T Account</option>
<option value="6">aaa First Account</option>
<option value="7">zzz Last Account</option></select>

view

<%= form_for(@user) do |f| %>
  <%= f.collection_select(:account_id,@accounts, :id, :name, :prompt => true)%>
<% end %>

Last edited by moiseszaragoza (2012-05-31 13:58:30)

Re: Adding Accounts to Users [SOLVED]

ok thanks
Caps come before lowercase
WOW

Thanks

Re: Adding Accounts to Users [SOLVED]

Try this in your controller:

Account.order("LOWER(name)")

Quick testing worked for me. This will basically make it think they're all lowercase for sorting.

Last edited by matthewvb (2012-05-31 14:11:08)

matthewvb

Re: Adding Accounts to Users [SOLVED]

you are a genius Thanks you so much