3

我正在自杀试图解决这个路由错误。

一些背景信息:我正在使用 Ryan Bates 的 Rails Cast on Wicked Wizard Forms 创建一个多步骤表单。我收到路由错误:

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 

用户步骤控制器(用于 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

路线:

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
4

3 回答 3

1

在您的 user_controller#create 方法中尝试更改

redirect_to user_steps_path(@user)

redirect_to user_steps_path(:gender)

另外,我认为您的路由文件中不需要此行:

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

于 2012-09-26T15:12:31.017 回答
1

您正在覆盖 :id 参数,wicked 使用该参数来控制步骤。要传递其他查询字符串参数,您需要使用第二个 arg(选项哈希)到 user_steps_path,这意味着您需要提供 :id(步骤)作为第一个 arg。

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

然后在你的控制器中:

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

最后在你看来(不要忘记 :method => :put);

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

虽然不确定为什么不使用表单辅助变量 f 来生成字段(例如 <%= f.radio_button :gender, "Guy" %>),但它应该仍然可以工作。

于 2013-01-05T04:30:36.980 回答
0

在用户控制器中,
redirect_to user_steps_path(@user)
我认为这可以解决您的问题。

于 2012-09-07T17:02:37.873 回答