Hello everybody. I have two models user and user_information. the User has_one user_information and the UserInformation belongs_to user. Now I need to make a form_for to create the user information for an user. So I go to the following link: http://localhost:3000/users/1/user_informations/new a fill the form, with the user_id, and the other fields. But I get the following error: Can''t mass-assign protected attributes: user_id I think the error is in the user_information#new view, in the form_form. This is the actual form <%= form_for(@user_information) do |f| %> <%= f.text_field :user %> </div> <div class="field"> <%= f.label :address %><br /> <%= f.text_field :address %> </div> <div class="field"> <%= f.label :phone %><br /> <%= f.text_field :phone %> </div> <div class="field"> <%= f.label :business %><br /> <%= f.check_box :business %> </div> <div class="actions"> <%= f.submit %> </div> <% end %> I think the form_for should be something like <%= form_for([@user_information, @user.user_information.bluild]) do |f| %> but it doesn''t work. So I really really need your help, please. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/xCZVgj_hsXUJ. For more options, visit https://groups.google.com/groups/opt_out.
Jean ha scritto:> Can''t mass-assign protected attributes: user_idto solve this you need: attr_accessible :user_id in the UserInformation model, but you''re using nested resources, so I''d prefer something like: # UserInformationController # def new @user = User.find(params[:id]) @user_information = @user.user_information.build end def create @user = User.find(params[:id]) @user_information = UserInformation.new(params[:user_information]) @user_information.user = @user @user_information.save end take a look here: http://stackoverflow.com/questions/2034700/form-for-with-nested-resources -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Thanks Tomas. I tried this way, but then I get this error: NoMethodError in UserInformationsController#new undefined method `build'' for nil:NilClass On Monday, November 5, 2012 4:55:43 PM UTC-4:30, Tommaso Visconti wrote:> > Jean ha scritto: > > Can''t mass-assign protected attributes: user_id > > to solve this you need: > > attr_accessible :user_id > > in the UserInformation model, but you''re using nested resources, so I''d > prefer something like: > > # UserInformationController # > > def new > @user = User.find(params[:id]) > @user_information = @user.user_information.build > end > > def create > @user = User.find(params[:id]) > @user_information = UserInformation.new(params[:user_information]) > @user_information.user = @user > @user_information.save > end > > take a look here: > http://stackoverflow.com/questions/2034700/form-for-with-nested-resources >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/kM1IEGZ82uYJ. For more options, visit https://groups.google.com/groups/opt_out.
Jean ha scritto:> Thanks Tomas. I tried this way, but then I get this error: > > > NoMethodError in UserInformationsController#new > > undefined method `build'' for nil:NilClassyeah, sorry, for has_one relation the correct statement is: def new @user = User.find(params[:id]) @user_information = @user.build_user_information end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Thanks Tommaso, It works, but the user_id has this User:0x007fe37d33b5e0 no User:1, is that ok??? On Monday, November 5, 2012 6:25:23 PM UTC-4:30, Tommaso Visconti wrote:> > Jean ha scritto: > > Thanks Tomas. I tried this way, but then I get this error: > > > > > > NoMethodError in UserInformationsController#new > > > > undefined method `build'' for nil:NilClass > > yeah, sorry, for has_one relation the correct statement is: > > def new > @user = User.find(params[:id]) > @user_information = @user.build_user_information > end >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/HauLu4dNT9kJ. For more options, visit https://groups.google.com/groups/opt_out.
Jean ha scritto:> Thanks Tommaso, It works, but the user_id has this User:0x007fe37d33b5e0 > no User:1, is that ok???Don''t confuse the actual data (saved on db) and the representation of it in the views. You see the representation of the User model, which is composed of user attributes (like @user.id, etc.). My advice is to play with rails console to clarify your doubts -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
That User: 0x007fe37d33b5e0 you see is the object id for that User object that was created. Thats ruby''s ID for it. Your''s is contained inside that object. Don''t confuse 0x007fe37d33b5e0 with being the .id field inside that user. Two very different things. The .id field (@user.id) is the *database* assigned ID, while the 0x007fe37d33b5e0 is the *object* id for ruby to track the user object in memory. Hope this helps you understand the difference between the two. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Ajn46ZuyRNgJ. For more options, visit https://groups.google.com/groups/opt_out.