Ruby on rails 3 如何使嵌套表单在不同的控制器中工作
我是编程和Rails新手,我正在为我正在开发的应用程序进行注册过程。我有Ruby on rails 3 如何使嵌套表单在不同的控制器中工作,ruby-on-rails-3,model-view-controller,nested-forms,Ruby On Rails 3,Model View Controller,Nested Forms,我是编程和Rails新手,我正在为我正在开发的应用程序进行注册过程。我有user和profile两种型号。作为注册过程的一部分,我混合了user和profile的字段,因此我使用嵌套表单 我试图让嵌套表单在处理静态页面的控制器中工作,因为我想在主页上启动注册过程 注册过程可以通过两种方式启动:直接从主页启动或在主页上跳过,并在/signup/skip视图中启动 My InfoController(其中包含表单的home.html.erb文件): home.html.erb中的嵌套表单: <
user
和profile
两种型号。作为注册过程的一部分,我混合了user
和profile
的字段,因此我使用嵌套表单
我试图让嵌套表单在处理静态页面的控制器中工作,因为我想在主页上启动注册过程
注册过程可以通过两种方式启动:直接从主页启动或在主页上跳过,并在/signup/skip视图中启动
My InfoController(其中包含表单的home.html.erb
文件):
home.html.erb
中的嵌套表单:
<%= form_for(:profile, :url => 'signup', :html => {:id => 'homepage'}) do |f| %>
<p class="hometext">I'm </p>
<div>
<%= f.label :first_name, :placeholder => 'First name' %>
<%= f.text_field :first_name, :size=> 8, :id => "profile[first_name]" %>
</div>
<div>
<label for="profile[last_name]">Last name</label>
<%= f.text_field :last_name, :size=> 8, :id => "profile[last_name]" %>
</div>
<%= f.fields_for :user do |f| %>
<p class="hometext">. My email is
<div>
<label for="user[email]">Email</label>
<%= f.text_field :email, :size=> 13, :id => "user[email]" %>
</div>
<% end %>
<p class="hometext">. I want to </p>
<div>
<label for="user[stat]">put stat here</label>
<%= f.text_field :stat, :size=> 13, :id => "user[stat]" %>
</div>
<p class="hometext">when I grow up. </p>
<div id="button">
<%= submit_tag 'Join', :class => 'button orange' %>
</div>
<% end %>
最后但并非最不重要的是ProfilesController:
class SessionsController < ApplicationController
def create
if user = User.authenticate(params[:email], params[:password])
session[:user_id] = user.id
redirect_to user.profile, :notice => "Logged in successfully"
else
flash.now[:alert] = "Invalid login/password. Try again!"
render :action => 'new'
end
end
class ProfilesController < ApplicationController
def new
@profile = Profile.new
end
def create
@profile = Profile.new(params[:profile])
if @profile.save
redirect_to profile_path, :notice => 'User successfully added.'
else
render :action => 'new'
end
end
更新:路由文件(至少相关部分)如下:
match '/login' => "sessions#new", :as => "login"
match '/signup', :to => 'profiles#new'
match 'skip/signup', :to => 'info#signupskip'
match 'skip/profiles/new', :to => 'profiles#newskip'
root :to => 'info#home'
resources :users
resources :profiles
resources :info
有人能帮我弄清楚吗?如果您能就如何使其正常工作并根据最佳实践提出任何建议,我们将不胜感激。您能发布您的routes.rb吗 还有,你有没有看过这个设计?看来你可能想重新发明轮子 去看看
我将Routes.rb的相关部分放在问题中。我还没有签出设计,但我会的!谢谢。Desive看起来不错,但我很好奇没有它怎么工作。如有任何见解,将不胜感激!
class SessionsController < ApplicationController
def create
if user = User.authenticate(params[:email], params[:password])
session[:user_id] = user.id
redirect_to user.profile, :notice => "Logged in successfully"
else
flash.now[:alert] = "Invalid login/password. Try again!"
render :action => 'new'
end
end
class ProfilesController < ApplicationController
def new
@profile = Profile.new
end
def create
@profile = Profile.new(params[:profile])
if @profile.save
redirect_to profile_path, :notice => 'User successfully added.'
else
render :action => 'new'
end
end
Started POST "/signup" for 127.0.0.1 at Sat Aug 27 10:36:50 -0400 2011
DEPRECATION WARNING: You are using the old router DSL which will be removed in Rails 3.1. Please check how to update your routes file at: http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/. (called from /Users/me/Desktop/app/config/routes.rb:1)
Processing by ProfilesController#new as HTML
Parameters: {"commit"=>"Join", "profile"=>{"last_name"=>"...", "user"=> {"test"=>"...", "email"=>"..."}, "first_name"=>"..."}, "authenticity_token"=>"u82Jom5PV+5BeTLZ5qQENxQiY1lcyFiXR4aNC7NR+5g=", "utf8"=>"\342\234\223"}
Redirected to http://localhost:3000/login
Completed 302 Found in 35ms
Started GET "/login" for 127.0.0.1 at Sat Aug 27 10:36:51 -0400 2011
Processing by SessionsController#new as HTML
Rendered layouts/_header_out.html.erb (0.5ms)
Rendered layouts/_footer.html.erb (2.2ms)
Rendered sessions/new.html.erb within layouts/application (32.1ms)
Completed 200 OK in 40ms (Views: 39.0ms | ActiveRecord: 0.0ms)
match '/login' => "sessions#new", :as => "login"
match '/signup', :to => 'profiles#new'
match 'skip/signup', :to => 'info#signupskip'
match 'skip/profiles/new', :to => 'profiles#newskip'
root :to => 'info#home'
resources :users
resources :profiles
resources :info