Search code examples
ruby-on-railsformsparametersauto-populate

Autopopulating a sign up form with params from pre-registration form not working


so I'm trying to build a sign-up process just like Twitter has. Meaning, there's a pre-sign up form which leads to full form with autopopulated fields from the first form. I'm trying to create this using :value => @name in the final form, but it's not working and I just can't figure out why.

Here's my code, simplified:

First form, controller: public_controller, action: index (root_path)

<%= form_tag({:controller => "users", :action => "register"})%>
  <input name="user[name]" type="text" />
  <input name="user[email]" type="text"/>
  <input name="user[password]" type="password"/>
  <input name="commit" type="submit" value="Sign Up!" />

Second form, controller: users_controller, action: register (/signup)

 <%= form_for(:user, :url => { :controller => 'users', :action => 'create' }) do |f| %>
    <%= f.text_field :name,  :value => @name %>
    <%= f.text_field :email, :value => @email %>
    <%= f.password_field :password, :value => @password %>
    <input name="commit" type="submit"
           value="Sign Up!" />
    <% end %>

Users_controller

def register
  @name     = params[:name]
  @email    = params[:email]
  @password = params[:password]
end

The params are passing to the final form page (/signup)

{"utf8"=>"✓", "authenticity_token"=>"R3wVNHvqpDBi1Lg69lwKu4NeQGjA12oV/Aytw38OeHc=", "user"=>{"name"=>"Stackoverflow", "email"=>"[email protected]", "password"=>"stacker", "password_confirmation"=>"stacker"}, "commit"=>"Sign Up!", "search"=>"", "controller"=>"users", "action"=>"register"}

But they are not autopopulating the final form's fields.

I've also tried using :input_html => { :value => @name } with params[:name], but it has no effect, and the input's value simply turns to value="nil".

I just can't think of anything else to try from here. Any ideas? Thanks in advance! Any help is greatly appreciated!


Solution

  • tiny oversight on the controller? shouldn't it be

    def register
      @name     = params[:user][:name]
      @email    = params[:user][:email]
      @password = params[:user][:password]
    end
    

    you kinda figured it out in the end though.