Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RoR: nested namespace routes, undefined method error

I am working on the admin section of a new rails app and i'm trying to setup some routes to do things "properly". 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 have verified that the routes exist:

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 going wrong and why?

like image 568
Jason Miesionczek Avatar asked Dec 13 '22 06:12

Jason Miesionczek


2 Answers

Your Delete link should end in _path:

<%= link_to 'Delete', admin_blog_path(blog), :method => :delete %>
like image 199
John Topley Avatar answered Dec 29 '22 18:12

John Topley


I'm assuming you are using rails 2.0.x so the way you generate a route is __path

admin_blog_path(blog) 

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

blog_path(blog)
like image 27
TonyLa Avatar answered Dec 29 '22 19:12

TonyLa