我想在我的主页上启动注册过程。最后,理想情况下,该过程将遵循以下逻辑:
user = User.new
user.email = ""
user.password = ""
user.profile = Profile.new
user.profile.info = ""
user.profile.save
user.save
我当然会使用嵌套的模型表单。但有没有办法将其分为两部分?在第1部分中,User
将主要输入user
个信息,以及一些profile
信息,第2部分仅包含“个人资料”信息。然后,当完成所有操作后,用户将被重定向到他们的用户配置文件。
如果可以,这种过程的一般思路是什么?其次,我想知道是否有人可以帮助我弄清楚如何实现它。我已经设置了所有嵌套的模型表单,但是我的routes.rb文件/控制器中一定有一些东西搞砸了我的体验。
这是我的routes.rb文件。
get "profiles/show"
get "/profiles/:id" => "profiles#show", :as => "profile"
post "/signup" => "profiles#create", :as => "signup"
get "skip/signup", :to => "users#newskip"
match "skip/profiles/new", :to => "profiles#newskip"
root :to => "users#new"
以下分别是我的UsersController和ProfilesController:
*class UsersController < ApplicationController*
before_filter :authenticate, :only => [:edit, :update]
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
redirect_to signup_path, :notice => 'User successfully added.'
else
render :action => 'new'
end
end
*class ProfilesController < ApplicationController*
before_filter :authenticate, :only => [:edit, :update]
def new
@user.profile = Profile.new
end
def create
@profile = Profile.new(params[:profile])
if @profile.save
redirect_to profile_path(@profile), :notice => 'User successfully added.'
else
render :action => 'new'
end
end
任何人都可以帮我看一眼光吗?我知道Devise是一个解决方案,但我试图在没有它的情况下学习。至少在开始时。这个previous question/answer看起来像是一个潜在的首发。
答案 0 :(得分:0)
Here是关于多步骤表单的Railscast。我认为它应该让你顺利完成你想要完成的任务。
答案 1 :(得分:0)
我是通过在主页上创建user
和profile
来完成此操作,第二步是个人资料#edit,并使用redirect_to个人资料。