Ad

Undefined Method In Rails

- 1 answer

I'm new to ruby and started to create my *nd toy app. I:

  1. Created controller 'questions'
  2. Created model 'question'
  3. Created controller action 'new'
  4. Added 'New.html.erb' file

in erb file I use form_for helper and and new controller action where I instantiate @question instance variable. When I try to run this I get 'undefined method: questions_path for #<ActionView::Base:0x5be5e24>' error. Below is my new.html.erb:

<%form_for @question do |f| %>
   <%=f.text_field :title %>
<%end%>    

Please advise how to fix this and also help me with aliasing this controller action. What I mean is I would like to type http://mysite/questions/ask, instead of /questions/create

Ad

Answer

In config/routes.rb you will need to add:

map.resources :questions

to fix the undefined method questions_path problem.

One way to get /questions/ask is to modify routes.rb like so:

map.ask_question '/questions/ask', :controller => 'questions', :action => 'create'

which will give you ask_question_path which you can reference in your code.

Ad
source: stackoverflow.com
Ad