RoR: nested namespace routes, undefined method error

I am working on the admin section of a new rails application, and I am trying to configure some routes to do something "correctly." I have the following controller:

class Admin::BlogsController < ApplicationController def index @blogs = Blog.find(:all) end def show @blog = Blog.find(params[:id]) end ... end 

in routes.rb:

 map.namespace :admin do |admin| admin.resources :blogs end 

in views /admin/blogs/index.html.erb:

 <% for blog in @blogs %> <%= link_to 'Delete', admin_blog(blog), :method => :delete <% end %> 

I checked that there are routes:

 admin_blogs GET /admin/blogs {:action => "index", :controller=>"admin/blogs"} admin_blog GET /admin/blogs/:id {:action => "show", :controller => "admin/blogs"} .... 

but when I try to view http: // localhost: 3000 / admin / blogs , I get this error:

 undefined method 'admin_blog' for #<ActionView::Base:0xb7213da8> 

where am i wrong and why?

+4
source share
3 answers

Your Delete link should end with _path:

 <%= link_to 'Delete', admin_blog_path(blog), :method => :delete %> 
+10
source

I assume you are using rails 2.0.x, so the way to generate the __path route

 admin_blog_path(blog) 

and if you are riding a previous version, I think it’s just

 blog_path(blog) 
+2
source

Side note: I also see that your controller is defined as follows:

 class Admin::BlogsController < ApplicationController 

Shouldn't it be?

 class Admin::BlogsController < Admin::ApplicationController 
+1
source

Source: https://habr.com/ru/post/1277316/


All Articles