Ruby on rails 在Rails中关联两个模型(用户和配置文件)

Ruby on rails 在Rails中关联两个模型(用户和配置文件),ruby-on-rails,activerecord,modeling,Ruby On Rails,Activerecord,Modeling,我不熟悉Rails。我正在构建一个包含用户模型和个人资料模型的应用程序 我希望将这些模型关联为: -用户创建帐户后,会自动将其发送到“创建配置文件”页面,并且他创建的配置文件仅连接到该特定用户 -只有拥有配置文件的用户才能对其进行编辑 我使用漂亮的生成器生成了用户模型。当用户点击submit创建帐户时,我将他重定向到“new profile”视图以创建一个配置文件。我通过在用户控制器中编辑重定向路径来实现这一点。用户控制器如下所示: def create @user = User.new(p

我不熟悉Rails。我正在构建一个包含用户模型和个人资料模型的应用程序

我希望将这些模型关联为:
-用户创建帐户后,会自动将其发送到“创建配置文件”页面,并且他创建的配置文件仅连接到该特定用户
-只有拥有配置文件的用户才能对其进行编辑

我使用漂亮的生成器生成了用户模型。当用户点击submit创建帐户时,我将他重定向到“new profile”视图以创建一个配置文件。我通过在用户控制器中编辑重定向路径来实现这一点。用户控制器如下所示:

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
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
这是可行的,但问题是应用程序似乎没有意识到该配置文件已连接到该特定用户。我能够创建配置文件,但配置文件和用户之间似乎没有关系

我的个人资料模型列表:属于用户
我的用户模型列表:has\u one:profile

My routes.rb文件列出了以下内容:
map.resources:users,:has_one=>:profile
地图资源:简介

我在profiles表中有一个user_id外键。我的模式如下所示:

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
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入门应用程序中的评论与帖子的连接方式相同。以下是我的配置文件控制器的相关部分。如果有帮助,我可以提供全部内容:

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
结束

在对profiles controller进行这些更新后,现在当我在帐户创建屏幕上提交时,我会被重定向到一个错误页面,该页面显示:

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


这似乎是一个非常直接的Rails用例,但我不确定哪些部分是错误的。提前感谢您的帮助

出于可用性考虑,个人资料应该是用户模型的一部分,或者你应该一次性创建你的帐户,至少是个人资料的基础。作为一个用户,我注册了,然后不得不填写另一张表格,我想我会很恼火


这样做的好处是,无论哪种方式,你都可以用一个表单来完成。我会查看上的railscasts,如果你能处理非常温和的财务支出,那么上的务实程序员系列将是一个完全的赢家。

创建用户时,客户端将重定向到
ProfileController
中的
操作,而无需
id
。您需要在参数中明确地传递用户的
id
。将引用传递给整个对象,而不仅仅是id,这是一种习惯用法

# app/controllers/users_controller.rb

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

请注意,我使用的是
new\u user\u profile\u path
,而不是
new\u profile\u path
。前者是您在
routes.rb
中定义的嵌套资源。您应该删除
map.resources:profiles
,因为没有用户配置文件就无法存在。

因为用户id不存在或未在配置文件数据库中创建。创建用户时

谢谢,泰特。我听从了你的建议,然后遇到了其他一些错误,我一直在努力自己解决这些错误。我现在又卡住了。当我在创建用户时单击submit时,它会将我指向localhost:3000/users/5/profile/new。但我得到的跟踪页面上写着:NoMethodError in ProfilesController#new当你没有预料到它时,你有一个nil对象!错误发生在评估nil时。生成跟踪说它在配置文件控制器-新配置文件方法中中断。def new@user=user.find(params[:user_id])@profile=@user.profile.build end Ideas?这是一条糟糕的路线。你应该同时创造这两个,除非你有一个很好的理由。我参考我的答案。
object.association.build
用于关联集合。单一关联应使用对象。构建关联,其中关联是关联模型的名称。如果您有疑问,您应该启动rails控制台并尝试一下它是如何工作的,正如@railsninja所指出的,您真的应该考虑完成两个表单的用户体验。尽管如此,它还是嵌套关联的一个非常有效的解决方案。