2012-09-07 52 views
3

我殺了自己試圖解決這個路由錯誤。Rails應用程序邪惡的路由錯誤

一些背景信息:我正在使用Ryan Bates的Rails Cast在邪惡嚮導窗體上創建多步表單。我得到一個路由錯誤:

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

顯然,user.id沒有被通過傳遞給後續的觀點 - 任何想法如何解決這個問題?

用戶控制器創建:

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 

使用者步驟控制器(用於邪惡)

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 

途徑:

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 %> 

耙路線:

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 
+0

注意:「匹配」user_steps/gender「...」必須位於* resources:user_steps行的上方。 – jdoe

回答

0

在用戶控制器
redirect_to user_steps_path(@user)
我覺得這個解決您的問題。

+0

嗯,我試過這個 - 不起作用。在頂部查看更新的代碼。 – abhir

+2

'user_step'的'rake routes'呢? – achempion

+0

更新了耙路線 – abhir

1

在你user_controller#創建方法嘗試改變

redirect_to user_steps_path(@user)

redirect_to user_steps_path(:gender)

此外,我不認爲你需要這條線在你的路由文件:

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

1

你正在覆蓋:id參數,它是邪惡的用來控制這些步驟。要傳遞額外的查詢字符串參數,您需要使用第二個參數(options hash)到user_steps_path,這意味着您需要提供:id(step)作爲第一個參數。

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

然後在控制器中:

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

最後(不要忘記:方法=>:放);

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

雖然不知道你爲什麼不使用的形式助手變量f生成的字段(例如<%= f.radio_button:性別,「蓋伊」%>),但它應該仍然工作。