【发布时间】:2014-07-11 22:18:28
【问题描述】:
我正在尝试实现 Best In Place gem 并遵循 Railscast,但遇到了问题。我正在学习 Rails,并正在构建一个带有两个模型的示例博客应用程序,Article 和 Comment。我正在尝试使用 Best In Place 来编辑 cmets。
_list_cmets.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。
cmets_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
【问题讨论】:
-
将
<!--<%=更改为<!--<%#=以不执行此字符串。我认为这对你的问题没有帮助,但无论如何。 -
@zishe 刚刚试了一下,还是报了同样的错误。
-
错误信息指向哪一行?哪个文件正在渲染部分内容?
-
@WaliAli
<%= best_in_place comment, :author %>。我将更新问题以澄清这一点。 -
如果您从该行中删除
best_in_place并用<%= comment.author %>替换它,您是否不再收到错误消息?那行中的代码对我来说是正确的
标签: ruby-on-rails gem rubygems best-in-place