1

I've been stuck on this for a bit and can't figure out the exact reason why I'm getting the following error:

undefined method `entries_path' for <%= form_for(@entry) do |f| %>

entry_controller:

class EntryController < ApplicationController
  def index
  end

  def new
    @entry = Entry.new
  end

   def create
    @entry = Entry.new(user_params)
    if @entry.save
      redirect_to @entry
    else
      render 'new'
    end
  end

  private

    def user_params
      params.require(:entry).permit(:comment, :flag)
    end

end

routes has:

resources :entry

and the new page where the error occurs:

<%= form_for(@entry) do |f| %>

  <%= f.label :comment %>
  <%= f.text_field :comment %>

  <%= f.label :flag %>
  <%= f.text_field :flag %>

<% end %>

I can't figure out why I'm getting this error.

Saad
  • 26,316
  • 15
  • 48
  • 69
  • Are you sure it's the `new` page that is giving you the error? Is there a stack trace you can share? How about the `Entry` model? Where does `entries_path` appear in your code? – Peter Alfvin Sep 12 '13 at 04:05
  • yea its the new page, entries_path doesn't appear anywhere in my code. How do you get the stack trace? – Saad Sep 12 '13 at 04:09
  • so I created a model using " rails generate model Entry comment:text flag:binary", but it ended up creating a table with name entries, which is really confusing. – Saad Sep 12 '13 at 04:13
  • That's normal. Rails uses the plural form of the ActiveRecord class to name the tables. And actually, that's the problem with your routes file. It should be `resources :entries`. – Peter Alfvin Sep 12 '13 at 04:30
  • That's not the `<%= form_for %>` instead of `<% form_for %>` ? – pierallard Sep 12 '13 at 08:36

2 Answers2

2

form_for needs to reference the path associated with @entry (i.e. entries_path), but your routes.rb file uses the singular form of the resource (:entry) rather than the required plural form (:entries), so the proper path names don't exist.

Rails models use the singular form, but the Rails database, controllers, views use the plural form and this is reflected in the routes file. One way to remember this is that a model is describing a single class that each object belongs to. Everything else, pretty much, is responsible for managing multiple instances, so while they themselves are singular (e.g. Controller), they refer to the objects they manage in the plural form (e.g. EntriesController, controller/entries directory).

See Ruby on Rails plural (controller) and singular (model) convention - explanation for more discussion of this.

Community
  • 1
  • 1
Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106
  • do my views still go under entry>new.html.erb or should i create an entries folder in view? – Saad Sep 12 '13 at 05:31
  • I guess you are using rails 4,even though Have you tried it will show you all the routes available for entries.It might give you some idea of the error. – vishB Sep 12 '13 at 06:16
  • @Saad You'll need to rename your directory to the plural form. See updated answer. On a related point, if this is your first Rails project and you're not following a tutorial, you might want to consider it. There are a lot of details like this for which a tutorial is helpful. The Hartl one is particularly well done and popular. – Peter Alfvin Sep 12 '13 at 13:26
0

Controller and views should always be treated in plural form. For example, if you have an object Book, then the controller declaration should be

class BooksController < ApplicationController

and the views( new, edit, show, index ) should be inside a folder named

/books

Also, the declaration of routes should be in plural form. In this case, the routes should be declared as

resources :books

You could try to generate the controller and view folder by running in your terminal:

rails generate controller name_of_object_in_plural_form( for sample, books)

The script will generate a controller named books_controller.rb and /books folder under /views