【发布时间】:2009-08-19 09:50:55
【问题描述】:
我是 Rails 新手。我正在构建一个具有用户模型和配置文件模型的应用程序。
我想这样关联这些模型:
- 用户创建帐户后,会自动转到“创建个人资料”页面,并且他创建的个人资料仅与该特定用户相关联。
- 只有拥有个人资料的用户才能对其进行编辑。
我使用 nifty_generators 生成了用户模型。当用户点击提交以创建帐户时,我将他重定向到“新配置文件”视图以创建配置文件。我通过在用户控制器中编辑重定向路径来做到这一点。用户控制器如下所示:
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
这可以正常工作,但问题是该应用似乎无法识别配置文件已与该特定用户相关联。我能够创建个人资料,但个人资料与用户之间似乎没有关系。
我的个人资料模型列表:belongs_to :user
我的用户模型列表:有 _one :profile
我的 routes.rb 文件列出了以下内容:
map.resources :users, :has_one => :profile
map.resources :profiles
我在配置文件表中有一个 user_id 外键。我的架构如下所示:
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
结束
在对配置文件控制器进行这些更新之后,现在当我在帐户创建屏幕上提交时,我被重定向到一个错误页面,上面写着:
ActiveRecord::RecordNotFound in ProfilesController#new
找不到没有 ID 的用户
这一切似乎都是一个非常简单的 Rails 用例,但我不确定哪些部分是错误的。提前感谢您的帮助!
【问题讨论】:
标签: ruby-on-rails activerecord modeling