我正在尝试建立一个评论系统。并使用ajax更新部分。每条评论都属于拍卖。现在我从小时起就有这个错误..并且无法弄明白:
拍卖中的NoMethodError#show
“undefined method`stringify_keys'for / auctions / 2 / comments / 9”:String
摘录来源(第12行左右):
</p>
<p>
<%= link_to 'Destroy Comment', auction_comment_path(comment.auction, comment), id: "#{comment.id}_delete", remote: true, method: :delete, data: { confirm: 'Are you sure?' } do %>
<button class="btn btn-flat grey lighten-2 grey-text text-darken-2 waves-effect waves-light"><i class="material-icons">delete</i></button>
<% end %>
<%= link_to 'Edit Comment', edit_auction_comment_path(comment.auction, comment), remote: true do %>
comments_controller.rb:
class CommentsController < ApplicationController
before_action :all_comments, only: [:index, :create, :update, :destroy]
before_action :set_comments, only: [:edit, :update, :destroy]
respond_to :html, :js
# New Comment (Form)
def new
# find Auction related to new Comment
@auction = Auction.find(params[:auction_id])
@comment = Comment.new
@auction.comments.build
end
def index
# created in order to handle renders from this controller, which produce URL 'root/auctions/:id/comments'
auction = Auction.find(params[:auction_id])
redirect_to auction
end
# Create Comment
def create
# find Auction related to new Comment
@auction = Auction.find(params[:auction_id])
@comment = @auction.comments.build(comment_params)
@comment.user_id = current_user.id
@comment.save
redirect_to auction_path(@auction), :notice => "Comment sent"
end
# Edit Comment
def update
@comment = Comment.find(params[:id])
@comment.update_attributes(params[:body])
end
# Delete Comment
def destroy
@comment = Comment.find(params[:id])
@auction = comment.auction
@comment.destroy
#redirect_to auction_path(@auction), :notice => "Comment deleted"
end
private
def all_comments
@comments = Comment.all
end
def set_comments
@auction = Auction.find(params[:auction_id])
@comment = @auction.comments.find(params[:id])
end
def comment_params
params.require(:comment).permit(:body)
end
end
视图/拍卖/ show.html.erb:
<h2>Questions & Answers</h2>
</div>
<div class="col s12">
<%= link_to new_auction_comment_path(@auction), remote: true do %>
<button class="btn">New Question</button>
<% end %>
</div>
</div>
<div class="row">
<div class="col s12" id="comment-form" style="display:none;"></div>
</div>
<div class="row">
<div class="col s12" id="comments"><%= render @auction.comments %></div>
</div>
视图/评论/ _comment.html.erb
<p>
<strong>Commenter:</strong>
<%= comment.user.name %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<p>
<%= link_to 'Destroy Comment', auction_comment_path(comment.auction, comment), id: "#{comment.id}_delete", remote: true, method: :delete, data: { confirm: 'Are you sure?' } do %>
<button class="btn btn-flat grey lighten-2 grey-text text-darken-2 waves-effect waves-light"><i class="material-icons">delete</i></button>
<% end %>
<%= link_to 'Edit Comment', edit_auction_comment_path(comment.auction, comment), remote: true do %>
<button>Edit</button>
<% end %>
</p>
视图/评论/ _form.html.erb
<%= simple_form_for [@auction, @auction.comments.build], remote: true do |f| %>
<%= f.input :body %>
<%= f.button :submit %>
<% end %>