我正在尝试实现最佳就位 gem,并与铁道一起执行,但存在一些问题。我正在学习Rails,并正在构建一个包含两个模型的示例博客应用程序,Article和Comment。我试着用最好的地方来编辑评论。
_list_comments.html.erb
<% @comments.each do |comment| %>
<hr />
<%= link_to article_comment_path(@article, comment), method: :delete, data: {confirm: 'Are you sure?'}, remote: true do %>
<span class="glyphicon glyphicon-remove"></span>
<% end %>
<%= content_tag :span, '', id: "#{comment.id}", class: "glyphicon glyphicon-edit edit_comment" %>
<!--<%= content_tag :p, content_tag(:small, "#{comment.author}"), id: "comment_author_#{comment.id}" %>-->
<%= best_in_place comment, :author %>
<%= content_tag :p, id: "comment_body_#{comment.id}" do %>
<%= comment.body %>
<% end %>
<% end %>它给了我一个错误:ActionView::Template::Error (undefined method comment_path for #<#<Class:0x007fdc38fb8288>:0x007fdc38fc36b0>):,它指的是<%= best_in_place comment, :author %>。我很确定我安装的所有东西都是正确的,所以我不知道问题出在哪里。
当我将<%= best_in_place comment, :author %>更改为<%= best_in_place "#{comment}", :author %>时,它会给出以下错误:undefined method 'author' for "#<Comment:0x007fdc3c841820>":String。
comments_controller.html.erb
class CommentsController < ApplicationController
def create
@comment = Comment.new(comment_params)
@comment.article_id = params[:article_id]
if @comment.save
respond_to do |f|
f.html { redirect_to article_path(params[:article_id]), notice: 'Comment created!' }
f.js {
@article = Article.find(params[:article_id])
@comment = @comment
@comments = Comment.where(article_id: params[:article_id])
}
end
else
redirect_to article_path(params[:article_id]), warning: 'Unable to create comment.'
end
end
def destroy
@comment = Comment.find(params[:id]).destroy
respond_to do |f|
f.html { redirect_to article_path(params[:article_id]) }
f.js {
@article = Article.find(params[:article_id])
@comments = Comment.where(article_id: params[:article_id])
}
end
end
def update
@comment = Comment.find(params[:id])
if @comment.update(comment_params)
respond_to do |f|
f.html { redirect_to article_path(@comment.article_id) }
f.json { render head :ok}
end
else
respond_to do |f|
f.html { redirect_to article_path(@comment.article_id) }
f.json { render json: @comment.errors.full_messages, status: :unprocessable_entity }
end
end
end
private
def comment_params
params.require(:comment).permit(:author, :body)
end
end发布于 2014-05-25 01:39:59
我克隆了您的项目,并找出了为什么会出现此错误:
undefined method comment_path for #<#<Class:0x007fdc38fb8288>:0x007fdc38fc36b0>下面是正在发生的事情。在此代码<%= best_in_place comment, :author %>中,您将传递comment对象。best_in_place试图将其映射到名为comment_path的路由助手,方法是将路径附加到注释(提示:未定义的方法comment_path)。
你的路线是这样设置的:
resources :articles do
resources :comments
end如果您使用rake路由,您会注意到您没有一个名为comment_path的助手路径,它对应于您的comments#show。它正在寻找comment_path,却找不到它。因此,错误消息。
但是,由于使用嵌套资源,对应于comments#show的辅助路径称为article_comment_path。以下是完整的映射:
article_comment_path GET /articles/:article_id/comments/:id(.:format) comments#show要将best_in_place映射到正确的助手路径,可以解决以下两种方法:
1)通过简单地将此路由添加到comment_path文件中,创建映射到routes.rb的路由:
resources :comments, only: [:show]上述路线的完整映射如下:
comment_path GET /comments/:id(.:format) comments#show现在您有了comment_path。不再有错误消息。
2)调整代码以映射到article_comment_path
取代:
<%= best_in_place comment, :author %>通过以下方式:
<%= best_in_place [@article, comment], :author %>数组[@article, comment]将通过在@项目中添加注释来构建助手路径article_comment_path。
https://stackoverflow.com/questions/23814255
复制相似问题