未定义的方法`回复'为nil:NilClass

时间:2015-02-04 21:55:19

标签: ruby-on-rails ruby ruby-on-rails-4

我从views / comments / _comment.html.erb收到错误,其中第27行     undefined方法`回复'为nil:NilClass

我正在尝试添加对我的评论的回复。但出了点问题。

这是我的代码。

的routes.rb

  resources :movies do
    resources :comments do
      resources :replies
    end
  end

在控制器中

replies_controller.rb

class RepliesController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
def create
            @comment = Comment.find(params[:comment_id])
            @movie = Movie.find(params[:movie_id])
            @reply = @comment.replies.build(reply_params)
            @reply.comment = @comment
            @reply.user = current_user
            @reply.save
            redirect_to @movie
end


def destroy
    @comment = Comment.find(params[:comment_id])
    @movie = Movie.find(params[:movie_id])
    @reply = Reply.find(params[:id])
    @reply.destroy
    redirect_to @movie
end

private
    # Never trust parameters from the scary internet, only allow the white list through.
    def reply_params
        params.require(:reply).permit(:comment_id, :user_id, :body)
    end

comment_controller.rb

class CommentsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
def create
            @movie = Movie.find(params[:movie_id])
            @comment = @movie.comments.build(comment_params)
            @comment.movie = @movie
            @comment.user = current_user
            @comment.save
            redirect_to @movie
end


def destroy
    @movie = Movie.find(params[:movie_id])
    @comment = Comment.find(params[:id])
    @comment.destroy
    redirect_to @movie
end

private
    # Never trust parameters from the scary internet, only allow the white list through.
    def comment_params
        params.require(:comment).permit(:movie_id, :user_id, :body)
    end

在模型中

replies.rb

class Reply < ActiveRecord::Base
  belongs_to :comment
  belongs_to :user
end

comments.rb

class Comment < ActiveRecord::Base
  belongs_to :movie
  belongs_to :user
  has_many :replies
  default_scope {order('comments.created_at DESC')}
        def self.from_users_followed_by(user)
          followed_user_ids = "SELECT followed_id FROM relationships
                               WHERE follower_id = :user_id"
          where("user_id IN (#{followed_user_ids}) OR user_id = :user_id",user_id: user.id)
      end
end

观看

评论/ _comment.html.erb

    <%= div_for comment do %>
            <p>
                <strong>
                    <span class="user">
                        <%= link_to comment.user.name, comment.user %>
                    </span>
                </strong>

                            Posted <%= time_ago_in_words(comment.created_at) %> ago

                    <br/>
                <strong>
                    <span class="name">
                        <%= link_to comment.movie.name, comment.movie %>
                    </span>
                </strong>  
                    <%= comment.body %>
                    <br/>
                    <% if current_user?(comment.user) %>
                    <%= link_to "delete", [comment.movie, comment], method: :delete,
                                             data: { confirm: "You sure?" },
                                             title: comment.body %>
          <% end %>
            </p>

             <div id="reply">
                 <%= render :partial => @comment.replies %>
             </div>

            <%= form_for [@comment, Reply.new] do |f| %>
            <p>
                    <%= f.label :body, "New reply" %><br/>
                    <%= f.text_area :body %>
            </p>
            <p><%= f.submit "Add reply" %></p>
    <% end %>
    <% end %>

replies/_reply.html.erb

<%= div_for reply do %>
        <p>
            <strong>
                <span class="user">
                    <%= link_to reply.user.name, reply.user %>
                </span>
            </strong>

                        Posted <%= time_ago_in_words(reply.created_at) %> ago

                <br/>

                <%= reply.body %>
                <br/>
                <% if current_user?(reply.user) %>
                <%= link_to "delete", [reply.comment, reply], method: :delete,
                                         data: { confirm: "You sure?" },
                                         title: reply.body %>
      <% end %>
        </p>
<% end %>

感谢您的帮助!

我改变了

 <div id="reply">
     <%= render :partial => @comment.replies %>
 </div>

进入

 <div id="reply">
     <%= render :partial => comment.replies %>
 </div>

并且错误消失了

但是我收到了一个新的错误 views / comments / _comment.html.erb第30行

undefined method `replies_path' for #<#<Class:0x007ff737a8d4d8>:0x007ff736c018d8>

这里是comments / _comment.html.erb

<%= div_for comment do %>
        <p>
            <strong>
                <span class="user">
                    <%= link_to comment.user.name, comment.user %>
                </span>
            </strong>

                        Posted <%= time_ago_in_words(comment.created_at) %> ago

                <br/>
            <strong>
                <span class="name">
                    <%= link_to comment.movie.name, comment.movie %>
                </span>
            </strong>  
                <%= comment.body %>
                <br/>
                <% if current_user?(comment.user) %>
                <%= link_to "delete", [comment.movie, comment], method: :delete,
                                         data: { confirm: "You sure?" },
                                         title: comment.body %>
      <% end %>
        </p>

         <div id="reply">
             <%= render :partial => comment.replies %>
         </div>

        <%= form_for [@comment, Reply.new] do |f| %>
        <p>
                <%= f.label :body, "New reply" %><br/>
                <%= f.text_area :body %>
        </p>
        <p><%= f.submit "Add reply" %></p>
<% end %>
<% end %>

回复和评论的路线

Prefix Verb   URI Pattern                                                       Controller#Action
   movie_comment_replies GET    /movies/:movie_id/comments/:comment_id/replies(.:format)          replies#index
                         POST   /movies/:movie_id/comments/:comment_id/replies(.:format)          replies#create
 new_movie_comment_reply GET    /movies/:movie_id/comments/:comment_id/replies/new(.:format)      replies#new
edit_movie_comment_reply GET    /movies/:movie_id/comments/:comment_id/replies/:id/edit(.:format) replies#edit
     movie_comment_reply GET    /movies/:movie_id/comments/:comment_id/replies/:id(.:format)      replies#show
                         PATCH  /movies/:movie_id/comments/:comment_id/replies/:id(.:format)      replies#update
                         PUT    /movies/:movie_id/comments/:comment_id/replies/:id(.:format)      replies#update
                         DELETE /movies/:movie_id/comments/:comment_id/replies/:id(.:format)      replies#destroy
          movie_comments GET    /movies/:movie_id/comments(.:format)                              comments#index
                         POST   /movies/:movie_id/comments(.:format)                              comments#create
       new_movie_comment GET    /movies/:movie_id/comments/new(.:format)                          comments#new
      edit_movie_comment GET    /movies/:movie_id/comments/:id/edit(.:format)                     comments#edit
           movie_comment GET    /movies/:movie_id/comments/:id(.:format)                          comments#show
                         PATCH  /movies/:movie_id/comments/:id(.:format)                          comments#update
                         PUT    /movies/:movie_id/comments/:id(.:format)                          comments#update
                         DELETE /movies/:movie_id/comments/:id(.:format)                          comments#destroy

1 个答案:

答案 0 :(得分:0)

一般情况下,要在堆栈跟踪中的文件和行号调试此类事物,请查看哪个变量意外为零,然后查看原因。

没有那个,我的第一个猜测就是你评论部分的这一行:

<%= render :partial => @comment.replies %>

如果堆栈跟踪指向此处,请尝试将@comment更改为comment。如果使用render @comments从父视图渲染部分,则在该部分中变量将是普通的局部变量。

如果不是这样,那么请将视图发布到您正在呈现注释的部分,以及足够的堆栈跟踪以显示发生错误的文件和行号。

更新: 这一行:

<%= form_for [@comment, Reply.new] do |f| %>

应该看起来更像:

<%= form_for(Reply.new) do |f| %>
  <%= f.hidden_field(:comment_id, @comment.id) %>

这样新的回复将包含当前评论的链接,但是提交此表单现在应该将其发送到replies_controller #create方法。