Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method in rails

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

like image 628
Valentin V Avatar asked Mar 02 '23 04:03

Valentin V


1 Answers

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.

like image 181
Michael Sepcot Avatar answered Mar 05 '23 15:03

Michael Sepcot