Simplest Collection Select Tag Not Working

Ok I just added a collection_select tag to the form and attempted to save it but the value is not getting saved. This is what my mvc looks like :

V (abbreviated edit.html):

<table> <% form_for :user, :url => user_url(@user), :html => { :method => :put } do |f| %>   <tr>     <td class="tlabel"><label for="email">Email:</label></td>     <td><%= f.text_field :email %></td>   </tr> <tr>   <td class="tlabel"><label for="customer_id">Customers:</label></td>   <td><%= f.collection_select(:customer_id,@customer_accounts,:id, :name, :include_blank=>true) %></td> <tr>   <td colspan="2" align="center"><br /><%= image_submit_tag('Save.png') %></td> </tr> <% end %> </table>

M (abbreviated user.rb): class User < ActiveRecord::Base     validates_presence_of :email     belongs_to :customer end

C (abbreviated users_controller.rb):   def edit     @customer_accounts = Customer.find(:all)     @user = current_user   end

  def update     @user = User.find(current_user)     @customer_accounts = Customer.find(:all)     @profile = @user.user_profile     if @user.update_attributes(params[:user])       logger.debug(">>>> user.customerid : #{@user.customer_id}")       if @profile.update_attributes(params[:user][:profile_attributes])         flash[:notice] = "User updated"         redirect_to :action => 'show', :id => current_user       else         render :action => 'edit'       end     else       render :action => 'edit'     end   end

Cayce Balara wrote:

Is it possible that params[:user]['customer_id'] is coming through from the form as a string and you need to to_i it before the update_attributes since customer_id is an integer?

I didnt try that but I figured out the error. It was with the controller code; I needed to set the user.customer to @customer and then save that user .It was just the addition of these two lines :

      @user.customer = @customer       @user.save!

Following is the over all code :

  def update     @user = User.find(current_user)     @profile = @user.user_profile     logger.debug(">>>> customer : #{params[:user][:customer_id]}")     @customer= Customer.find(params[:user][:customer_id])     logger.debug(">>>> customer : #{@customer.name}")     if @user.update_attributes(params[:user])       logger.debug(">>>> user.customerid : #{@user.customer_id}")       @user.customer = @customer       logger.debug(">>>> user.customerid : #{@user.customer_id}")       @user.save!       if @profile.update_attributes(params[:user][:profile_attributes])         flash[:notice] = "User updated"         redirect_to :action => 'show', :id => current_user       else         render :action => 'edit'       end     else       render :action => 'edit'     end   end

Thanks for the help!

That certainly is a fix - but note you are making what should be an unnecessary database call with the .find method on Customer. Since you already have the customer_id you should be able to just store it and move on, without having to go look up the customer again in the database. Although, doing it your way does provide some protection against rogue requests coming in from somewhere other than your form. But, you'll also want to make sure you prepare for the possibility that the .find call doesn't return anything and catch that exception.

Your solution even more makes me think it was a string_to_integer issue, since ActiveRecord .find works fine with a string "1" or an integer.

But, no worries - you have a fix - good work!

Ather Shiraz wrote: