Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with nested form sign-up process
    primarykey
    data
    text
    <p>After researching on SO, I found that I desire a sign-up process that goes:</p> <ol> <li>User fills out form</li> <li>User clicks 'Join'</li> <li>User is created/logged in and redirected to fill out profile</li> <li>Button clicked</li> <li>User directed to profile</li> </ol> <p>If I were to create a User and User's Profile in <code>rails console</code> it would look like this:</p> <pre><code>user = User.new user.email = "" user.password = "" user.profile = Profile.new user.profile.info = "" user.profile.save user.save </code></pre> <p><strong>THE PROBLEM:</strong> I'm using a nested model form on the homepage to create the user and add that user's profile first/last name. What happens now is: The user clicks 'Join', they are redirected to /users, then redirected to /signup. That's where I'm stuck. It gives me the following error:</p> <pre><code>Action Controller: Exception caught NoMethodError in ProfilesController#new undefined method `profile=' for nil:NilClass app/controllers/profiles_controller.rb:5:in `new' </code></pre> <p>I want /signup to be a form to fill in the profile but it's not working.</p> <p>My code below...</p> <p><strong>Users#new.html.erb (homepage form):</strong></p> <pre><code>&lt;%= form_for(@user, :html =&gt; {:multipart =&gt; true, :id =&gt; 'homesign'}) do |f| %&gt; &lt;%= f.hidden_field :id %&gt; &lt;% if @user.errors.any? %&gt; &lt;% end %&gt; &lt;div&gt; &lt;%= f.label :email %&gt; &lt;%= f.text_field :email, :size =&gt; 38 %&gt; &lt;/div&gt; ... &lt;%= f.fields_for :profile do |profile| %&gt; &lt;%= profile.label :first_name %&gt; &lt;%= profile.text_field :first_name, :size =&gt; 18 %&gt; ... &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p><strong>Profiles#new.html.erb (signup form):</strong></p> <pre><code>&lt;%= form_for @profile, :html =&gt; { :multipart =&gt; true } do |f| %&gt; &lt;table id="signupTable"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td class="label"&gt;&lt;%= f.label :gender, "Gender:" %&gt;&lt;/td&gt; &lt;td&gt; &lt;fieldset&gt; &lt;%= select(:gender, :gender_type, [['Female', 1], ['Male', 2], ['Rather not say', 3]], :class =&gt; 'optionText') %&gt; &lt;/fieldset&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="label"&gt;&lt;%= f.label :birthday, "Birthday:" %&gt;&lt;/td&gt; &lt;td&gt; &lt;fieldset&gt; &lt;%= select_month(14, :prompt =&gt; 'Month', :field_name =&gt; 'Month', :id =&gt; 'Date.mon') %&gt; &lt;%= select_day(32, :prompt =&gt; 'Day') %&gt; &lt;%= select_year(0, {:prompt =&gt; "Year", :start_year =&gt; DateTime.now.year, :end_year =&gt; DateTime.now.year - 115}, {:field_name =&gt; 'Year', :id =&gt; 'Date.year'}) %&gt; &lt;/fieldset&gt; &lt;% end %&gt; </code></pre> <p><strong>User.rb:</strong></p> <pre><code>class User &lt; ActiveRecord::Base attr_accessor :password has_one :profile, :dependent =&gt; :destroy accepts_nested_attributes_for :profile validates :email, :uniqueness =&gt; true, :length =&gt; { :within =&gt; 5..50 }, :format =&gt; { :with =&gt; /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i } validates :password, :confirmation =&gt; true, :length =&gt; { :within =&gt; 4..20 }, :presence =&gt; true, :if =&gt; :password_required? end </code></pre> <p><strong>Profile.rb:</strong></p> <pre><code>class Profile &lt; ActiveRecord::Base belongs_to :user accepts_nested_attributes_for :user end </code></pre> <p><strong>UsersController:</strong></p> <pre><code>class UsersController &lt; ApplicationController before_filter :authenticate, :only =&gt; [: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 =&gt; @user), :notice =&gt; 'User successfully added.' else render :action =&gt; 'new' end end end </code></pre> <p><strong>ProfilesController:</strong></p> <pre><code>class ProfilesController &lt; ApplicationController before_filter :authenticate, :only =&gt; [: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 =&gt; 'User successfully added.' else render :action =&gt; 'new' end end def index @profile = current_user.profile end end </code></pre> <p><strong>SessionsController (create only):</strong></p> <pre><code>class SessionsController &lt; ApplicationController def create if user = User.authenticate(params[:email], params[:password]) session[:user_id] = user.id redirect_to user.profile, :notice =&gt; "Logged in successfully" else flash.now[:alert] = "Invalid login/password. Try again!" render :action =&gt; 'new' end end end </code></pre> <p><strong>Routes.rb:</strong></p> <pre><code>match "/signup" =&gt; "profiles#new", :as =&gt; "signup" post "/profiles/new" =&gt; "profiles#create" match "skip/signup", :to =&gt; "info#signupskip" match "skip/profiles/new", :to =&gt; "profiles#newskip" get "/profiles/:id" =&gt; "profiles#show", :as =&gt; "profile" get "profiles/new" root :to =&gt; "users#new" </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload