2013-04-30 83 views
1

當我點擊「創建問題」按鈕時,爲什麼會出現這個奇怪的路由錯誤?爲什麼我得到這個奇怪的路由錯誤?

No route matches [POST] "/questions" 

我有...

/config/routes.rb

resources :questions, :only => [:index, :update, :destroy, :edit] 
resources :products do 
    resources :questions, :except => [:index, :update, :destroy, :edit] 
end 

/app/controllers/questions_controller.rb

def new 
    @product = Product.find(params[:product_id]) 
    session[:product] = @product.id 
    @question = @product.questions.build 
end 

def create 
    @product = Product.find(session[:product]) 
    flash[:notice] = "Question was successfully created." if @question = @product.questions.create(params[:question]) 
    respond_with @product, @question 
end 

/app/vie WS /問題/ _form.html.haml

= simple_form_for @question do |f| 
    = f.association :products, :label => "Products" unless @product.present? 
    = f.button :submit, :id => 'submit_question' 

我一直訪問products#showquestions#new,所以@product.present?始終是真實的。

回答

2

你缺少的:在'資源

resources :questions, :only => [:index, :update, :destroy, :edit, :create] 
+0

是不是它意味着:創建你的路由的行動產品根本 資源:questions'? – 2013-04-30 11:39:32

+0

您的表單發佈到'/ questions'。有了上面的內容,你需要將表單提交到'/:product_id/questions' ... – AlexBrand 2013-04-30 11:44:32

+0

關於這個「接受」打勾時,Stackoverflow讓我在幾分鐘內,但它的原始建議添加:創建路線到'資源:問題'。 (該解決方案可讓我在編輯和新方法之間共享一個表單部分。) – 2013-04-30 11:47:47