Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 与用户和帖子关联时,评论表中的用户id为空_Ruby On Rails 4_Railstutorial.org - Fatal编程技术网

Ruby on rails 4 与用户和帖子关联时,评论表中的用户id为空

Ruby on rails 4 与用户和帖子关联时,评论表中的用户id为空,ruby-on-rails-4,railstutorial.org,Ruby On Rails 4,Railstutorial.org,我正在使用我们在rails教程中构建的注册和登录作为我正在制作的Reddit克隆的基础。从目前情况看,应用程序运行正常,但在我发表评论时,注释表中的用户id为空,链接id存在且正确,因此我可以对链接发表评论。链接表中的用户id也存在且正确 我很确定我犯的错误是在comments_controller.rb的创建操作中,但也可能是我最初的迁移。让我(作为新手)困惑的是,我曾经用rails 4.1.8和设备以当前的形式工作过一次。但是,在rails 4.2.1中使用这种方法,并以rails教程为基础

我正在使用我们在rails教程中构建的注册和登录作为我正在制作的Reddit克隆的基础。从目前情况看,应用程序运行正常,但在我发表评论时,注释表中的用户id为空,链接id存在且正确,因此我可以对链接发表评论。链接表中的用户id也存在且正确

我很确定我犯的错误是在comments_controller.rb的创建操作中,但也可能是我最初的迁移。让我(作为新手)困惑的是,我曾经用rails 4.1.8和设备以当前的形式工作过一次。但是,在rails 4.2.1中使用这种方法,并以rails教程为基础,这是行不通的。我在这里有点新,所以我希望我已经正确地阐述了这篇文章,并提供了足够的信息,以便有人能给我一些关于这个问题的建议

评论控制器

    before_action :logged_in_user, only: [:create, :destroy]

def create
    @link = Link.find(params[:link_id])
    @comment = @link.comments.create(params[:comment].permit(:link_id, :body))
    @comment.user = User.find(current_user.id)
    redirect_to link_path(@link)
end

def destroy
    @link = Link.find(params[:link_id])
    @comment = @link.comments.find(params[:id])
    @comment.destroy
    redirect_to link_path(@link)
end

private

def logged_in_user
    unless logged_in?
        flash[:danger] = "Please log in."
        redirect_to login_url
    end
end
表格 app/views/links/show.html.erb

<h2 class="comment-count"><%= pluralize(@link.comments.count, "comment") %></h2>
<%= render @link.comments %>
<%= form_for([@link, @link.comments.build]) do |f| %>
<%= render 'comments/fields', f: f %>
<%= f.submit "Save Comment", class: 'btn btn-primary margin-bottom-10' %>
<% end %>
型号 app/models/link.rb

belongs_to :user
has_many :comments, dependent: :destroy
app/models/comment.rb

belongs_to :user
belongs_to :link
validates :body,  presence: true
app/models/user.rb

has_many :links, dependent: :destroy
迁移

class CreateComments < ActiveRecord::Migration
    def change
    create_table :comments do |t|
    t.integer :link_id
    t.text :body
    t.references :user, index: true, foreign_key: true

    t.timestamps null: false
   end
   add_index :comments, :link_id
   end
end
class CreateComments
希望这是一个足够好的帖子,我已经包含了你需要的一切,所以有人可以帮助我,这是一个新手的错误,但我看不到。提前谢谢


就目前的情况来看,您当前正在创建一条
注释
,而没有为其保存
用户id
。请尝试下面的代码

#comments_controller.rb
def create
  @link = Link.find(params[:link_id])
  @comment = @link.comments.new(params[:comment].permit(:link_id, :body))
  @comment.user = User.find(current_user.id)
  @comment.save
  redirect_to link_path(@link)
end

我忘了@comment.save,这真是太好笑了。真的有点尴尬,谢谢你的朋友:)comments.new代替comments.create有什么区别??
has_many :links, dependent: :destroy
class CreateComments < ActiveRecord::Migration
    def change
    create_table :comments do |t|
    t.integer :link_id
    t.text :body
    t.references :user, index: true, foreign_key: true

    t.timestamps null: false
   end
   add_index :comments, :link_id
   end
end
#comments_controller.rb
def create
  @link = Link.find(params[:link_id])
  @comment = @link.comments.new(params[:comment].permit(:link_id, :body))
  @comment.user = User.find(current_user.id)
  @comment.save
  redirect_to link_path(@link)
end