Hey, I got a form which looks like this: - form_for(@article, :html => {:multipart => true}, :url => articles_path) do |f| = errors_for(@article) .field = f.label :text %br = f.text_field :text .field = f.label :author_id %br = f.text_field :author_id .actions = f.submit But since I don''t want anyone to type in an author id I changed it to: .field = f.label :author_name %br = f.text_field :author_name added to article.rb: attr_accessible :author_name def before_validation if self.author_name author_name = self.author_name.split self.author Author.find_or_create_by_firstname_and_lastname(author_name[0], author_name[1]) end But now this line: @article = Article.new(params[:article]) throws this error: unknown attribute: author_name I thought attr_accessible would cover that? How can I make this work? -- 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.
On 20 May 2011 05:01, Heinz Strunk <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I got a form which looks like this: > - form_for(@article, :html => {:multipart => true}, :url => > articles_path) do |f| > = errors_for(@article) > > .field > = f.label :text > %br > = f.text_field :text > > .field > = f.label :author_id > %br > = f.text_field :author_id > > .actions > = f.submit > > But since I don''t want anyone to type in an author id I changed it to: > .field > = f.label :author_name > %br > = f.text_field :author_name > > added to article.rb: > attr_accessible :author_name > > def before_validation > if self.author_name > author_name = self.author_name.split > self.author > Author.find_or_create_by_firstname_and_lastname(author_name[0], > author_name[1]) > end > > But now this line: > @article = Article.new(params[:article]) > throws this error: > unknown attribute: author_name > > I thought attr_accessible would cover that? How can I make this work?I think you probably want ''attr_accessor'' instead of ''attr_accessible''. ''attr_accessor'' is a Ruby feature that creates getter and setter methods (#author_name and #author_name=) for an instance variable. ''attr_accessible'' is part of Rails'' mass-assignment feature; it white-lists an existing attribute as assignable via mass-assignment. They''re easily confused, thanks to the similar names. Chris -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi,> added to article.rb: > attr_accessible :author_nameYou should use attr_accessor instead (not attr_accessible, as You don''t have the author_name DB column). -- 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.
Yes, of course! Thank you guys :) -- 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.