【问题标题】:NoMethodError for Best In Place gem最佳就地宝石的 NoMethodError
【发布时间】: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 #&lt;#&lt;Class:0x007fdc38fb8288&gt;:0x007fdc38fc36b0&gt;):,它指的是&lt;%= best_in_place comment, :author %&gt;。我很确定我正确安装了所有东西,所以我不知道问题出在哪里。

当我将 &lt;%= best_in_place comment, :author %&gt; 更改为 &lt;%= best_in_place "#{comment}", :author %&gt; 时,它给了我这个错误:undefined method 'author' for "#&lt;Comment:0x007fdc3c841820&gt;":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

【问题讨论】:

  • 将 &lt;!--&lt;%= 更改为 &lt;!--&lt;%#= 以不执行此字符串。我认为这对你的问题没有帮助,但无论如何。
  • @zishe 刚刚试了一下,还是报了同样的错误。
  • 错误信息指向哪一行?哪个文件正在渲染部分内容?
  • @WaliAli &lt;%= best_in_place comment, :author %&gt;。我将更新问题以澄清这一点。
  • 如果您从该行中删除best_in_place 并用&lt;%= comment.author %&gt; 替换它,您是否不再收到错误消息?那行中的代码对我来说是正确的

标签: ruby-on-rails gem rubygems best-in-place


【解决方案1】:

我能够克隆您的项目并找出您收到此错误的原因:

undefined method comment_path for #<#<Class:0x007fdc38fb8288>:0x007fdc38fc36b0>

这就是正在发生的事情。在这段代码&lt;%= best_in_place comment, :author %&gt; 中,您传递的是comment 对象。 best_in_place 正在尝试通过将路径附加到注释来将其映射到名为 comment_path 的路由助手(提示:未定义的方法 comment_path)。

您的路线是这样设置的:

resources :articles do
    resources :comments
end

如果您执行 rake 路由,您会注意到您没有与您的 comments#show 对应的名为 comment_path 的辅助路径。它正在寻找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 上附加注释来构建帮助程序路径article_comment_path

【讨论】:

    猜你喜欢
    • 2012-07-07
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多