Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Pass Parameters to New Controller during 'redirect_to'

I'm using Ryan Bates' Rails Cast on Wicked Wizard Forms to create a multi-step form. I don't have a current_user method defined (not using an authentication gem) - so, I'm trying to pass the user.id parameter during the redirect_to - unfortunately, I can't seem to get it to work. Any help is appreciated!

My user controller create method

  def create
    @user = User.new(params[:user])
    respond_to do |format|
      if @user.save
        format.html { redirect_to controller: 'user_steps', id: 'user.id' }
        #format.html { redirect_to @user, notice: 'User was successfully created.' }#
        format.json { render json: @user, status: :created, location: @user }
      else
        format.html { render action: "new" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

The user_steps controller that to which I am redirecting:

class UserStepsController < ApplicationController
    include Wicked::Wizard
    steps :gender, :items, :brands, :final

    def show
        render_wizard
    end 
end
like image 390
abhir Avatar asked Sep 07 '12 03:09

abhir


1 Answers

You should pass it through as a param, ideally, which the redirect_to method will do for you if you use a proper route path.

Example:

redirect_to(user_steps_path(@user))

In your case, if you don't have a named route, you might do this:

redirect_to(controller: 'user_steps', id: @user.to_param)

In URLs it's advisable to use the to_param method. id is used for database queries.

What you're passing in is literally 'user.id' as a parameter. It will not be evaluated.

like image 157
tadman Avatar answered Sep 21 '22 19:09

tadman