0

Im new to the rails platform...i have an issue of saving user sign up information on clicking register in my form....i get no error but on checking the rails console all the user information is equated to nil...cant figure out why. my user steps controller code

class UserStepsController < ApplicationController
include Wicked::Wizard
 steps :finishing_step

def show
 @user = current_user
  render_wizard
end

def update
  @user = current_user
  if @user.save
   redirect_to root_path
  else
    render_wizard
  end
end

def user_params
  params.require(:user).permit(:first_name, :middle_name, :last_name, :address_first_line, :address_second_line, :city, :nationality)
end

private

def redirect_to_finish_wizard
  redirect_to root_path, notice: "Thanks for signing up."
end
end

below is the code for the users controller

class UsersController < ApplicationController
def create
@user = User.new(params[:user])
      if @user.save
        redirect_to root_path
      else
        render_wizard
      end
    end
  end

here below is the code for my simple form with user sign up information

<%= form_for User.new, url: wizard_path do |f| %>
<div><%= f.label :first_name, "First Name" %><br />
    <%= f.text_field :first_name %></div>
  <div><%= f.label :middle_name, "Middle Name" %><br />
    <%= f.text_field :middle_name %></div>
  <div><%= f.label :last_name, "Last Name" %><br />
    <%= f.text_field :last_name %></div>
  <div><%= f.label :phone_number, "Phone Number" %><br />
    <%= f.text_field :phone_number %></div>
  <div><%= f.label :date_of_birth, "Date of Birth" %><br />
    <%= f.date_select :date_of_birth, start_year: 1900 %></div>
  <div><%= f.label :address_first_line, "Address (first line)" %><br />
    <%= f.text_field :address_first_line %></div>
  <div><%= f.label :address_second_line, "Address (second line)" %><br />
    <%= f.text_field :address_second_line %></div>
  <div><%= f.label :city, "City" %><br />
    <%= f.text_field :city %></div>
  <div><%= f.label :nationality, "Nationality" %><br />
    <%= country_select(:user, :nationality, {selected: "UG"}) %></div>
    <div>
    <%= f.label :avatar %>
    <%= f.file_field :avatar %>
  </div>
  <div>
    <%= f.label :terms_of_service, "Agree to Terms of Service" %> <br>
    <%= f.check_box :terms_of_service %>
  </div>
  <%= f.submit "Register", class: "btn btn-primary" %>
  <% end %>

for any help thanks in advance

  • your basic setup is either wrong or I don't understand the significance of users_steps_controller. plus @user = User.new(params[:user]) is such a bad idea, look into strong_params in rails – argentum47 Apr 01 '15 at 08:05

1 Answers1

0

I think there is some mistakes in your conception. The wizard operates AFTER the sign_up of your user. So basically, you need to create your user first without the wizard.

  1. Creation of your User

If you're using Devise with your model User, you already have a User Controller somewhere. Actually, your class UsersController is useless if you are using Devise.

So if you want to redirect to your wizard step after the sign up, then you need to override the Devise registration controller (doc: Redirect to specific page). To do so, create a new controller like this:

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
    user_steps_path(:finishing_step) #add proper route
  end
end

I don't have the content of your config/route.rb file but you need to have something like this :

resources :user_steps
  1. Adding steps

Wicked wizard works like this: - One controller with 2 actions (show and update) - N views for N steps (1 step = 1 view, 42 steps = 42 views)

You can access the name of your current step in the actions show and update using step.

render_wizard render the view of the current step render_wizard @user render the next step view (or the current step view if there is any error)

  1. Using strong parameters

In your controller, you never use user_params. So in your update action, you never the your @user with the parameters from your form. current_user doesn't call user_params for you.

Also, there is a big difference between save and update_attributes and I think you want to use the second in your update action.

It's better to place user_params in the private section also.

  1. Final form of your controller

Your controller should look like that I guess:

  class UserStepsController < ApplicationController
    include Wicked::Wizard

    steps :finishing_step

    def show
      @user = current_user
      render_wizard
    end

    def update
      @user = current_user
      if @user.update_attributes(user_params)
        # because you only have one step, you don't need render_wizard @user
        redirect_to_finish_wizard
      else
        render_wizard
      end
    end

    private

    def user_params
      params.require(:user).permit(:first_name, :middle_name, :last_name, :address_first_line, :address_second_line, :city, :nationality)
    end

    def redirect_to_finish_wizard
      redirect_to root_path, notice: "Thanks for signing up."
    end
  end
  1. Read the doc

I think you miss some tips in the wicked doc. Maybe you should re-read it. Wicked doc

Chambeur
  • 1,509
  • 1
  • 12
  • 16
  • thanks for the help your code is rightand helpful though my issue is i would like to use wicked to update my user after signing up with email and password i would like wicked to update the other fields and then save – user4737150 Apr 01 '15 at 10:27
  • The part 3. Using strong parameters should fix your problem. – Chambeur Apr 01 '15 at 10:42
  • have this step in my wicked controller(code) def user_params params.require(:user).permit(:first_name, :middle_name, :last_name, :address_first_line, :address_second_line, :city, :nationality) end – user4737150 Apr 01 '15 at 10:46
  • i realise something in my form whenever i use user or current_user i get an error of argument cannot be nil and whenever i use User.last it doesnt save i really cnt figure out what to use on my form helper – user4737150 Apr 01 '15 at 10:54
  • It's not because you define the method `user_params` that it will automatically be triggered. I think you don't know how strong parameters works. – Chambeur Apr 01 '15 at 11:01
  • im new to the rails platform could you please help me out with this so that i can configure this the right way – user4737150 Apr 01 '15 at 11:09
  • The best way to debug is to cut your code in part. For example, are you sure that devise create your user ? If yes, then it's probably your wizard controller. Did you test the code I gave you in the part 4 ? If yes, did it change anything ? – Chambeur Apr 01 '15 at 11:20
  • If you're new to rails, take a look at http://railscasts.com/ and this screen cast: http://railscasts.com/episodes/346-wizard-forms-with-wicked – Chambeur Apr 01 '15 at 11:22
  • your code changed something but the major concern is with the point of saving the user info and devise creates the user and even sends the confirmation email...anyway let me look through the guides maybe i will get some help thanks – user4737150 Apr 01 '15 at 11:26