Michael
Michael

Reputation: 6907

'no implicit conversion of symbol into Integer' with form_for

I am receiving

no implicit conversion of symbol into Integer

with the following code:

<%= form_for  @question, admin_questions_path do |f| %>         
     <%= f.label :question %>   
     <%= f.text_area :question %>
<% end %>

But when I change the form_for method as follows; the form renders correctly.

<%= form_for  [:admin, @question] do |f| %>

What is the difference between the code? If the incorrect code is routing to the create method of the Admin::QuestionsController with the path admin_questions_path why does it not work? I am new to rails and namespacing, so I may be missing something completely obvious.

Edit:

The questions controller is namespaced under admin.

namespace :admin do
    resources :questions, only: [:index, :new, :create]
end

Upvotes: 2

Views: 992

Answers (1)

universa1
universa1

Reputation: 172

The first example should be syntactic sugar for writing the following (if i'm not mistaken):

form_for [@question, admin_questions_path] do |f|

Where you most likely wanted:

form_for @question, url: admin_questions_path do |f|

Your second form uses polymorphic routing to automatically determine the correct route, this is useful if you want to use the same form for editing and creating. The link Arup provided should be helpful about that topic, as is the guide about routing at: http://guides.rubyonrails.org/routing.html

Upvotes: 5

Related Questions