collection_select not being submitted database

3 views
Skip to first unread message

Loic Regnault

unread,
Jul 26, 2010, 6:48:15 AM7/26/10
to rubyonra...@googlegroups.com
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"=>"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/.

Frederick Cheung

unread,
Jul 26, 2010, 8:48:48 AM7/26/10
to Ruby on Rails: Talk
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

Loic Regnault

unread,
Jul 26, 2010, 9:18:56 AM7/26/10
to rubyonra...@googlegroups.com
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

Frederick Cheung

unread,
Jul 26, 2010, 9:24:28 AM7/26/10
to Ruby on Rails: Talk

On Jul 26, 2:18 pm, Loic Regnault <li...@ruby-forum.com> wrote:
> 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 ;).
>
What is the choosen crc ? According to what you posted above, users
have multiple crcs.


Fred

Loic Regnault

unread,
Jul 26, 2010, 9:43:49 AM7/26/10
to rubyonra...@googlegroups.com
Frederick Cheung wrote:
> On Jul 26, 2:18�pm, Loic Regnault <li...@ruby-forum.com> wrote:
>> � � � <% 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 ;).
>>
> What is the choosen crc ? According to what you posted above, users
> have multiple crcs.
>
>
> Fred

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.

Frederick Cheung

unread,
Jul 26, 2010, 9:55:08 AM7/26/10
to Ruby on Rails: Talk
On Jul 26, 2:43 pm, Loic Regnault <li...@ruby-forum.com> wrote:
>
> 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.
>
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

Loic Regnault

unread,
Jul 26, 2010, 10:06:48 AM7/26/10
to rubyonra...@googlegroups.com

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.

Reply all
Reply to author
Forward
0 new messages