无法写入未知属性 `user_id`

can't write unknown attribute `user_id`

我正在使用 gem 设计来创建用户配置文件。每个用户都可以在任何个人资料中创建评论我想在他写的评论下添加每个用户名。

一个用户有很多评论

一个用户有很多个人资料评论,这是用户和评论之间的关系这是我创建评论时得到的错误

can't write unknown attribute `user_id`

这是我做的

class CreateProfileComments < ActiveRecord::Migration
      def change
        create_table :profile_comments do |t|
    
          t.timestamps null: false
        end
      end
    end

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.text :text
      t.references :user, index: true, foreign_key: true
      
      t.timestamps null: false
    end
  end
end

在 user.rb 模型中

  has_many :comments
  has_many :profile_comments, dependent: :destroy

在comment.rb

class Comment < ActiveRecord::Base
  belongs_to :user #not users as you have defined in your question
  has_many :profile_comments
end

个人资料comment.rb

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

在评论控制器中

class CommentsController < ApplicationController
  before_action :find_comment ,only:[:show,:update,:edit,:destroy]

   def new
    @user =User.find(params[:id])
    @comment = @user.comments.build
  end

  def index

  end

  def create
    @user =User.find(params[:id])
    @comment = current_user.comments.build(comment_params)
    @profile_comment = ProfileComment.new
    @user.profile_comments < @profile_comment
    @comment.profile_comment < @profile_comment
    if @comment.save
      redirect_to doctor_path(:id => @user.id)
    end
  end 

  def edit
  end

  def update

    if @comment.update(comment_params)
      redirect_to root_path, alert: "user Information updated successfully"
    else
      flash.now[:error] = "Couldn't update!"
      render :edit
    end
  end
  def destroy
    @comment.destroy
    redirect_to doctors_path, notice: "comment deleted Successfully"
  end 

private

  def find_comment
    @comment = Comment.find(params[:id])
  end

  def comment_params
    params.require(:comment).permit(:text)
  end
end

在用户个人资料视图中

      <% @user.profile_comments.each do | profile_comment |%>
  <%comment = profile_comment.comment%>
  <% if comment.text.present? %>
    <%= comment.text %><br>
    <%if comment.user.blank?%>
      No user assigned to this comment
    <%else%>
      <%= comment.user.name #or email or whatever%>
    <%end%>
    <br><hr>
  <% end %>
<% end %>

我猜你真正想要的只是创建两个指向相同的关联 table:

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.text :text
      t.references :user, index: true, foreign_key: true
      t.references :author, index: true, foreign_key: { to_table: :users }
      t.timestamps null: false
    end
  end
end

此处user_id引用评论的目标用户,author_id引用撰写评论的用户。两者都引用 users.id.

然后在您的评论模型中创建两个 belongs_to 关联:

class Comment < ApplicationRecord
  belongs_to :user
  belongs_to :author, 
    class_name: 'User', 
    inverse_of: :authored_comments
end

以及您的用户模型中的两个 has_many 关联:

class User < ApplicationRecord
  # comments about this user
  has_many :comments 
  # comments written by this user 
  has_many :authored_comments, 
    class_name: 'Comment',
    foreign_key: :author_id,
    inverse_of: :author
end

我不知道你为什么要混合另一个 table 因为这两种关系都是一对多的。

class CommentsController < ApplicationController
  before_action :set_user, only: [:new, :create]
  
  # ...

  def new
    @comment = @user.comments.new
  end

  def create
    @comment = @user.comments.new(comment_params) do |c|
      c.author = current_user
    end
    if @comment.save
      redirect_to doctor_path(id: @user.id)
    else
      render :new
    end
  end 

  private

  def set_user
    @user = User.find(params[:id])
  end

  # ...
end

如果您随后想要显示评论,您可以这样做:

<% @user.comments.each do |comment| %>
<div class="comment">
  <div class="body">
    <%= comment.body %>
  </div>
  <p class="author"><%= comment.author.email %></p>
</div>  
<% end %>