I use polymorphic associations with comments. When I try to add 'edit' and also 'destroy' in show template, I get the error in the title (just edit for now). How do I add both links to show?
comments_controller.rb
class CommentsController < ApplicationController
....
before_action :signed_in_user, only: [:new, :edit]
before_filter :load_commentable
def index
@commentable = load_commentable
@comments = @commentable.comments
end
def show
end
def edit
@commentable = load_commentable
end
def new
@commentable = load_commentable
@comment = @commentable.comments.new
end
def create
@comment = @commentable.comments.new(comment_params)
@comment.user = current_user
if @comment.save
redirect_to @comment, notice: "Created."
else
render :new
end
end
def update
@comment = @commentable.comments.build(comment_params)
@comment.user = current_user
respond_to do |format|
if @comment.update(comment_params)
format.html { redirect_to @comment, notice: 'It was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
private
def load_commentable
resource, id = request.path.split('/')[1, 2]
@commentable = resource.singularize.classify.constantize.find(id)
end
....
end
show.html.erb template
<%= link_to "Edit", [:edit, @commentable, :comment] %>
form
<%= form_for [@commentable, @comment] do |f| %>
....
log
Processing by CommentsController#show as HTML
Parameters: {"post_id"=>"1", "id"=>"2"}
Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT 1 [["id", "2"]]
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "1"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]]
(0.2ms) SELECT COUNT(*) FROM "comments" WHERE "comments"."user_id" = ? [["user_id", 2]] CACHE (0.0ms) SELECT COUNT(*) FROM "comments" WHERE "comments"."user_id" = ? [["user_id", 2]]
Rendered comments/show.html.erb within layouts/application (10.7ms)
Completed 500 Internal Server Error in 19msActionView::Template::Error (No route matches {:action=>"edit", :controller=>"comments", :post_id=>#, :id=>nil, :format=>nil} missing required keys: [:id]):
25: <div class="thumbsdown"><%= link_to image_tag('othericons/thumbiconDown.PNG', height: '20', width: '20'),
"#" %>
26: <%= link_to image_tag('othericons/flagicon.PNG', height: '20', width: '18'), "#" %>
27:
28: <%= link_to "Edit", [:edit, @commentable, :comment] %> 29:
30:
31: app/views/comments/show.html.erb:28:in `_app_views_comments_show_html_erb___2937579164590753686_69833853514120'Rendered /home/action/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb
(1.6ms) Rendered /home/action/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.2ms) Rendered /home/action/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (19.1ms)
routes:
resources :posts do
resources :comments
end
ActionController::UrlGenerationError in Comments#show
– Kirti Thorat♦routes.rb
– Kirti Thorat♦