Topic: Is polymorphic to nested polymorphic models possible?

Hi, I'm have a comments polymorphic form to which I wan to nest a links polymorphic form. The problem is I keep getting a Mmss assignment error for attribute link. I have added the links_attributes but that still has not work. Am I going about this the wrong way? Been stuck on this for quite a while now and would really like to solve this. Tried asking this elsewhere but not a single response, so beginning to wonder is it possible and how would others approach this. my code http://stackoverflow.com/questions/1274 … rity-error

Re: Is polymorphic to nested polymorphic models possible?

Post your log file entry where the error occurs so we can see the params that are being passed into the controller and the actual error stack trace. It's very difficult to solve other peoples coding issues with only half the information

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)

Re: Is polymorphic to nested polymorphic models possible?

Hi, here is the log entry

Started POST "/forum_posts/16/comments" for 127.0.0.1 at 2012-10-11 11:55:15 +0100
Processing by CommentsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"KeuhAPvZNkgusJN9bkpOwjtP2OdMfOxzi3/1Q0UvAFA=", "comment"=>{"content"=>"comment 1", "link"=>{"link_url"=>"link 1"}}, "commit"=>"Add comment", "forum_post_id"=>"16"}
  ForumPost Load (0.3ms)  SELECT `forum_posts`.* FROM `forum_posts` WHERE `forum_posts`.`id` = 16 LIMIT 1
Completed 500 Internal Server Error in 2ms

ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: link):
  app/controllers/comments_controller.rb:19:in `create'


  Rendered /home/otis/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (3.3ms)

  Rendered /home/otis/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.3ms)

  Rendered /home/otis/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (11.1ms)

Re: Is polymorphic to nested polymorphic models possible?

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

"link"=>{

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)

Re: Is polymorphic to nested polymorphic models possible?

Thank you for looking at this! I add a new action to the comments controller

def new
    @links = @comment.links.build
  end

I changed the form as suggested


<%= form_for :comments do |f| %>

  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content, :rows => 5 %>
  </div>


  <%= f.fields_for :links do |link| %>
   <%= render :partial => 'links/link', :locals => { :f => link } %>
  <% end%>
  
  <div class="actions">
    <%= submit_tag "Add comment" %>
  </div>

The comments render but the links did not. Not sure if its because the form is not using the new action in the controller?

Re: Is polymorphic to nested polymorphic models possible?

When the form is posted back to the server The RESTful routes will automatically determine which action is called. Rails standards dictate that the form uses the create action for a new comment record and update action for an existing comment record.
It is standard practice to use a new or an edit action to render the form in the first place. There are exceptions to this and you are obviously frtee to set up whatever routes and actions you want.

I just assumed you had just not included the full CRUD actions for the comments controller in your post. If they are missing then perhaps something went wrong when you scaffolded the comments model?

I suggest that you place that build in whatever action is used to render the form in the first place.

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)

Re: Is polymorphic to nested polymorphic models possible?

Followed the railscast #154 polymorphic to construct the form which does add comments but can't add the next level down for links. Thanks for you advice. Still fairly new to rails so will we look at how I'm constructing the form as still can't get  this to work.

Re: Is polymorphic to nested polymorphic models possible?

What action is used to render the form?

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)

Re: Is polymorphic to nested polymorphic models possible?

I have a _comment.html.erb partial in my my forum_posts show.html.erb

<%= form_for [@commentable, Comment.new] do |f| %>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content, :rows => 5 %>
  </div>


  <%= f.fields_for :links do |link| %>
   <%= render :partial => 'links/link', :locals => { :f => link } %>
  <% end%>

  <div class="actions">
    <%= submit_tag "Add comment" %>
  </div>
<% end %>

This renders the comment form and the links, but throws the mass assignment error. Tried adding a new action in the comments controller to build the links for that but then the links do not render

Last edited by otissv (2012-10-12 13:56:47)

Re: Is polymorphic to nested polymorphic models possible?

The problem will lie in either the links not being built, i.e the wrong action being called to render the form, or the build statement is in the wrong action. Amounts to the same thing really.
or
the fields_for delaration is not correct.
Use your log file to check which action is being called that renders the form containing the partial and make sure you use that action to build the links.

Check the fields_for dclaration. I did say that the code I gave you might not be correct as I wasn't comfortable witth your original approach.
The fields_for declaration needs to be able to find the links you have available and I possibly steered you in the wrong direction here. Sorry. You can use the comment object to find all the links

<%= fields_for @comment.links # etc...  %> should do the trick

Read more here http://apidock.com/rails/ActionView/Hel … fields_for

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)

Re: Is polymorphic to nested polymorphic models possible?

Thanks for your help jamesw. As you said the error was where I was trying to render the form. The @commentable.comments.build and @link = @comment.links.build needed to be in the show action in the PostsController. Here's the example for anyone trying to do this https://github.com/otissv/nestedforms