nil的未定义方法`user_name':添加注释表时的NilClass

时间:2016-07-27 03:10:30

标签: ruby-on-rails ruby forms ruby-on-rails-4 form-for

一切正常,直到我添加form_for来创建注释,我得到这个错误未定义方法`user_name'为nil:NilClass ...

我寻找解决方案的时间但是徒劳无功.. 这是我的帖子视图索引文件,其中添加了用于创建注释的表单:

<div class="row">
    <div class="col-md-12">
        <%= render 'form' %>
            <div id="posts" class="transitions-enabled">
                <% @posts.each do |post| %>
                    <div class="box box-1 panel panel-default">
                      <div class="panel-body">
                        <%= link_to image_tag(post.user.avatar.url, size: "50x50", class: "img-circle"), profile_path(post.user.user_name) %>
                            <ul class="posts-shows">
                            <li><strong><%= link_to post.user.user_name, profile_path(post.user.user_name)  %></strong></li>
                            <li><small><%= link_to "#{time_ago_in_words(post.created_at)} ago", post_path(post), class: "time-link" %> </small></li>
                        </ul>
                            <p class="disc-posts"><%= post.description %></p>
                            <%= link_to image_tag(post.image.url(:large), class: "img-responsive img-in" ) %><br/>
                            <% if post.user == current_user %>
                                <div><%= link_to "Edit", edit_post_path(post) %> | <%= link_to "delete", post, method: :delete, data: {confirm: "Are you sure?"} %> </div>
                            <% else %>                          
                                <div><%= link_to "Repost", repost_post_path(post), method: :post, data: { confirm: "Are you sure?"} if user_signed_in? %></div>                 
                            <% end %>
                        </div>


                        <%= form_for([post, post.comments.build]) do |f| %> 
                            <p>
                            <%= f.label :content %><br> 
                            <%= f.text_area :content, class: "form-control"  %>
                            </p> 

                            <p> 
                            <%= f.submit %> 
                            </p> 

                        <% end %>

                        <% if post.comments.present? %>
                            <div class="panel-footer">
                                <% post.comments.each do |comment| %>
                                    <%= link_to image_tag(post.user.avatar.url, size: "30x30", class: "img-circle"), profile_path(comment.user.user_name) %>
                                    <%= link_to comment.user.user_name, profile_path(comment.user.user_name), class: "username-size" %>
                                    <%= comment.content %>
                                    <% if comment.user == current_user %>
                                        <%= link_to "delete", post_comment_path(post, comment), method: :delete, data: { confirm: "Are you sure?" } %>
                                    <% end %>
                                <% end %>
                            </div>
                        <% end %>


                    </div>
                <% end %>
            </div>
    </div>
</div>

这里是form_for:

<%= form_for([post, post.comments.build]) do |f| %> 
  <p>
    <%= f.label :content %><br> 
    <%= f.text_area :content, class: "form-control"  %>
   </p> 

   <p> 
     <%= f.submit %> 
   </p> 

 <% end %>

这是我的评论,用户和帖子模型,显示了关联:

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

.........................

class Post < ActiveRecord::Base
  validates_presence_of :description
  belongs_to :user
  has_many :comments, dependent: :destroy
end

......................

class User < ActiveRecord::Base
  has_many :comments, dependent: :destroy
  has_many :posts, dependent: :destroy
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  validates :user_name, presence: true, length: { minimum: 4, maximum: 16 } 
end

和我的评论控制员:

class CommentsController < ApplicationController
  before_action :set_post

  def index
    @comments = @post.comment.all
  end

  def new
    @comment = @post.comments.build(comments_params)
  end

  def create
    @comment = @post.comments.build(comments_params)
    @comment.user_id = current_user.id

    if @comment.save
      redirect_to :back
    else
      flash[:alert] = "Check the comment form"
      render root_path
    end
  end

  def edit

  end

  def update

  end

  def destroy
    @comment = @post.comments.find(params[:id])

    @comment.destroy
    flash[:success] = "Comment deleted :("
    redirect_to root_path
  end

  private

  def comments_params
    params.require(:comment).permit(:content)
  end

  def set_post
    @post = Post.find(params[:post_id])
  end
end

我的架构:

  create_table "comments", force: :cascade do |t|
    t.integer  "user_id"
    t.integer  "post_id"
    t.text     "content"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "comments", ["post_id"], name: "index_comments_on_post_id"
  add_index "comments", ["user_id"], name: "index_comments_on_user_id"

  create_table "posts", force: :cascade do |t|
    t.string   "description"
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.string   "image"
    t.integer  "user_id"
    t.integer  "original_post_id"
  end

  add_index "posts", ["user_id"], name: "index_posts_on_user_id"

3 个答案:

答案 0 :(得分:1)

似乎很少有评论在这一行中没有user_id

<%= link_to comment.user.user_name, profile_path(comment.user.user_name), class: "username-size" %>

尝试销毁user_id nil

的评论
Comment.destroy_all(user_id: nil)

答案 1 :(得分:0)

您确定自己的帖子表中包含user_id或其他foreign key吗?

您可以使用:

belongs_to :user, :foreign_key => 'user_id'型号

post.rb

并且:

has_many :posts, :foreign_key => 'user_id'

user.rb

答案 2 :(得分:0)

我自己对Rails很陌生,但我已经解决了类似的错误十几次。这个位似乎是一个问题:

profile_path(post.user.user_name)

在同一行代码中,您使用:

link_to post.user.user_name

但这部分似乎没问题,因为它只是显示的文字。

在实际链接中,我认为您不应该包含user_name。尝试简单地使用&#34; post.user&#34;代替。