【发布时间】:2023-03-31 16:18:01
【问题描述】:
我正在构建一个应用程序,其中用户 has_one :profile 和个人资料 belongs_to :user 但我在摸索为什么它在寻找 user_id 而不是 profile_id 当我在个人资料显示页面。我得到的错误是
Couldn't find User with 'id'=15
def show
@user = User.find(params[:id])
@profile = @user.profile
end
我有一些与主题模型相关的多态关系。个人资料可以有许多主题,基本上在主题显示页面上,我想链接创建主题的用户,然后单击以转到该用户的个人资料页面。对不起,如果这很难理解,请在下面找到代码。
users_controller.rb
class UsersController < ApplicationController
include UserCreateHelper
skip_before_action :require_login, only: [:new, :create], raise: false
def show
@user = User.find(params[:id])
@profile = @user.profile
end
def new
@user = User.new
end
def create
@user = sign_up(user_params)
if @user.valid?
sign_in(@user)
create_user_profile(current_user)
redirect_to profile_path(current_user)
else
render :new
end
end
private
def user_params
params.require(:user).permit(:email, :password)
end
end
在这里,我在用户注册时创建个人资料实例并重定向到他们的新个人资料页面以填写他们的详细信息。这一步工作正常。
profiles_controller.rb
class ProfilesController < ApplicationController
def show
@user = User.find(params[:id])
@profile = @user.profile
end
def update
if @profile = current_user.profile
@profile.update(profile_params)
redirect_to profile_path(current_user)
end
end
private
def profile_params
params.require(:profile).permit(:bio, :user_id)
end
end
上面的配置文件显示方法是造成问题的方法。
views/profiles/show.html.erb
<div>
<p>Hi <%= @user.email %></p>
<% if @profile.bio %>
<hr>
<p><%= @profile.bio %></p>
<% else %>
<p>Fill out the rest of your profile!</p>
<%= render partial: 'profiles/form', locals: {type: @profile} %>
<% end %>
</div>
<hr>
<%= render partial: 'subjects/subject', locals: {subjectable: @profile} %>
<%= render partial: 'subjects/form', locals: {subjectable: @profile} %>
<%= render partial: 'locations/location', locals: {locationable: @profile} %>
<%= render partial: 'locations/form', locals: {locationable: @profile} %>
如果在配置文件控制器中显示我删除了@user = User.find(params[:id])(我也尝试将其更改为 params[:user_id]),则上述多态关联工作正常,但无济于事。正如我所说,删除此行允许我添加主题,注册为另一个用户并再次添加主题,主题索引/all_subjects 页面上的主题也很好,但是当我尝试链接创建的用户时转到他们的个人资料页面会失败。
views/subjects/all_subjects.html.erb
<% @all_subjects.each do |subject| %>
<p><%= subject.title %> | Created by <%= link_to subject.user.email, profile_path(subject.user) %>, <%= time_ago_in_words(subject.created_at) + ' ago' %></p>
<% end %>
我知道这很啰嗦,但我们将不胜感激任何帮助。如果有帮助,我很乐意链接 github 存储库。
schema.rb
ActiveRecord::Schema.define(version: 20160612114642) do
create_table "locations", force: :cascade do |t|
t.integer "user_id"
t.string "locationable_type"
t.integer "locationable_id"
t.string "suburb"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["locationable_type", "locationable_id"], name: "index_locations_on_locationable_type_and_locationable_id"
t.index ["user_id"], name: "index_locations_on_user_id"
end
create_table "profiles", force: :cascade do |t|
t.integer "user_id"
t.text "bio"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_profiles_on_user_id"
end
create_table "subjects", force: :cascade do |t|
t.integer "user_id"
t.string "title"
t.text "description"
t.string "subjectable_type"
t.integer "subjectable_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["subjectable_type", "subjectable_id"], name: "index_subjects_on_subjectable_type_and_subjectable_id"
t.index ["user_id"], name: "index_subjects_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "email", null: false
t.string "password_digest", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
end
end
【问题讨论】:
标签: sql ruby-on-rails ruby activerecord ruby-on-rails-5