在 Rails 中关联两个模型(用户和配置文件) [英] Associating Two Models in Rails (user and profile)

查看:28
本文介绍了在 Rails 中关联两个模型(用户和配置文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rails 的新手.我正在构建一个具有用户模型和配置文件模型的应用程序.

I'm new to Rails. I'm building an app that has a user model and a profile model.

我想将这些模型关联起来:
- 用户创建帐户后,他会自动进入创建个人资料"页面,他创建的个人资料仅与该特定用户相关联.
- 只有拥有该配置文件的用户才能对其进行编辑.

I want to associate these models such that:
- After the user creates an account, he is automatically sent to the "create profile" page, and the profile he creates is connected to only that particular user.
- Only the user who owns the profile can edit it.

我使用 nifty_generators 生成了用户模型.当用户点击提交创建帐户时,我将他重定向到新配置文件"视图以创建配置文件.我通过编辑用户控制器中的重定向路径来做到这一点.用户控制器看起来像这样:

I generated the user model using nifty_generators. When the user hits submit for the account creation, I redirect him to the "new profile" view to create a profile. I did this by editing the redirect path in the user controller. The user controller looks like this:

def create
  @user = User.new(params[:user])
  if @user.save
    session[:user_id] = @user.id
    flash[:notice] = "Thank you for signing up! You are now logged in."
    redirect_to new_profile_path
  else
    render :action => 'new'
  end
end

这是有效的,但问题是该应用似乎没有识别出该个人资料与该特定用户相关联.我能够创建个人资料,但个人资料和用户之间似乎没有关系.

This was working, but the problem was that the app didn't seem to recognize that the profile was connected to that particular user. I was able to create profiles, but there didn't seem to be a relationship between the profile and the user.

我的个人资料模型列表:belongs_to :user
我的用户模型列表:有 _one :profile

My Profile model lists: belongs_to :user
My User model lists: has _one :profile

我的 routes.rb 文件列出了以下内容:
map.resources :users, :has_one => :profile
map.resources :profiles

My routes.rb file lists the following:
map.resources :users, :has_one => :profile
map.resources :profiles

我在配置文件表中有一个 user_id 外键.我的架构如下所示:

I have a user_id foreign key in the profiles table. My schema looks like this:

create_table "profiles", :force => true do |t|
  t.integer  "user_id"
  t.string   "name"
  t.string   "address1"
  t.string   "address2"
  t.string   "city"
  t.string   "state"
  t.string   "zip"
  t.string   "phone"
  t.string   "email"
  t.string   "website"
  t.text     "description"
  t.string   "category"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "users", :force => true do |t|
  t.string   "username"
  t.string   "email"
  t.string   "password_hash"
  t.string   "password_salt"
  t.datetime "created_at"
  t.datetime "updated_at"
end

为了尝试将配置文件连接到用户,我使用以下内容更新了 profiles_controller.rb 文件,这基本上是从 Rails 入门指南中推断出来的.我的想法是,在我的应用程序中,配置文件连接到用户的方式与 Rails Getting Started 应用程序中的评论连接到帖子的方式相同.这是我的配置文件控制器的相关部分.如果有帮助,我可以提供全部内容:

To try to connect the profile to the user, I updated the profiles_controller.rb file with the following, which I basically extrapolated from the Rails Getting Started Guide. My thinking is that in my app, profiles connect to users in the same way that in the Rails Getting Started app, comments connect to posts. Here's the relevant parts of my profiles controller. I can provide the whole thing if it will help:

def new
  @user = User.find(params[:user_id])
  @profile = @user.profile.build
end

def create
  @user = User.find(params[:user_id])
  @profile = @user.profile.build(params[:profile])
  if @profile.save
    flash[:notice] = 'Profile was successfully created.'
    redirect_to(@profile)
  else
    flash[:notice] = 'Error.  Something went wrong.'
    render :action => "new"
  end

结束

在对配置文件控制器进行这些更新后,现在当我在帐户创建屏幕上提交时,我被重定向到一个错误页面,上面写着:

After making these updates to the profiles controller, now when I submit on the account creation screen, I'm redirected to an error page that says:

ActiveRecord::RecordNotFound in ProfilesController#new
找不到没有 ID 的用户

ActiveRecord::RecordNotFound in ProfilesController#new
Couldn't find User without an ID

这一切似乎都是一个非常直接的 Rails 用例,但我不确定哪些部分是错误的.预先感谢您的帮助!

This all seems like a pretty straight-forward Rails use case, but I'm not sure which pieces are wrong. Thanks in advance for your help!

推荐答案

当一个用户被创建时,客户端被重定向到 ProfileController 中的 new 动作而没有id.您需要在参数中显式传递用户的id.将引用传递给整个对象是一种习惯用法,而不仅仅是 id.

When a user is created, the client is redirected to the new action in the ProfileController without an id. You need to explictly pass the user's id in the parameters. It's an idiom to pass the reference to the entire object, not just the id.

# app/controllers/users_controller.rb

def create
  # ...
  redirect_to new_user_profile_path(:user_id => @user)
  # ...
end

请注意,我使用的是 new_user_profile_path 而不是 new_profile_path.前者是您在 routes.rb 中定义的嵌套资源.您应该删除 map.resources :profiles 因为没有用户就不能存在配置文件.

Notice that I'm using new_user_profile_path and not new_profile_path. The former is the nested resource that you defined in routes.rb. You should delete map.resources :profiles because a profile cannot exist without a user.

这篇关于在 Rails 中关联两个模型(用户和配置文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆