【发布时间】:2012-12-29 17:40:08
【问题描述】:
我有 2 个模型 - 用户模型和配置文件模型。我已将关系设置如下:
class User
has_one :profile
end
class Profile
belongs_to :user
end
我有一个具有 4 个操作的配置文件控制器 - 新创建编辑和更新。一旦用户注册或登录,他就会被重定向到 Profiles 控制器中的 New 操作。从这里我如何为该用户创建个人资料?具体来说,我应该在我的新动作和创建动作中拥有什么。现在新操作的路径只是profiles/new,它没有捕获用户参数。我正在尝试这样做,但失败了。
配置文件控制器
def new
@user = User.find(params[:id])
@profile = @user.build_profile
end
def create
@profile = current_user.build_profile(params[:profile])
if @profile.save
redirect_to current_user
else
render new
end
end
【问题讨论】:
-
下降情况如何?您的路线如何?
-
它说找不到没有 id 的用户。 profile#new 的路由本质上是 localhost:3000/profiles/new。这里没有传递用户 ID。不知道我是否应该改变路线。我正在使用设计进行身份验证,因此它创建了一个用户并被重定向到配置文件#new。从这里开始我不知道该怎么办了。
-
我想我想弄清楚是否需要在新操作中捕获用户 ID。我可以做这样的事情吗? def new @profile = current_user.build_profile
标签: ruby-on-rails ruby-on-rails-3