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"=>"te...@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',
'te...@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
--
Posted via http://www.ruby-forum.com/.
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
I mean the chosen crc on my collection.
Exactly users have multiple crcs, this is why I used nested attribute
with child_link to add crcs to users. (I hope I'm understandable ..)
In fact crcs are already created and had a 'NULL' field on DB (user_id)
that I want to edit on submitting with my collection.
Thanks again Fred for your help.
Ok,I'm working on a cleanest way to do this. I already have waste a lot
of time for this, thank for yours help I'm not going to waste my time
any more.
Thanks a lot Fred.