Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails route not working using resource :model

In my Rails 3.1 app, I have a model named "Child"

In my routes.rb file, I have the line:

resources :children

Here is the whole routes.rb text:

  root :to => "pages#home"

  resources :children

Here is the full rake routes results (note that the majority of the routes are related to ActiveAdmin):

                  children GET        /children(.:format)                       {:action=>"index", :controller=>"children"}
                           POST       /children(.:format)                       {:action=>"create", :controller=>"children"}
                 new_child GET        /children/new(.:format)                   {:action=>"new", :controller=>"children"}
                edit_child GET        /children/:id/edit(.:format)              {:action=>"edit", :controller=>"children"}
                     child GET        /children/:id(.:format)                   {:action=>"show", :controller=>"children"}
                           PUT        /children/:id(.:format)                   {:action=>"update", :controller=>"children"}
                           DELETE     /children/:id(.:format)                   {:action=>"destroy", :controller=>"children"}

When I run "rake routes" I see this line in the results:

children GET  /children(.:format) {:action=>"index", :controller=>"children"}

This is the code in my ChildrenController:

     def index
        @children = Child.all

        @base_class = "children-index"
        @title = "Your Children"

        respond_to do |format|
            format.html # children/index.html.erb
            format.json { render :json => @children }
        end
    end

    def show
        @child = Child.find(params[:id])

        @base_class = "child-show"
        @title = child_name(@child)

        respond_to do |format|
            format.html # children/show.html.erb
            format.json { render :json => @child }
        end
    end

When I visit the url "/children" I get this error:

No route matches {:action=>"show", :controller=>"children"}

Here's the full trace:

Started GET "/children" for 127.0.0.1 at 2011-11-07 13:06:24 -0600
  Processing by ChildrenController#index as HTML
  Child Load (0.8ms)  SELECT `children`.* FROM `children` 
Rendered children/index.html.erb within layouts/application (65.0ms)
Completed 500 Internal Server Error in 166ms

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"children"}):
    1: <h1><%= title %></h1>
    2: <ul>
    3:  <%= @children.each do |child| %>
    4:      <li><%= link_to child.child_name(child), child_path(@child) %></li>
    5:  <% end %>
    6: </ul>
  app/views/children/index.html.erb:4:in `block in _app_views_children_index_html_erb__674498165009231817_70298485459960'
  app/views/children/index.html.erb:3:in `each'
  app/views/children/index.html.erb:3:in `_app_views_children_index_html_erb__674498165009231817_70298485459960'
  app/controllers/children_controller.rb:9:in `index'

Rendered /.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.5ms)

Why is "/children" trying to execute the "show" action and why does the show action act like the route is not there? All of my other models have worked just fine thus far using the "resource :model" instruction.

like image 239
Randy Burgess Avatar asked Nov 07 '11 18:11

Randy Burgess


1 Answers

in the server output we have this: Processing by ChildrenController#index as HTML that confirms the route is right.

but inside your template you are using an route that the server can't find.

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"children"}):

1: <h1><%= title %></h1>
2: <ul>
3:  <%= @children.each do |child| %>
4:      <li><%= link_to child.child_name(child), child_path(@child) %></li>
5:  <% end %>
6: </ul>

more precisely, in line 4 you got a problem, probably here: child_path(@child)

are you using any custom ids for your models (overriding the to_param method) ?


just to make it clear, this is not a route error, is a template error

like image 121
Nicos Karalis Avatar answered Sep 16 '22 19:09

Nicos Karalis