Ruby on rails 未定义的方法`user';对于#<;注释:0x007fee941c6aa8>;宝石设计

Ruby on rails 未定义的方法`user';对于#<;注释:0x007fee941c6aa8>;宝石设计,ruby-on-rails,database,devise,one-to-many,Ruby On Rails,Database,Devise,One To Many,我正在使用gemdesigne创建用户配置文件。每个用户都可以创建一条注释。我只需要显示用户发布的评论,比如:但是我得到了一个错误,未定义方法“user”,用于#Comment:0x007fee941c6aa8 gem designe 在user.rb中 has_many :comments, dependent: :destroy 在comment.rb中 belongs_to :users in comment controller before_action :find_co

我正在使用gemdesigne创建用户配置文件。每个用户都可以创建一条注释。我只需要显示用户发布的评论,比如:但是我得到了一个错误,未定义方法“user”,用于#Comment:0x007fee941c6aa8 gem designe

在user.rb中

  has_many :comments, dependent: :destroy 
在comment.rb中

  belongs_to :users
in comment controller

before_action :find_comment ,only:[:show,:update,:edit,:destroy]

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

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

private

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

  def comment_params
    params.require(:comment).permit(:text)
  end
用户控制器

      class DoctorsController < ApplicationController
  before_action :authenticate_user!
  before_action :find_user, only: [:destroy,:edit,:update]


  def index
    @users = User.order("id DESC").all
    
  end

  def show
    @user = User.find(params[:id])
    user_id = User.find(params[:id]).id

  @comments = Comment.where(user_id: user_id)
  current_user=@comments.where(user_id: user_id)
  end

  def edit
  end

  def update
    if @user.update_attributes(user_attributes)
      redirect_to members_path, notice: "user Information updated successfully"
    else
      flash.now[:error] = "Couldn't update!"
      render :edit
    end
  end

  private
  def user_attributes
    user_attributes = params.require(:user).permit([:name,:address,:phone])
  end
  def find_user
    @user = User.find(params[:id,:name])
  end
end
类博士控制器
用户show.html.erb

<% for item in @user.comments %>
  <% if item.text.present? %>
    <%= item.text %><br>        
    <%= @comment.user.name %>
    <br><hr>
  <% end %>




设计数据库

# frozen_string_literal: true

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :name
      t.string :phone
      t.string :address
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      # t.integer  :sign_in_count, default: 0, null: false
      # t.datetime :current_sign_in_at
      # t.datetime :last_sign_in_at
      # t.inet     :current_sign_in_ip
      # t.inet     :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  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
#冻结的字符串文字:true
类designeCreateUsers
在评论数据库中

# frozen_string_literal: true

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :name
      t.string :phone
      t.string :address
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      # t.integer  :sign_in_count, default: 0, null: false
      # t.datetime :current_sign_in_at
      # t.datetime :last_sign_in_at
      # t.inet     :current_sign_in_ip
      # t.inet     :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  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
class CreateComments
在您的comment.rb中 应该是

belongs_to :user

如果您只需键入
classuserscoontroller,您的问题将更易于阅读。。。结束
,并相应缩进代码,而不是写入文件名。看@Eyeslandic它并没有给我想要的结果。通过这样做,它将为我提供在评论旁边有配置文件的用户的姓名,而不是发布评论的人的姓名。@max我添加了用户controllerIt,但它并没有提供我想要的结果。通过这样做,它会给我在评论旁边有个人资料的用户的名字,而不是发表评论的人的名字。@nourza这是我在这里给你的另一个问题的解决方案的同一个问题