gpt4 book ai didi

ruby-on-rails - 在 Rails 中与设计用户建立 has_one 关系

转载 作者:行者123 更新时间:2023-12-04 01:23:42 26 4
gpt4 key购买 nike

我是 Rails 的新手,很难通过设计用户创建配置文件

这里是配置文件 Controller :

class ProfilesController < ApplicationController

before_action :set_profile, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!

def index
@profiles = Profile.all
end

def new
@profile = current_user.build_profile
end

def create
@profile = current_user.build_profiles(profile_params)
redirect_to profiles_path
end

def show

end

def edit

end

def update
@profile.update(profile_params)
redirect_to(profiles_path(@profile))
end

def destroy
@profile.destroy
redirect_to profiles_path
end

private
def profile_params
params.require(:profile).permit(:university, :degree)
end

def set_profile
@profile = Profile.find(params[:id])
end

end

当我运行 Rails 服务器时,我可以提交表单,但模型“配置文件”中没有存储任何内容

这是数据应该出现的 index.html.erb:

<h2>profiles</h2>

<% @profiles.each do |profile| %>

<%= link_to profile do %>
<%= profile.university %>
<% end %>
<%= profile.degree %>

<% end %>

user.rb 文件:

has_one :profile

和 profile.rb 文件:

belongs_to :user

似乎没有任何内容被保存到配置文件模型中,也没有任何内容显示在 index.html.erb 上。我还创建了一个迁移以将 user_id 存储在配置文件模型中。

谢谢你的帮助

最佳答案

到目前为止,为用户创建配置文件最佳方法是在用户时构建它对象被创建:

#app/models/user.rb
class User < ActiveRecord::Base
has_one :profile
before_create :build_profile
accepts_nested_attributes_for :profile
end

#app/models/profile.rb
class Profile < ActiveRecord::Base
belongs_to :user
end

每次创建新的 user 时,这都会构建一个空白的 profile。这意味着每个用户将拥有一个个人资料,他们将能够填充和编辑该个人资料。


关于你的问题,有几点:

  1. 不要列出带有索引的“个人资料”,列出用户并提取他们的个人资料数据
  2. 如果管理个人资料,请将其嵌套在关联用户模型下

方法如下:

# config/routes.rb
resources :users, only: :index
resource :profile, only: [:show, :update]

#app/controllers/profiles_controller.rb
class ProfilesController < ApplicationController
def show
end

def update
redirect_to :show if current_user.update profile_params
end

private

def profile_params
params.require(:user).permit(profile_attributes: [:name])
end
end

#app/views/profiles/show.html.erb
<%= form_for current_user, url: profile_path do |f| %>
<%= f.fields_for :profile do |p| %>
<%= p.text_field :name %>
<% end %>
<%= f.submit %>
<% end %>

更新

我上面的帖子正是我们所做的。

它的工作方式非常简单——当一个 User 被创建时(IE 他们已经不厌其烦地填写了他们的详细信息),Rails 后端会自动创建一个 blank 配置文件 对象。

这会做几件事:

  1. Always makes sure you have a Profile for each user (you don't have to go to the bother of making them "create" a profile).

  2. Gives you the ability to validate only inputted data on a created Profile (not having to guess whether it's already been done).

--

如果您得到未定义的方法 build_profile,这意味着您的关联不正确。

所有单数关联都有 build_[association]作为定义的实例方法。我提供的代码为 has_one 关联触发了 build_profile。唯一“未定义”的情况是关联是复数

--

更新

enter image description here

这表明存在路由错误。

考虑到它出现在root,我认为问题是here :

#app/views/layouts/application.html.erb
<%= link_to "Profile", profile_path %>

您没有edit_profile_path——它应该只是profile_path

关于ruby-on-rails - 在 Rails 中与设计用户建立 has_one 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35047132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com