0

I have an application where there are a few nested models... Two parent models and two child models.

I'm trying to create comments on the child models and I had it working great for the first one until I realized I have to create comments on the second child so I realized I had to scrap my work because I was targeting the first parent + child model in the comments controller. So I decided to watch Ryan Bates screencast (http://railscasts.com/episodes/154-polymorphic-association) on creating comments that belong to multiple models...unfortunately, it's not working for me and I'm assuming its because I am trying to create comments on the child models. I will show you what I was using before that worked for the one model and I'll show you what im doing now that doesnt work...

here is what i had for the comments controller

def create
  @collection = Collection.find(params[:collection_id])
  @design = @collection.designs.find(params[:design_id])
  @comment = @design.comments.create(comment_params)
  @comment.user = current_user
  @comment.save
  redirect_to collection_design_path(@collection, @design)
end

and here is what it is now after i tried to implement it to work for multiple models

def create
  @commentable = find_commentable
  @comment = @commentable.comments.build(comment_params)
  @comment.user = current_user
  @comment.save
end

private 

    def find_commentable
      params.each do |name, value|
        if name =~ /(.+)_id$/
          return $1.classify.constantize.find(value)
        end
      end
      nil
    end

here are my crazy routes

resources :collections do
  member do
    post :like
    post :unlike
  end
  resources :designs do
    resources :comments
    member do
      post :like
      post :unlike
    end
  end
end

anyone have any other different ideas for created comments for multiple nested models? Thanks in advance for the help.

EDIT:

Here was the form I was using for the one model

<%= form_for([@collection, @design, @design.comments.build]) do |f| %>
  <%= f.text_area :comment %>
  <%= f.submit "Comment", :class => "btn" %>
<% end %>

and here is the one i'm using now

<%= form_for([@collection, @design, @commentable, Comment.new]) do |f| %>
  <%= f.text_area :comment %>
  <%= f.submit "Comment", :class => "btn" %>
<% end %>

Right now when I try to submit the new comment form I get this error

undefined method `comments' for #<Collection:0x0000010150cf88>

which points back to the create method

EDIT 2

Here is my comment model

belongs_to :commentable, :polymorphic => true
belongs_to :user

Here is my design model (which is a child of the collection model)

has_many :comments, :dependent => :destroy, :as => :commentable
belongs_to :user
belongs_to :collection

and my collection model (which has the child model: design)

belongs_to :user
has_many :designs, :dependent => :destroy

and there is more to the models but its not related to the problem.

Justin
  • 4,922
  • 2
  • 27
  • 69
  • Where are you defining the `@design` and `@commentable` variables so that they are available for the forms? I only see your `create` actions. – Ryan Bigg Sep 12 '13 at 21:32
  • All i have is the create, destroy, and edit actions...when a user goes to the design page, they see a list of comments and the comment form...when they submit the comment form it just redirects back to the page (i dont want a separate page for comments like that video...that seems pointless) – Justin Sep 12 '13 at 21:35
  • Please provide a stacktrace for the error. – Ryan Bigg Sep 12 '13 at 21:46
  • When I submit the form i get `undefined method comments for #` error which points to the create method of comments – Justin Sep 12 '13 at 21:51
  • I will add more to the bottom of my post...check the second edit in a bit. – Justin Sep 12 '13 at 21:51
  • You did not provide the stacktrace as I requested. Please provide this if you are still having issues. – Ryan Bigg Sep 15 '13 at 21:20
  • sorry about that..was working on it yesterday and finished it...was gonna let you know today that i no longer needed help...thank you very much though. have a good one. – Justin Sep 16 '13 at 16:08

0 Answers0