2

I have the following route:

resources :organisations do
  resources :comments, only: [:create, :destroy]
  resources :events do
    resources :comments, only [:create, :destroy]
  end
end

As the documentation for AACWT example suggests, I try adding a comment for an event with the following code in view/comments/_comments.html.erb:

<%= form_for [commentable, Comment.new], :remote => false do |f|%>

Which, is rendered from views/events/show.html.erb by:

<%= render :partial => "comments/comment", :locals => { :commentable => @event } %>

Where @event is assigned in the respective controller action.

The create method in comments_controller.rb:

def create
  @commentable = Event.find(params[:commentable][:commentable_id])
  @user_who_commented = current_user
  @comment = Comment.build_from(@commentable, @user_who_commented.id, "Comment!" )
end

Although this might not be correct at this point in time as I have tried many different things to get this to work and need to get back to the point where my code is generating the problem. The problem being that it tries to load the path of the commentable, in this case event, so what it is trying is event_path when it should be organisation_event_path. How do I pass this to the method?

My thought were perhaps by a proc, proc.new |organisation, event| or something similar, but that is where I get stuck. I'm still learning Rails and how the associations in ActiveRecord work. Any pointers would be hugely appreciated because it will save me having to make lots of changes to the structure and having events as its own resource and not nested, which then produces all sorts of other problems.

That said, I'm ideally after the best practise way to achieve this, so if that does require a change of scheme then I guess I'll have to do it!

Ashley Bye
  • 1,752
  • 2
  • 23
  • 40

0 Answers0