开发者

How to fix my nested model form redirect

I have a signup process that goes:

user = User.new
user.email = ...
user.password = ...
user.profile = Profile.new
user.profile.save
user.save

In my app I initiate the signup process on the homepage through an InfoController to handle static pages. Because the form on the homepage mixes the user and profile models, I am using a nested model form. However, when I submit the form I am redirected to the wrong place. Can anyone help me figure out what I did wrong?

Routes.rb file:

match '/login' => "sessions#new", :as => "login"
match '/signup' => 'profiles#new', :as => "signup"
match 'skip/signup', :to => 'info#signupskip'
match 'skip/profiles/new', :to => 'profiles#newskip'

root :to => 'info#home'
root :to => "questions#index"
resources :users
resources :profiles
resources :info
resource :session
resources :session

ProfilesController:

class ProfilesController < ApplicationController
  before_filter :authenticate

  def new
    @profile = Profile.new
  end

  def name
    puts "#{user.profile.first_name} #{user.profile.last_name}"
  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
  ...

UsersController:

class UsersController < ApplicationController
  before_filter :authenticate, :only => [:edit, :update]

  def new
    @user = User.new
  end

  def index
    @user = User.all
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      redirect_to profile_path, :notice => 'User successfully added.'
    else
      render :action => 'new'
    end
  end

And finally, the form with nested models:

  <%= 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[goal]">ex: be President</label>
      开发者_JAVA百科<%= f.text_field :goal, :size=> 13, :id => "user[goal]" %>
    </div>
    <p class="hometext">when I grow up.&nbsp;</p>
    <div id="button">
      <%= submit_tag 'Join', :class => 'button orange' %>
    </div>
  <% end %>


The following route is incorrect:

match '/signup' => 'profiles#new', :as => "signup"

In your form_for, you're specifying that it's for :profile. By default, Rails would POST this to profiles#create. Since you're passing :url => 'signup', the form is actually being POSTed to /signup, which is mapped, via the aforementioned route, to profiles#new. However, the new action simply sets up the form — it's the action you first started on. That being said, the correct route should be:

match '/signup' => 'profiles#create', :as => "signup"

In fact, if you wanted to make it that much better, it should be this:

post '/signup' => 'profiles#create', :as => "signup"

Further, since you're using a named route (by passing as in the route), you should be using :url => signup_path in your form_for.

As a quick aside: I'm not quite sure what your models look like, but I'd probably agree with Kleber here. It seems more intuitive that it should be form_for :user instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜