首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >最佳创业板NoMethodError

最佳创业板NoMethodError
EN

Stack Overflow用户
提问于 2014-05-22 18:09:03
回答 1查看 873关注 0票数 2

我正在尝试实现最佳就位 gem,并与铁道一起执行,但存在一些问题。我正在学习Rails,并正在构建一个包含两个模型的示例博客应用程序,ArticleComment。我试着用最好的地方来编辑评论。

_list_comments.html.erb

代码语言:javascript
复制
<% @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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-25 01:39:59

我克隆了您的项目,并找出了为什么会出现此错误:

代码语言:javascript
复制
undefined method comment_path for #<#<Class:0x007fdc38fb8288>:0x007fdc38fc36b0>

下面是正在发生的事情。在此代码<%= best_in_place comment, :author %>中,您将传递comment对象。best_in_place试图将其映射到名为comment_path的路由助手,方法是将路径附加到注释(提示:未定义的方法comment_path)。

你的路线是这样设置的:

代码语言:javascript
复制
resources :articles do
    resources :comments
end

如果您使用rake路由,您会注意到您没有一个名为comment_path的助手路径,它对应于您的comments#show。它正在寻找comment_path,却找不到它。因此,错误消息。

但是,由于使用嵌套资源,对应于comments#show的辅助路径称为article_comment_path。以下是完整的映射:

代码语言:javascript
复制
article_comment_path GET /articles/:article_id/comments/:id(.:format) comments#show

要将best_in_place映射到正确的助手路径,可以解决以下两种方法:

1)通过简单地将此路由添加到comment_path文件中,创建映射到routes.rb的路由:

代码语言:javascript
复制
resources :comments, only: [:show]

上述路线的完整映射如下:

代码语言:javascript
复制
comment_path GET /comments/:id(.:format) comments#show

现在您有了comment_path。不再有错误消息。

2)调整代码以映射到article_comment_path

取代:

代码语言:javascript
复制
<%= best_in_place comment, :author %>

通过以下方式:

代码语言:javascript
复制
<%= best_in_place [@article, comment], :author %>

数组[@article, comment]将通过在@项目中添加注释来构建助手路径article_comment_path

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23814255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档