Ruby on rails 3 嵌套表单注册过程存在问题

Ruby on rails 3 嵌套表单注册过程存在问题,ruby-on-rails-3,login,nested-forms,Ruby On Rails 3,Login,Nested Forms,在对SO进行研究后,我发现我希望注册流程如下: 用户填写表格 用户单击“加入” 用户已创建/登录并重定向以填写配置文件 点击按钮 用户定向到配置文件 如果我要在rails控制台中创建一个用户和用户配置文件,它将如下所示: user = User.new user.email = "" user.password = "" user.profile = Profile.new user.profile.info = "" user.profile.save user.save 问题是:我在主页上

在对SO进行研究后,我发现我希望注册流程如下:

用户填写表格 用户单击“加入” 用户已创建/登录并重定向以填写配置文件 点击按钮 用户定向到配置文件 如果我要在rails控制台中创建一个用户和用户配置文件,它将如下所示:

user = User.new
user.email = ""
user.password = ""
user.profile = Profile.new
user.profile.info = ""
user.profile.save
user.save
问题是:我在主页上使用嵌套模型表单来创建用户并添加该用户的个人资料名/姓。现在发生的情况是:用户单击“加入”,他们被重定向到/users,然后重定向到/signup。这就是我被困的地方。它给了我以下错误:

Action Controller: Exception caught
NoMethodError in ProfilesController#new
undefined method `profile=' for nil:NilClass
app/controllers/profiles_controller.rb:5:in `new'
我想/注册成为填写个人资料的表格,但它不起作用

下面是我的代码

Usersnew.html.erb主页表单:


是一个表单_用于内部还是一个表单_用于拼写错误?对不起,应该是fields_,我修复了这个问题,并在问题下面添加了一些上下文。用户将被创建/保存,但由于某些原因,我不会呈现新的/注册表单。
<%= form_for(@user, :html => {:multipart => true, :id => 'homesign'}) do |f| %>
  <%= f.hidden_field :id %>
  <% if @user.errors.any? %>
  <% end %>
  <div>
    <%= f.label :email %>
    <%= f.text_field :email, :size => 38 %>
  </div>
  ...
  <%= f.fields_for :profile do |profile| %>
    <%= profile.label :first_name %>
    <%= profile.text_field :first_name, :size => 18 %>
    ...
  <% end %>
<% end %>
<%= form_for @profile, :html => { :multipart => true } do |f| %>
  <table id="signupTable">
    <tbody>
      <tr>
        <td class="label"><%= f.label :gender, "Gender:" %></td>
        <td>
        <fieldset>
          <%= select(:gender, :gender_type, [['Female', 1], ['Male', 2], ['Rather not say', 3]], :class => 'optionText') %>
        </fieldset>
        </td>
      </tr>
      <tr>
        <td class="label"><%= f.label :birthday, "Birthday:" %></td>
        <td>
        <fieldset>
          <%= select_month(14, :prompt => 'Month', :field_name => 'Month', :id => 'Date.mon') %>
          <%= select_day(32, :prompt => 'Day') %>
          <%= select_year(0, {:prompt => "Year", :start_year => DateTime.now.year, :end_year => DateTime.now.year - 115}, {:field_name => 'Year', :id => 'Date.year'}) %>
        </fieldset>
<% end %>
class User < ActiveRecord::Base
  attr_accessor :password
  has_one :profile, :dependent => :destroy
  accepts_nested_attributes_for :profile

  validates :email, :uniqueness => true, 
                :length => { :within => 5..50 }, 
                :format => { :with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i }
  validates :password, :confirmation => true,
                   :length => { :within => 4..20 },
                   :presence => true,
                   :if => :password_required?
end
class Profile < ActiveRecord::Base
  belongs_to :user
  accepts_nested_attributes_for :user
end
class UsersController < ApplicationController
  before_filter :authenticate, :only => [:edit, :update]

  def new
    @user = User.new
    @user.profile = Profile.new
    if logged_in?
      redirect_to current_user.profile
    end
  end

  def index
    @user = User.all
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      session[:user_id] = user.id
      redirect_to new_user_profile_path(:user_id => @user), :notice => 'User successfully added.'
    else
      render :action => 'new'
    end
  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

  def index
    @profile = current_user.profile
  end
end
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
end
match "/signup" => "profiles#new", :as => "signup"
post "/profiles/new" => "profiles#create"
match "skip/signup", :to => "info#signupskip"
match "skip/profiles/new", :to => "profiles#newskip"
get "/profiles/:id" => "profiles#show", :as => "profile"
get "profiles/new"
root :to => "users#new"