I am trying out a commenting system in a way described by ryanb in https://github.com/railscasts/154-polymorphic-association/tree/master/revised/blog-after.
It uses Rails 3 nested resource routing seen in routes.rb:
resources :articles do
resource :comments
end
Comments are loaded by parents type and id as seen in articles_controller.rb and comments_controller.rb:
class ArticlesController < ApplicationController
...
def show
@article = Article.find(params[:id])
@commentable = @article
@comments = @commentable.comments
@comment = Comment.new
end
class CommentsController < ApplicationController
before_filter :load_commentable
def index
@comments = @commentable.comments
end
...
private
def load_commentable
resource, id = request.path.split('/')[1, 2]
@commentable = resource.singularize.classify.constantize.find(id)
end
...
end
How would I go about adding a link to a comments edit or destroy actions in comments’ view template?
<% @comments.each do |comment| %>
<div class="comment">
<%= simple_format comment.content %>
<%= link_to "Delete", comment, method: :delete %>
</div>
<% end %>
With jdoes advice I looked more into the array passing and noticed routes.rb was missing an s from resources :comments. Fixed version:
Now the template works perfectly just by giving the link as an array.