在 rails 中与设计用户建立 has_one 关系

Gur*_*ngh 2 ruby-on-rails devise ruby-on-rails-4

我是 Rails 的新手,并且很难由设计用户创建配置文件

这是 ProfileController:

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
Run Code Online (Sandbox Code Playgroud)

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

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

<h2>profiles</h2>

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

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

<% end %>
Run Code Online (Sandbox Code Playgroud)

user.rb 文件:

has_one :profile
Run Code Online (Sandbox Code Playgroud)

和 profile.rb 文件:

belongs_to :user
Run Code Online (Sandbox Code Playgroud)

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

感谢您的帮助

Ric*_*eck 6

到目前为止,为 a创建 a的最佳方法是在创建对象时构建它:profileuserUser

#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
Run Code Online (Sandbox Code Playgroud)

profile每次user创建一个新的时,这将建立一个空白。这意味着每个用户将曾经有一个配置文件,他们就可以填充和编辑。


关于你的问题,有几点:

  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 %>
Run Code Online (Sandbox Code Playgroud)

更新

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

它的工作方式非常简单——当 aUser被创建时(IE 他们已经麻烦地填写了他们的详细信息),Rails 后端会自动创建一个空白 Profile对象。

这有几件事情:

  1. 始终确保Profile每个用户都有一个(您不必费心让他们“创建”个人资料)。

  2. 使您能够验证一个只创建输入数据的能力Profile不会有猜测是否它已经完成)。

——

如果您收到undefined methodbuild_profile,则意味着您的关联不正确。

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

——

更新

在此处输入图片说明

这表明路由错误。

考虑到它出现在root,我认为问题出在这里

#app/views/layouts/application.html.erb
<%= link_to "Profile", profile_path %>
Run Code Online (Sandbox Code Playgroud)

你没有edit_profile_path- 它应该只是profile_path