2011-05-15 117 views
1

在routes.rb中我有:Rails的路由錯誤

get "survey/show" => "survey#show" 
post "survey/step_2" => "survey#step_2" 
post "survey/step_3" => "survey#step_3" 

而在step_2.html.erb我:

<%= form_for @result, :url => { :controller => 'survey', :action => 'step_3' } do |f| %> 

而在survey_controller.rb我:

def step_2 
@result = Result.new(params[:result]) 

if @result.save 
    session[:result_id] = @result.id 
    render :action => "step_2" 
else 
    render :action => "show" 
end 
end 

def step_3 
@result = Result.find(session[:result_id]) 

if @result.update_attributes(params[:result]) 
    render :action => "step_3" 
else 
    render :action => "step_2" 
end 
end 

而當我在步驟2上提交表單時,出現以下錯誤: No route matches "/survey/step_3"

+0

你確定你正在請求是一個POST請求。根據您的路線定義,獲取調查/步驟_3的請求將被拒絕。 – codinguser 2011-05-15 02:59:19

回答

3

我相信Rails的form_for方法可能會使得這個PUT請求,因爲@result對象有一個ID。我相信你應該改變你的form_for行:

<%=的form_for @result, :URL => {:控制器=> '調查',:動作=> 'step_3'}, :HTML => { :method =>:post} do | f | %>

或更改路線類型put在routes.rb中

0

您必須使用匹配。

match 'survey/step_3' => 'survey#step_3', :via => 'post' 

我可能是錯誤的:通過,但它是這樣的。