【问题标题】:How to make nested forms work in different controller如何使嵌套表单在不同的控制器中工作
【发布时间】:2011-08-28 02:46:15
【问题描述】:

我是编程和 Rails 的新手,我正在为我正在开发的应用程序进行注册流程。我有userprofile 模型。作为注册过程的一部分,我混合了userprofile 的字段,所以我使用的是嵌套表单。

我试图让嵌套表单在处理我的静态页面的控制器中工作,因为我想在主页上启动注册过程。

可以通过两种方式启动注册过程:直接从主页或在主页上跳过并在 /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&nbsp;</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">.&nbsp;My&nbsp;email&nbsp;is&nbsp;
    <div>
      <label for="user[email]">Email</label>
      <%= f.text_field :email, :size=> 13, :id => "user[email]" %>
    </div>
  <% end %>
  <p class="hometext">.&nbsp;I want to&nbsp;</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.&nbsp;</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


    【解决方案1】:

    你能发布你的 routes.rb 吗?

    另外,您是否为此查看过 Devise?看来您可能正在尝试重新发明轮子。

    看看吧。。

    http://railscasts.com/episodes/209-introducing-devise

    【讨论】:

    • 我把Routes.rb的相关部分放在了问题里。我还没有检查过设计,但我会的!谢谢。
    • Devise 看起来不错,但我很好奇如何在没有它的情况下工作。任何见解将不胜感激!
    猜你喜欢
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多