form for - What are some possible reasons that you would get an undefined method 'TheModelName_path' error using a form_for in rails 3? -
i can't figure out why have error knowledge set should work.
this error get: nomethoderror @ /short/new undefined method `shorts_path' #<#:0x007fbc441426e0>
- my model short not shorts
- when run rake routes there no
shorts_path
i'm not sure helper coming from. don't understand whyform_for
giving me error when@short
defined indef new
section of controller.
can please explain me?
thank in advance
this controller looks like
class shortcontroller < applicationcontroller def show @short = short.find(params[:id].to_i(36)) respond_to |format| #redirect directly url stored long in database format.html { redirect_to @short.long} end end def new @short = short.new respond_to |format| format.html # new.html.erb end end def create @short = short.new(params[:short]) respond_to |format| if @short.save format.html { render action: "show" } else format.html { render action: "new" } end end end end
routes
trainingproject::application.routes.draw root :to => 'short#welcome' resources :short, :only => [:new, :create, :show] end
form partial rendered in view new
<%= form_for(@short) |f| %> <% if @short.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@short.errors.count,"error") %> prohibited url being saved:</h2> <ul> <% @short.errors.full_messages.each |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="form-field"> <%= f.label "enter url" %><br /> <%= f.text_field :long %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
you're getting error because form_for(@short)
, @short
new short
record, attempt use path helper called shorts_path
route form needs go. you're missing route definition in config/routes.rb
file:
resources :shorts
please read getting started , routing guides, should explain things you're missing.
Comments
Post a Comment