ok, you have
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: link):
app/controllers/comments_controller.rb:19:in `create'
This tells you that error is occurring on line number 19 in the comments_controller in the create action.
From your post on stack overflow, line 19 is
@comment = @commentable.comments.build(params[:comment])
From the stack trace you posted above the comment
the releant params that are being passed in to your controller are
"comment"=>{"content"=>"comment 1", "link"=>{"link_url"=>"link 1"}}, "commit"=>"Add comment", "forum_post_id"=>"16"}
From this, it's pretty clear that
is the offensive line.
you have
class Comment < ActiveRecord::Base
attr_accessible :commentable_id, :commentable_type, :content,:links_attributes, :link_url
belongs_to :commentable, :polymorphic => true
has_many :links, :as => :linkable
accepts_nested_attributes_for :links, :allow_destroy => true #, :reject_if => lambda { |t| t[:link].nil?}
end
,:links_attributes would accept a param called links. that are nested inside the comments params but your param is called link not links
You have 2 solutions.
Arrange for the param to be called links or arrange for attr_accessible to include link_attributes, which you don't want.
It's a pretty safe bet that what you want to do is to chage your form to post back links rather than link in your params which would require a change to the f.fields_for declaration in your form
<%= f.fields_for [@linkable, Link.new] do |link| %>
<%= render :partial => 'links/link', :locals => { :f => link } %>
<% end%>
I'm not comfortable with your approach on this and I could have this wrong but you could try
<%= f.fields_for :links do |link| %>
<%= render :partial => 'links/link', :locals => { :f => link } %>
<% end%>
and build your link on to the comment model in the new action
Last edited by jamesw (2012-10-11 11:55:50)
What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)