Search code examples
ruby-on-railsroutescrudrails-routingactiveresource

Why is resources in the routes.rb file not creating ALL of the routes?


I am making a rails application. After a user has registered (I have already created user registration with devise), they can fill out this form that will contain their profile information. The form should submit a post request to the create action in the informations controller, but for some reason I can't configure the routes properly. When i run rake routes, for informations#create, which is what the form should be going to, it has a blank path. There is also informations#index, which is what I guess its going to now. How do I get the form to go to informations#create if the path is blank? I have done this several times, and i can't find what is wrong. Here is the model:

class Information < ActiveRecord::Base
    belongs_to :user
end

Here is the controller:

class InformationsController < ApplicationController

    def new
        @information = Information.new
    end
    def create
        @information = Information.create(params[:information])
        redirect_to student_path
    end
    def index
    end
end

And here is the view for the new action.

<div class="span6 offset3 text-center">
<h1>Edit your information</h1>

    <%= simple_form_for @information do |f| %>
        <%= f.input :skills %>


        <%= f.input :looking_for, :label => 'What help do you need?' %>
        <%= f.input :my_idea %>
        <%= submit_tag "Save", :class => "btn btn-primary btn-large" %>
    <% end %>
</div>

Here is the line in the routes file:

resources :informations

I get the following error:

undefined method `information_index_path' for #<#:0x007f9c00c7b3e0>

Here is rake routes for the files

 informations GET    /informations(.:format)          informations#index
             POST   /informations(.:format)          informations#create

Here is my full stacktrace when trying to load the page that shows the form:

Started GET "/informations/new" for 127.0.0.1 at 2013-10-21 17:25:09 -0400
Processing by InformationsController#new as HTML
  Rendered informations/new.html.erb within layouts/application (64.2ms)
Completed 500 Internal Server Error in 70ms

ActionView::Template::Error (undefined method `information_index_path' for #<#<Class:0x007ff03e8b34a0>:0x007ff03e8b23e8>):
    2:  <div class="span6 offset3 text-center">
    3:  <h1>Edit your information</h1>
    4: 
    5:      <% simple_form_for @information do |f| %>
    6:          <%= f.input :skills %>
    7:          
    8:          
  app/views/informations/new.html.erb:5:in `_app_views_informations_new_html_erb__3172218935119285240_70334909089600'


  Rendered /usr/local/rvm/gems/ruby-2.0.0-p247@firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
  Rendered /usr/local/rvm/gems/ruby-2.0.0-p247@firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.7ms)
  Rendered /usr/local/rvm/gems/ruby-2.0.0-p247@firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (9.1ms)


Started GET "/informations/new" for 127.0.0.1 at 2013-10-21 17:25:09 -0400
Processing by InformationsController#new as HTML
  Rendered informations/new.html.erb within layouts/application (8.3ms)
Completed 500 Internal Server Error in 13ms

ActionView::Template::Error (undefined method `information_index_path' for #<#<Class:0x007ff03e8b34a0>:0x007ff03e8708a8>):
    2:  <div class="span6 offset3 text-center">
    3:  <h1>Edit your information</h1>
    4: 
    5:      <% simple_form_for @information do |f| %>
    6:          <%= f.input :skills %>
    7:          
    8:          
  app/views/informations/new.html.erb:5:in `_app_views_informations_new_html_erb__3172218935119285240_70334908978600'


  Rendered /usr/local/rvm/gems/ruby-2.0.0-p247@firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.0ms)
  Rendered /usr/local/rvm/gems/ruby-2.0.0-p247@firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.3ms)
  Rendered /usr/local/rvm/gems/ruby-2.0.0-p247@firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (14.0ms)

Solution

  • Your problem is related to the fact that the word information is both the plural and the singular form. Just like water, or paper, or evidence. Rails considers those words uncountable.

    There are two things you can do: either name your model something else or edit the file config/initializers/inflections.rb like so:

    ActiveSupport::Inflector.inflections(:en) do |inflect|
      inflect.uncountable %w( information )
    end