ruby-on-rails - 未定义的方法“用户”#宝石设计
问题描述
我正在使用 gem 设计来创建用户配置文件。每个用户都可以创建评论。我只需要显示用户发布的评论,例如: <%= @comment.user.name %> 但我得到了这个错误 undefined method `user' for #Comment:0x007fee941c6aa8 gem devise
在用户.rb
has_many :comments, dependent: :destroy
在评论.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
解决方案
在您的comment.rb 中应该是
belongs_to :user
推荐阅读
- javascript - 无法将javascript中的eventListener添加到css创建的箭头
- aws-lambda - Serverless.yml 配置将一个 lambda 函数部署到服务而不删除其他函数
- php - 功能测试和与 GitLab 的持续集成
- r - 获取一对行的总和
- amazon-web-services - MalformedPolicyDocumentExceptionnull AWS KMS KEY
- linux - tcsh : 重新映射键绑定
- php - 使用 curl PHP 获取 API
- android - 等待片段被附加
- ios - 管道中的 Azure DevOps Fastlane 实现
- python - 一次对多列进行一次热编码并附加到主数据集?