为什么我会收到这个奇怪的路由错误?

时间:2013-04-30 11:35:59

标签: ruby-on-rails simple-form rails-routing

单击“创建问题”按钮时,为什么会出现这种奇怪的路由错误?

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?始终为真。

1 个答案:

答案 0 :(得分:2)

您在路线中缺少:create action:

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