【发布时间】:2011-08-28 02:46:15
【问题描述】:
我是编程和 Rails 的新手,我正在为我正在开发的应用程序进行注册流程。我有user 和profile 模型。作为注册过程的一部分,我混合了user 和profile 的字段,所以我使用的是嵌套表单。
我试图让嵌套表单在处理我的静态页面的控制器中工作,因为我想在主页上启动注册过程。
可以通过两种方式启动注册过程:直接从主页或在主页上跳过并在 /signup/skip 视图中启动。
我的 InfoController(其中存在带有表单的 home.html.erb 文件):
def home
if logged_in?
redirect_to current_user.profile
end
end
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 %>
个人资料模型:
class Profile < ActiveRecord::Base
attr_accessible :first_name, :last_name ...
belongs_to :user
accepts_nested_attributes_for :user
end
用户模型:
class User < ActiveRecord::Base
attr_accessor :password, :email
accepts_nested_attributes_for :profile
has_one :profile, :dependent => :destroy
end
由于某种原因,当我在表单中输入数据时,我被重定向到 /login,它在 Routes.rb 中为 Sessions#new。所以这里是 SessionsController:
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
最后但并非最不重要的是 ProfilesController:
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)
更新:Routes 文件(至少相关部分)如下:
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
谁能帮我解决这个问题?任何有关如何使其工作并根据最佳实践进行操作的建议将不胜感激。
【问题讨论】:
标签: ruby-on-rails-3 model-view-controller nested-forms