abhir
abhir

Reputation: 1069

Rails App Wicked Routing Error

I'm killing myself trying to solve this routing error.

Some background info: I'm using Ryan Bates' Rails Cast on Wicked Wizard Forms to create a multi-step form. I'm getting a routing error:

No route matches {:controller=>"user_steps", :action=>"show", :id=>nil}

Clearly, the user.id isn't being passed through to the subsequent view - any ideas how to solve this?

User Controller Create:

def create
    @user = User.new(params[:user])
    respond_to do |format|
      if @user.save
        session[:user_id] = @user.id
        format.html { redirect_to user_steps_path(@user) }
        #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 

User Steps Controller (for Wicked)

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

    def show
        render_wizard
    end 

    def update
        @user.update_attributes(params[:user])
        render_wizard @user
    end
end

Routes:

Store::Application.routes.draw do

  resources :likes

  resources :categories

  resources :user_steps

  match "user_steps/gender", to: "user_steps#gender", via: "post"
  resources :users

users_steps/gender.html.erb

<%= form_for :user, url: wizard_path do |f| %>

<div class="container" align="center">
  <div class="div2" align="center">
    <h2 align="center"> You are a ...</h2>
    <div class="container" align="center">
      <div class="row" align="center">
        <div class="span6">
          <h9>Guy</h9>
          <label for="user_gender_guy"><img src="http://i.imgur.com/bpIMo.png" class="new" width="200" height="500"></label>
          <input checked="checked" id="user_gender_guy" name="user[gender]" type="radio" value="Guy" /> 
        </div>
        <div class="span6">
          <h9>Girl</h9>
          <label for="user_gender_girl"><img src="http://i.imgur.com/xpA1S.png" class="new" width="200" height="500"></label>
          <input checked="checked" id="user_gender_girl" name="user[gender]" type="radio" value="Girl" />
        </div>
      </div>
    </div>
    </div> 
</div>

<%= f.submit "Next" %>
<% end %>

Rake Routes:

user_steps GET    /user_steps(.:format)          user_steps#index
                  POST   /user_steps(.:format)          user_steps#create
    new_user_step GET    /user_steps/new(.:format)      user_steps#new
   edit_user_step GET    /user_steps/:id/edit(.:format) user_steps#edit
        user_step GET    /user_steps/:id(.:format)      user_steps#show
                  PUT    /user_steps/:id(.:format)      user_steps#update
                  DELETE /user_steps/:id(.:format)      user_steps#destroy
user_steps_gender POST   /user_steps/gender(.:format)   user_steps#gender

Upvotes: 3

Views: 2272

Answers (3)

soupdog
soupdog

Reputation: 335

You are overwriting the :id parameter, which wicked uses to control the steps. To pass additional query string arguments, you need to use the second arg (options hash) to user_steps_path, which means you need to supply the :id (step) as the first arg.

redirect_to user_steps_path(:gender, :user_id => @user.id)

Then in your controller:

def show
    @user = User.find(params[:user_id])
    render_wizard
end 

Finally in your view (don't forget :method => :put);

<%= form_for @user, url: wizard_path, method: put do |f| %>
    ...    
<%= f.submit "Next" %>
<% end %>

Though not sure why you aren't using the form helper variable f to generate the fields (e.g. <%= f.radio_button :gender, "Guy" %>), but it should still work.

Upvotes: 1

jacob
jacob

Reputation: 2424

In your user_controller#create method try changing

redirect_to user_steps_path(@user) to

redirect_to user_steps_path(:gender)

Also I don't think you need this line in your routes file:

match "user_steps/gender", to: "user_steps#gender", via: "post"

Upvotes: 1

achempion
achempion

Reputation: 814

in User controller
redirect_to user_steps_path(@user)
I think this solve your problem.

Upvotes: 0

Related Questions