steven_noble
steven_noble

Reputation: 4203

Why am I getting this strange routing error?

Why am I getting this strange routing error when I click the "Create Questions" button?

No route matches [POST] "/questions"

I have...

/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'

I always access questions#new from products#show, so @product.present? is always true.

Upvotes: 1

Views: 72

Answers (1)

AlexBrand
AlexBrand

Reputation: 12399

You are missing the :create action in your routes:

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

Upvotes: 2

Related Questions