Question

When I go to the path: /genre/new in my application I get this error:

myapp/app/views/genre/_form.html.erb where line #1 raised:
undefined method `genres_path' for #<#<Class:0x007fdcb39edcb0>:0x007fdcb39e8080>

However when I go to /genre/:id/edit the _form.html.erb file renders without error and the record is updated with no problems.

My new.html.erb and edit.html.erb files call <%= render 'form' %> and my _form.html.erb file has:

<%= form_for(@genre) do |f| %>
    <%= f.label :title %> <br />  <%= f.text_field :title %>
    <%= f.label :desc %>  <br />  <%= f.text_field :desc %>
    <%= f.submit %>
<% end %>

In genre_controller.rb my 'new' and 'edit' actions are as follows:

  def new
    @genre = Genre.new
    current_user.authorize! :create, @genre  # cancan authorization

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @genre }
    end
  end

  def edit
    @genre = Genre.find(params[:id])
    current_user.authorize! :update, @genre   # cancan authorization
  end

I've run a search in my codebase for the string "genres" and the only place it occurs is in the logs, so I'm sure this is not a typo in my code.

My guess is that Rails routing system correctly pluralizes "genre" to "genre", but form_for (or a dependency) is creating the pluralization "genres", but only when the parameter passed to it is empty or "new".

Given the error is around 'genres_path', I tried various combinations of the following in my routes.rb file, but they didn't solve the problem:

  match "/genres" => "genre#index", :as => :genre
  match "/genres/:id(.:format)" => "genre#show", :as => :genre
  match "/genre" => "genre#index", :as => :genres
  match "/genre/:id(.:format)" => "genre#show", :as => :genres

Any thoughts on how I can work around this?

EDIT: Here are the routes generated by the resources :genre statement in my routes.rb file:

genre_index GET    /genre(.:format)           {:action=>"index", :controller=>"genre"}
            POST   /genre(.:format)           {:action=>"create", :controller=>"genre"}
  new_genre GET    /genre/new(.:format)       {:action=>"new", :controller=>"genre"}
 edit_genre GET    /genre/:id/edit(.:format)  {:action=>"edit", :controller=>"genre"}
      genre GET    /genre/:id(.:format)       {:action=>"show", :controller=>"genre"}
            PUT    /genre/:id(.:format)       {:action=>"update", :controller=>"genre"}
            DELETE /genre/:id(.:format)       {:action=>"destroy", :controller=>"genre"}
Was it helpful?

Solution

on new.html.erb try

<%= form_for(@genre, :url => genre_path, :method => :post) do |f| %>

assuming you have your route setup as a resource - resources :genre

also this will not work on edit.html.erb

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for

Update:

this is the one we are interested in

POST   /genre(.:format)           {:action=>"create", :controller=>"genre"}

try this

<%= form_for(@genre, :url => {:action=>"create", :controller=>"genre"}, :method => :post) do |f| %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top