Jim Burgess
2010-Aug-15 19:17 UTC
Can save object via console, but not in app''s controller
My app has a ''page'' model and a ''category'' model. ''Page'' belongs_to :category. ''Category'' has_many :pages. From the console I can create and save a page object with no problems:>> p = Page.new#<Page id: nil, title: nil, body: nil, created_at: nil, updated_at: nil, published: nil, read_counter: nil, category_id: nil>>> p.title, p.body, p.category_id = "Test", "Test", "1"#<Page id: nil, title: "Test", body: "Test", created_at: nil, updated_at: nil, published: nil, read_counter: nil, category_id: 1>>>p.save=> true Yet, when I submit the same data in a post request to my controller, ''category_id'' is being ignored. The page object is created in my controller thus: @page = Page.new(params[:page]) If I write the contents of ''params[:page]'' to WEBrick, I see: {"title"=>"Test", "body"=>"Test", "category_id"=>"1"} If I write the contents of @page to the WEBrick, I see: #<Page id: nil, title: "Test", body: "Test", created_at: nil, updated_at: nil, published: nil, read_counter: nil, category_id: nil> I get the same result on the console:>> params = {"title"=>"Test", "body"=>"Test", "category_id"=>"1"} >> p = Page.new(params)#<Page id: nil, title: "Test", body: "Test", created_at: nil, updated_at: nil, published: nil, read_counter: nil, category_id: nil>>> p.save=> false Does anyone have any tips they can give me? Am I missing something really obvious? Thanks in advance -- Posted via http://www.ruby-forum.com/. -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jim Burgess
2010-Aug-15 19:23 UTC
Re: Can save object via console, but not in app''s controller
Please ignore my question. Changed: attr_accessible :category into: attr_accessible :category_id and it works now. Doh! Checked, double checked and tripple checked before posting, but still I missed that! -- Posted via http://www.ruby-forum.com/. -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.