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/views/questions/_form.html.haml

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

我总是questions#new从访问products#show,所以@product.present?总是如此。

4

1 回答 1

2

您在路线中缺少 :create 操作:

resources :questions, :only => [:index, :update, :destroy, :edit, :create]
于 2013-04-30T11:38:11.377 回答