【问题标题】:'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path while creating comments Rails'nil' 不是 ActiveModel 兼容的对象。它必须在创建评论时实现 :to_partial_path Rails
【发布时间】:2015-08-04 18:48:15
【问题描述】:

好的,伙计们,作为我在 Bloc 的项目之一,我正在创建一个 reddit 克隆。对于这个特定的任务,我将创建一个功能,用户可以在其中评论每个帖子(witch 已经嵌套在每个主题中)。但是,当我尝试查看与某个主题相关联的帖子时遇到此错误。

ArgumentError in Posts#show
'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.

到目前为止,我已经创建了 cmets 控制器,在 cmets 和用户之间建立了外键连接,使 cmets 路由嵌套在帖子路由中,创建了一个用于提交评论的表单部分和一个在 post/show 中的评论部分,并创建了一个 CommentPolicy 以授权用户创建新的 cmets。

我检查了我的数据库,并且 cmets 确实存在,因为我将它们添加到我的种子文件中。我怀疑我的错误在于我的表格和我对部分内容的引用,但我有点难过。在这里的任何帮助将不胜感激。谢谢!

我的代码:

用户.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  has_many :posts
  has_many :comments
  mount_uploader :avatar, AvatarUploader

  def admin?
    role == 'admin'
  end

  def moderator?
    role == 'moderator'
  end
end

comment.rb

class Comment < ActiveRecord::Base
  belongs_to :post
  belongs_to :user
end

routes.rb 中的各个路由

  resources :topics do
    resources :posts, except: [:index] do
      resources :comments, only: [:create]
    end
  end

cmets_controller:

class CommentsController < ApplicationController
  def index
  end

  def create
    @topic = Topic.find(params[:topic_id])
    @post = Post.find(params[:post_id])
    @comments = @post.comments

    @comment = current_user.comments.build(params[:comment])
    @comment.post = @post
    @new_comment = Comment.new

    authorize @comment

      if @comment.save
        flash[:notice] = "Comment was saved"
      else
        flash[:error] = "There was an error saving this comment. Please try again."
      end
  end
end

posts_controller:

class PostsController < ApplicationController
  def show
    @post = Post.find(params[:id])
    @topic = Topic.find(params[:topic_id])
    @comment = Comment.find(params[:id])
    authorize @comment
  end

  def new
    @topic = Topic.find(params[:topic_id])
    @post = Post.new
    authorize @post
  end

  def create
    @topic = Topic.find(params[:topic_id])
    @post = Post.new(post_params)
    @post.user = current_user
    @post.topic = @topic
    authorize @post
      if @post.save
        flash[:notice] = "Post was saved."
        redirect_to [@topic, @post]
      else
        flash[:error] = "There was an error saving this post. Please try again."
        render :new
      end
  end

  def edit
    @topic = Topic.find(params[:topic_id])
    @post = Post.find(params[:id])
    authorize @post
  end

   def update
     @topic = Topic.find(params[:topic_id])
     @post = Post.find(params[:id])
     authorize @post
     if @post.update_attributes(post_params)
       flash[:notice] = "Post was updated."
       redirect_to [@topic, @post]
     else
       flash[:error] = "There was an error saving the post. Please try again."
       render :edit
     end
   end

   private

  def post_params
    params.require(:post).permit(:title, :body, :image)
  end

end

cmets/_form.html.erb

<%= form for [topic, post, comment] do |f| %>
  <div class="form-group">
    <%= f.label :body %>
    <%= f.text_area :body, rows: 8, class: 'form-control', placeholder: "Enter comment here" %>
  </div>
  <div class="form-group">
    <%= f.submit "Add comment", class: "btn btn-success" %>
  </div>
<% end %>

cmets/_comment.html.rb

<%= comment.body %>

posts/show.html.erb

<h1><%= @post.markdown_title %></h1>

<div class="row">
  <div class="col-md-8">
    <small>
      <%= image_tag(@post.user.avatar.tiny.url) if @post.user.avatar? %>
      submitted <%= time_ago_in_words(@post.created_at) %> ago by
      <%= @post.user.name %>
    </small>
    <p><%= @post.markdown_body %></p>
    <small>
      <%= image_tag @post.image_url%> 
    </small>
  </div>
  <div class="col-md-4">
    <% if policy(@post).edit? %>
      <%= link_to "Edit", edit_topic_post_path(@topic, @post), class: 'btn btn-success' %>
    <% end %>
  </div>

  <%= render @comments %>

    <% if policy(@comment).create? %> 
      <h4>New Comment</h4>
      <%= render partial: 'comments/form', locals: { topic: @topic, post: @post, comment: @comment } %>
    <% end %>

comment_policy

class CommentPolicy < ApplicationPolicy

  def new
    user.present?
  end

  def create
    user.present?
  end

end

种子文件中的cmets

#Create Comments
100.times do
    Comment.create!(
        user: users.sample,   # we have not yet associated Users with Comments
        post: posts.sample,
        body: Faker:: Lorem.paragraph
    )
end
comments = Comment.all

【问题讨论】:

    标签: ruby-on-rails forms partials


    【解决方案1】:

    相信错误在这一行

    <%= render @comments %>
    

    在你的posts/show.html.erb

    您没有在posts_controllershow 方法中定义@comments。尝试将@comments = @post.comments 放入show 方法中。

    def show
      @post = Post.find(params[:id])
      @topic = Topic.find(params[:topic_id])
      @comment = Comment.find(params[:id])
      authorize @comment
      @comments = @post.comments
    end
    

    【讨论】:

    • 谢谢,当用户未登录时,我可以看到 cmets,但是当我登录时,我看到以下错误:comments/_form.html.erb:1: syntax error, unexpected keyword_do_block, expecting :: or '[' or '.' ...m for [topic, post, comment] do |f| @output_buffer.safe_appe... ... ^ /Users/sethjones/code/bloccit/app/views/comments/_form.html.erb:10: syntax error, unexpected keyword_ensure, expecting end-of-input
    • 并在 cmets/_form 中突出显示 &lt;%= form for [topic, post, comment] do |f| %&gt;,在帖子/显示中突出显示 &lt;%= render partial: 'comments/form', locals: { topic: @topic, post: @post, comment: @comment } %&gt;
    • @user3700231 这是错误的@comment = Comment.find(params[:id])。尝试调试它
    • 啊,我注意到我的表单中有语法问题。我将&lt;%= form for [topic, post, comment] do |f| %&gt; 更改为&lt;%= form_for [topic, post, comment] do |f| %&gt; 现在我收到此错误undefined method `topic_post_comment_path,它在cmets/form 中突出显示&lt;%= form_for [topic, post, comment] do |f| %&gt;,在posts/show 中突出显示&lt;%= render partial: 'comments/form', locals: { topic: @topic, post: @post, comment: @comment } %&gt;
    猜你喜欢
    • 2018-08-04
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    相关资源
    最近更新 更多