collection_select not being submitted database

Hi everybody, I apologize for my student's english.

I got some problem with my Rails app, i got a collection_select who aren't submited & save in my database. After 2 days googling and didn't find anything helpful i'am asking your help.

I had two tables: Crc and User: (I removed useless relation from others tables)

User model:

class User < ActiveRecord::Base   has_many :crcs   accepts_nested_attributes_for :crcs   acts_as_authentic

  def role_symbols      return ["#{self.role.name}".to_sym]   end end

Crc model: class Crc < ActiveRecord::Base   belongs_to :user   attr_accessible :user_id

  validates_presence_of :name, :place   validates_uniqueness_of :name end

My views: (I removed user field form except username)

<% form_for @user do |f| %>   <%= f.error_messages %>       <p>         <%= f.label :username %><br />         <%= f.text_field :username %>       </p>       </p>          <%= collection_select(:crc, :user_id, Crc.all, :id, :name) %>       </p>   <p><%= f.submit %></p> <% end %>

My logs:

Processing UsersController#create (for 127.0.0.1 at 2010-07-26 12:20:29) [POST]   Parameters: {"commit"=>"Save changes", "authenticity_token"=>"6qJ/pJVcDVvNeaACnyOKRKT2R6EIA7TWVfvlMXxRMkk=", "user"=>{"password_confirmation"=>"pass", "role_id"=>"1", "username"=>"usertest", "password"=>"pass", "email"=>"test@test.com"}, "crc"=>{"user_id"=>"2"}}

For me that seems good.

i got an User_Create acion:

User Create (0.3ms) INSERT INTO `users` (`created_at`, `crypted_password`, `updated_at`, `username`, `role_id`, `password_salt`, `persistence_token`, `email`) VALUES('2010-07-26 10:20:29', 'b693ef04a548e3006ac8d81534717b46d7cb6146dd60e5f0e253d88356798431a5accabb135ee4727a68b9b9692e4245ab10ef9455fdbcfb44ffc88b5b402897', '2010-07-26 10:20:29', 'usertest', 1, 'SCgvhFqM0E9R7rkxXPWY', '26260bc8aa988fc3895af5b6cfcc5778b0872ce21aaa6924ac38f6c185293b457edb81d95a2ee3d5d7b78ea76a3229e6da68e6102c258861e857a769e784f2a5', 'test@test.com')

I didn't got Crc_Create action or any other Crc action. And my user_id field still 'NULL' on database.

Thanks for reading, I hope is clear.

Loic/Harks

That's not going to put parameters in the form expected by accepts_nested_attributes_for (the easiest way to do that is to use fields_for). You're also not submitting name or place attributes for crc so the crc wouldn't able to be saved anyway. Last of all you say you want tp create a new crc, but you're asking the user to choose from a drop down populated with existing crcs, which doesn't make sense to me

Fred

Frederick Cheung wrote: � � � </p>

That's not going to put parameters in the form expected by accepts_nested_attributes_for (the easiest way to do that is to use fields_for). You're also not submitting name or place attributes for crc so the crc wouldn't able to be saved anyway. Last of all you say you want tp create a new crc, but you're asking the user to choose from a drop down populated with existing crcs, which doesn't make sense to me

Fred

Hi, I understand the non-sense. I used fields_for and it's works but as you say, I create a new crc when I want to edit one.

I put a partial on my users views:

      <% f.fields_for :crcs do |crc| %>         </p>           <%= render :partial => 'crcatt', :locals => { :f => crc } %>         </p>       <% end %>

and on "_crcatt.html.erb" I got:         <%= f.collection_select( :user_id, @crcs, :id, :name) %>

how can I edit the choosen crc ? I'am a 'little' bit lost... I only got 4 month of rails experience and 1 years of OO language. Sorry for telling you my life ;).

Thanks for your help.

Loïc

What is the choosen crc ? According to what you posted above, users have multiple crcs.

Fred

Frederick Cheung wrote:

This is a really weird way of doing things, so I'm not sure this will mesh with stuff nested attributes. If i were you (and had to do it this way) I'd probably just submit the id of the chosen crc separately and have a line somewhere that updates the user_id for that crc. Seems rather back to front though.

Fred

Frederick Cheung wrote: