Mywebsite/admin/categories point to show.html.erb?

Hi everyone, I’m having some troubles with RoR and REStful
controllers:

I generated a simple scaffold “categories” wich contains name and
branch column: easy.
I also generated a namespaced controller “admin/categories”.
I replaced all the scaffold’s link_path in my views to match with my
admin namespace, such as “<%= link_to ‘Show’, @category %>” became "<
%= link_to ‘Show’, [:admin, @category] %> ".
I replace the resource routes with my namespace:

namespace :admin do
resources :categories
end

And everything works fine… Until I created a resource in my database
using scaffold forms… Now when I’m going to “mywebsite/admin/
categories”, rails is trying to render show method:
No route matches {:action=>“show”, :controller=>“admin/categories”}

I search on the web to fix and found this:
http://www.ruby-forum.com/topic/145160

But I didn’t really understand how to fix the problem.

Can someone help me? =)

Regards,
Romain ‘Maz’ B.

What version of Rails are you using? Can you post your entire routes.rb
file?

I’m using rails 3.1.0 on ruby 1.9.2. my routes.rb just contains the
admin namespace and a root_to which points to application#index. I’ll
post rake routes as soon I can if it’s necessary

On 13 September 2011 22:47, Romain ‘Maz’ BILLOIR [email protected]
wrote:

namespace :admin do
resources :categories
end

And everything works fine… Until I created a resource in my database
using scaffold forms… Now when I’m going to “mywebsite/admin/
categories”, rails is trying to render show method:
No route matches {:action=>“show”, :controller=>“admin/categories”}

When you say that it is trying to render the show method do you mean
that is during running the index action? Is the render being invoked
from the index action? Have a look in the rails log. If you can’t
see what is happening then post the log starting with what happens
when you click the link.

You also might like to have a look at the rails guide on debugging.
That will show you how to use ruby-debug to break into your code and
see what is going on. So you can break into the index action
(assuming it is getting there) and inspect data and follow the flow to
see what is happening.

Colin

gplus.to/clanlaw

Okay, i just fixed the problem.

In index.html.erb to display link i used this:

<%= link_to ‘Show’, admin_category_path(@category) %>
<%= link_to ‘Edit’, edit_admin_category_path(@category) %></
td>
<%= link_to ‘Destroy’, admin_category_path(@category),
confirm: ‘Are you sure?’, method: :delete %>

which is the default generated link by scaffold, just added “admin_”.
But the problem come from the @category, if i delete the @:

<%= link_to ‘Show’, admin_category_path(category) %>
<%= link_to ‘Edit’, edit_admin_category_path(category) %>
<%= link_to ‘Destroy’, admin_category_path(category), confirm:
‘Are you sure?’, method: :delete %>

Everything works fine!

Thanks a lot!

No, this is what’s happening:
when i typed for the first time /admin/categories, it rendered me the
empty list of categories. I clicked the New link, rendered the add
form, submit the form rendered the new category, click the Return link
which is /admin/categories, performed show action. WHY?

If i delete the entry in my database, /admin/categories performs index
action like it should. WHY²?

Here is my log:

Started GET “/admin/categories” for 127.0.0.1 at 2011-09-14 23:16:04
+0200
Processing by Admin::CategoriesController#index as HTML
[1m [36mCategory Load (0.5ms) [0m [1mSELECT “categories”.* FROM
“categories” [0m
Rendered admin/categories/index.html.erb within layouts/application
(19.3ms)
Completed 200 OK in 120ms (Views: 117.5ms | ActiveRecord: 0.5ms)

Started GET “/assets/application.css?body=1” for 127.0.0.1 at
2011-09-14 23:16:05 +0200
Served asset /application.css - 304 Not Modified (0ms)

Started GET “/assets/jquery.js?body=1” for 127.0.0.1 at 2011-09-14
23:16:05 +0200
Served asset /jquery.js - 304 Not Modified (6ms)

Started GET “/assets/categories.js?body=1” for 127.0.0.1 at 2011-09-14
23:16:05 +0200
Served asset /categories.js - 304 Not Modified (2ms)

Started GET “/assets/admin/categories.js?body=1” for 127.0.0.1 at
2011-09-14 23:16:05 +0200
Served asset /admin/categories.js - 304 Not Modified (2ms)

Started GET “/assets/application.js?body=1” for 127.0.0.1 at
2011-09-14 23:16:05 +0200
Served asset /application.js - 304 Not Modified (0ms)

Started GET “/assets/jquery_ujs.js?body=1” for 127.0.0.1 at 2011-09-14
23:16:05 +0200
Served asset /jquery_ujs.js - 304 Not Modified (4ms)

Started GET “/admin/categories/new” for 127.0.0.1 at 2011-09-14
23:16:06 +0200
Processing by Admin::CategoriesController#new as HTML
Rendered admin/categories/_form.html.erb (24.0ms)
Rendered admin/categories/new.html.erb within layouts/application
(38.3ms)
Completed 200 OK in 48ms (Views: 44.7ms | ActiveRecord: 2.0ms)

Started GET “/assets/application.css?body=1” for 127.0.0.1 at
2011-09-14 23:16:06 +0200
Served asset /application.css - 304 Not Modified (0ms)

Started GET “/assets/jquery_ujs.js?body=1” for 127.0.0.1 at 2011-09-14
23:16:06 +0200
Served asset /jquery_ujs.js - 304 Not Modified (0ms)

Started GET “/assets/jquery.js?body=1” for 127.0.0.1 at 2011-09-14
23:16:06 +0200
Served asset /jquery.js - 304 Not Modified (0ms)

Started GET “/assets/admin/categories.js?body=1” for 127.0.0.1 at
2011-09-14 23:16:06 +0200
Served asset /admin/categories.js - 304 Not Modified (0ms)

Started GET “/assets/categories.js?body=1” for 127.0.0.1 at 2011-09-14
23:16:06 +0200
Served asset /categories.js - 304 Not Modified (0ms)

Started GET “/assets/application.js?body=1” for 127.0.0.1 at
2011-09-14 23:16:06 +0200
Served asset /application.js - 304 Not Modified (0ms)

Started POST “/admin/categories” for 127.0.0.1 at 2011-09-14 23:16:16
+0200
Processing by Admin::CategoriesController#create as HTML
Parameters: {“utf8”=>“✓”,
“authenticity_token”=>“PVlLpCPYEhLre3tsFlYRQdVnbbySI2Gw/YSCEuzjnWo=”,
“category”=>{“name”=>“vaze”, “branch”=>“Project”}, “commit”=>“Create
Category”}
[1m [35m (0.3ms) [0m BEGIN
[1m [36mSQL (24.0ms) [0m [1mINSERT INTO “categories” (“branch”,
“created_at”, “name”, “updated_at”) VALUES ($1, $2, $3, $4) RETURNING
“id” [0m [[“branch”, “Project”], [“created_at”, Wed, 14 Sep 2011
21:16:16 UTC +00:00], [“name”, “vaze”], [“updated_at”, Wed, 14 Sep
2011 21:16:16 UTC +00:00]]
[1m [35m (12.0ms) [0m COMMIT
Redirected to http://127.0.0.1:3000/admin/categories/7
Completed 302 Found in 51ms

Started GET “/admin/categories/7” for 127.0.0.1 at 2011-09-14 23:16:16
+0200
Processing by Admin::CategoriesController#show as HTML
Parameters: {“id”=>“7”}
[1m [36mCategory Load (0.8ms) [0m [1mSELECT “categories”.* FROM
“categories” WHERE “categories”.“id” = $1 LIMIT 1 [0m [[“id”, “7”]]
Rendered admin/categories/show.html.erb within layouts/application
(7.1ms)
Completed 200 OK in 25ms (Views: 16.6ms | ActiveRecord: 4.1ms)

Started GET “/assets/application.css?body=1” for 127.0.0.1 at
2011-09-14 23:16:16 +0200
Served asset /application.css - 304 Not Modified (0ms)

Started GET “/assets/jquery.js?body=1” for 127.0.0.1 at 2011-09-14
23:16:16 +0200
Served asset /jquery.js - 304 Not Modified (0ms)

Started GET “/assets/jquery_ujs.js?body=1” for 127.0.0.1 at 2011-09-14
23:16:16 +0200
Served asset /jquery_ujs.js - 304 Not Modified (0ms)

Started GET “/assets/categories.js?body=1” for 127.0.0.1 at 2011-09-14
23:16:16 +0200
Served asset /categories.js - 304 Not Modified (0ms)

Started GET “/assets/admin/categories.js?body=1” for 127.0.0.1 at
2011-09-14 23:16:16 +0200
Served asset /admin/categories.js - 304 Not Modified (0ms)

Started GET “/assets/application.js?body=1” for 127.0.0.1 at
2011-09-14 23:16:16 +0200
Served asset /application.js - 304 Not Modified (0ms)

Started GET “/admin/categories” for 127.0.0.1 at 2011-09-14 23:16:18
+0200
Processing by Admin::CategoriesController#index as HTML
[1m [35mCategory Load (0.3ms) [0m SELECT “categories”.* FROM
“categories”
Rendered admin/categories/index.html.erb within layouts/application
(39.4ms)
Completed 500 Internal Server Error in 52ms

ActionView::Template::Error (No route matches
{:action=>“show”, :controller=>“admin/categories”}):
13:
14: <%= category.name %>
15: <%= category.branch %>
16: <%= link_to ‘Show’, admin_category_path(@category) %></
td>
17: <%= link_to ‘Edit’,
edit_admin_category_path(@category) %>
18: <%= link_to ‘Destroy’, admin_category_path(@category),
confirm: ‘Are you sure?’, method: :delete %>
19:
app/views/admin/categories/index.html.erb:16:in block in _app_views_admin_categories_index_html_erb___936809880_79782210' app/views/admin/categories/index.html.erb:12:in each’
app/views/admin/categories/index.html.erb:12:in
_app_views_admin_categories_index_html_erb___936809880_79782210' app/controllers/admin/categories_controller.rb:7:in index’

Rendered /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.1.0/lib/
action_dispatch/middleware/templates/rescues/routing_error.erb within
rescues/layout (0.8ms)