开发者

Setting up restful routes as a total newb

开发者 https://www.devze.com 2023-01-02 17:42 出处:网络
I\'m getting the following error: Unknown action No action responded to show. Actions: activate, destroy, index, org_deals, search, and suspend

I'm getting the following error:

Unknown action

No action responded to show. Actions: activate, destroy, index, org_deals, search, and suspend

Controller:

class Admin::HomepagesController < Admin::ApplicationController

def org_deals
@organization = Organization.find(:all)
end

Routes:

   map.root :controller => 'main'
   map.admin '/admin', :controller => 'admin/main'

   map.namespace :admin do |admin|
   admin.resources :organizations, :collection => {:search => :get}, :member => {:suspend => :get, :activate =>开发者_StackOverflow社区 :get}

To note: This is a controller inside of a controller.

Any idea why this is defaulting to show?

Update:

I updated what the routes syntax is. Read that article, and tried quite a few variations but its still adamantly looking for a show.


Firstly, it looks like your routes file has the wrong syntax. If you are trying to establish routes for nested resources, you'd do it like so:

map.resources :admin
  admin.resources :organizations
end

This would give you paths such as:

/admin/
/admin/1
/admin/1/organizations
/admin/1/organizations/1

The mapping from route to controller/action is done via a Rails convention, where HTTP verbs are assigned in ways that are useful for the typical CRUD operations. For example:

/admin/1/organizations/1

would map to several actions in the OrganizationsController, distinguished by the type of verb:

/admin/1/organizations/1 # GET -> :action => :show
/admin/1/organizations/1 # PUT -> :action => :update
/admin/1/organizations/1 # DELETE -> :action => :destroy

"Show" is one of the seven standard resourceful actions that Rails gives you by default. You can exclude "show" with the directive :except => :show, or specify only the resourceful actions you want with :only => :update, for example.

I recommend you look at Rails Routing from the Outside In, which is a great introduction to this topic.

EDIT

I see I ignored the namespacing in my answer, sorry. How about this:

  map.namespace(:admin) do |admin|
    admin.resources :homepages, :member => { :org_deals => :get }
  end

This will generate your org_deals action as a GET with an id parameter (for the organization). You also get a show route, along with the six other resourceful routes. rake routes shows this:

org_deals_admin_homepage GET /admin/homepages/:id/org_deals(.:format) {:controller=>"admin/homepages", :action=>"org_deals"}

Of course your homepages_controller.rb has to live in app/controllers/admin/

EDIT redux

Actually, you want organizations in the path, I'll bet, in which case:

  map.namespace(:admin) do |admin|
    admin.resources :organizations, :controller => :homepages, :member => { :org_deals => :get }
  end

which gives you:

org_deals_admin_organization GET    /admin/organizations/:id/org_deals(.:format) {:controller=>"admin/homepages", :action=>"org_deals"}


By specifying admin.resources ... you are telling Rails you want the seven default different routes in your application. If you do not want them, and only want the ones you specify, do not use .resources. Show is called because that's the default route called for a GET request with a path such as /admin/id when you have the default resources.

0

精彩评论

暂无评论...
验证码 换一张
取 消