2010-06-23 21 views
1

新的鐵軌用戶在這裏。如何使用控制器和動作路由Rails URLS但沒有參數?

我有一個rails項目,我想接受一個控制器和一個動作,但沒有參數。目前,除非是「新」操作,否則每當我輸入一個沒有參數的操作時,(默認)腳手架創建的控制器將返回'show'命令。如果我將def添加到category_controller.rb文件並嘗試訪問該URL,那麼會得到一個「找不到ID = testaction的類別」。任何想法我失蹤?

下面可能有相關的代碼。

非常感謝

網址進入:http://0.0.0.0:3000/categories/testaction

# app/controllers/categories_controller.rb 
class CategoriesController < ApplicationController 
    # GET /categories 
    # GET /categories.xml 
    def index 
    @categories = Category.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.xml { render :xml => @categories } 
    end 
    end 

    # GET /categories/1 
    # GET /categories/1.xml 
    def testaction 
    @category = Category.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.xml { render :xml => @category } 
    end 
    end 


    # GET /categories/1 
    # GET /categories/1.xml 
    def show 
    @category = Category.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.xml { render :xml => @category } 
    end 
    end 



    # GET /categories/new 
    # GET /categories/new.xml 
    def new 
    @category = Category.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.xml { render :xml => @category } 
    end 
    end 

    # GET /categories/1/edit 
    def edit 
    @category = Category.find(params[:id]) 
    end 

    # POST /categories 
    # POST /categories.xml 
    def create 
    @category = Category.new(params[:category]) 

    respond_to do |format| 
     if @category.save 
     format.html { redirect_to(@category, :notice => 'Category was successfully created.') } 
     format.xml { render :xml => @category, :status => :created, :location => @category } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @category.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /categories/1 
    # PUT /categories/1.xml 
    def update 
    @category = Category.find(params[:id]) 

    respond_to do |format| 
     if @category.update_attributes(params[:category]) 
     format.html { redirect_to(@category, :notice => 'Category was successfully updated.') } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @category.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /categories/1 
    # DELETE /categories/1.xml 
    def destroy 
    @category = Category.find(params[:id]) 
    @category.destroy 

    respond_to do |format| 
     format.html { redirect_to(categories_url) } 
     format.xml { head :ok } 
    end 
    end 
end 

# Routes.rb 
ActionController::Routing::Routes.draw do |map| 
    map.resources :categories 

    # The priority is based upon order of creation: first created -> highest priority. 

    # Sample of regular route: 
    # map.connect 'products/:id', :controller => 'catalog', :action => 'view' 
    # Keep in mind you can assign values other than :controller and :action 

    # Sample of named route: 
    # map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase' 
    # This route can be invoked with purchase_url(:id => product.id) 

    # Sample resource route (maps HTTP verbs to controller actions automatically): 
    # map.resources :products 

    # Sample resource route with options: 
    # map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get } 

    # Sample resource route with sub-resources: 
    # map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller 

    # Sample resource route with more complex sub-resources 
    # map.resources :products do |products| 
    #  products.resources :comments 
    #  products.resources :sales, :collection => { :recent => :get } 
    # end 

    # Sample resource route within a namespace: 
    # map.namespace :admin do |admin| 
    #  # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb) 
    #  admin.resources :products 
    # end 

    # You can have the root of your site routed with map.root -- just remember to delete public/index.html. 
    # map.root :controller => "welcome" 

    # See how all your routes lay out with "rake routes" 

    # Install the default routes as the lowest priority. 
    # Note: These default routes make all actions in every controller accessible via GET requests. You should 
    # consider removing or commenting them out if you're using named routes and resources. 
    map.connect ':controller/:action/:id' 
    map.connect ':controller/:actoin' 
    map.connect ':controller/:action/:id.:format' 
end 


# Error Message 
ActiveRecord::RecordNotFound in CategoriesController#show 

Couldn't find Category with ID=testaction 

RAILS_ROOT: /home/john/Websites/sandbox/testnoparams 
Application Trace | Framework Trace | Full Trace 

/var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1616:in `find_one' 
/var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1599:in `find_from_ids' 
/var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:619:in `find' 
/home/john/Websites/sandbox/testnoparams/app/controllers/categories_controller.rb:28:in `show' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:1331:in `send' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:1331:in `perform_action_without_filters' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/filters.rb:617:in `call_filters' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue' 
/var/lib/gems/1.8/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in `ms' 
/var/lib/gems/1.8/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in `ms' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/rescue.rb:160:in `perform_action_without_flash' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/flash.rb:151:in `perform_action' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:532:in `send' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:532:in `process_without_filters' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/filters.rb:606:in `process' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:391:in `process' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:386:in `call' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/routing/route_set.rb:438:in `call' 

/usr/lib/ruby/1.8/benchmark.rb:308:in `realtime' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:87:in `dispatch' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:121:in `_call' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:130 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:114:in `call' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:108:in `call' 
/home/john/.gem/ruby/1.8/gems/rails-2.3.8/lib/rails/rack/static.rb:31:in `call' 
/home/john/.gem/ruby/1.8/gems/rails-2.3.8/lib/rails/rack/log_tailer.rb:17:in `call' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in `process_client' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `each' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `process_client' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `initialize' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `new' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `initialize' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `new' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `run' 
/home/john/.gem/ruby/1.8/gems/rails-2.3.8/lib/commands/server.rb:111 
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require' 
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require' 
script/server:3 

/var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1616:in `find_one' 
/var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1599:in `find_from_ids' 
/var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:619:in `find' 
/home/john/Websites/sandbox/testnoparams/app/controllers/categories_controller.rb:28:in `show' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:1331:in `send' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:1331:in `perform_action_without_filters' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/filters.rb:617:in `call_filters' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue' 
/var/lib/gems/1.8/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in `ms' 
/usr/lib/ruby/1.8/benchmark.rb:308:in `realtime' 
/var/lib/gems/1.8/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in `ms' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/rescue.rb:160:in `perform_action_without_flash' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/flash.rb:151:in `perform_action' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:532:in `send' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:532:in `process_without_filters' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/filters.rb:606:in `process' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:391:in `process' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:386:in `call' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/routing/route_set.rb:438:in `call' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:87:in `dispatch' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:121:in `_call' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:130 
/var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/query_cache.rb:29:in `call' 
/var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/query_cache.rb:29:in `call' 
/var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache' 
/var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/query_cache.rb:9:in `cache' 
/var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/query_cache.rb:28:in `call' 
/var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in `call' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/string_coercion.rb:25:in `call' 
/var/lib/gems/1.8/gems/rack-1.1.0/lib/rack/head.rb:9:in `call' 
/var/lib/gems/1.8/gems/rack-1.1.0/lib/rack/methodoverride.rb:24:in `call' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/params_parser.rb:15:in `call' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/session/cookie_store.rb:99:in `call' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/failsafe.rb:26:in `call' 
/var/lib/gems/1.8/gems/rack-1.1.0/lib/rack/lock.rb:11:in `call' 
/var/lib/gems/1.8/gems/rack-1.1.0/lib/rack/lock.rb:11:in `synchronize' 
/var/lib/gems/1.8/gems/rack-1.1.0/lib/rack/lock.rb:11:in `call' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:114:in `call' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/reloader.rb:34:in `run' 
/var/lib/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:108:in `call' 
/home/john/.gem/ruby/1.8/gems/rails-2.3.8/lib/rails/rack/static.rb:31:in `call' 
/var/lib/gems/1.8/gems/rack-1.1.0/lib/rack/urlmap.rb:47:in `call' 
/var/lib/gems/1.8/gems/rack-1.1.0/lib/rack/urlmap.rb:41:in `each' 
/var/lib/gems/1.8/gems/rack-1.1.0/lib/rack/urlmap.rb:41:in `call' 
/home/john/.gem/ruby/1.8/gems/rails-2.3.8/lib/rails/rack/log_tailer.rb:17:in `call' 
/var/lib/gems/1.8/gems/rack-1.1.0/lib/rack/content_length.rb:13:in `call' 
/var/lib/gems/1.8/gems/rack-1.1.0/lib/rack/chunked.rb:15:in `call' 
/var/lib/gems/1.8/gems/rack-1.1.0/lib/rack/handler/mongrel.rb:67:in `process' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in `process_client' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `each' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `process_client' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `initialize' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `new' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `initialize' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `new' 
/home/john/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `run' 
/var/lib/gems/1.8/gems/rack-1.1.0/lib/rack/handler/mongrel.rb:38:in `run' 
/home/john/.gem/ruby/1.8/gems/rails-2.3.8/lib/commands/server.rb:111 
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require' 
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require' 
script/server:3 

Request 

Parameters: 

{"id"=>"testaction"} 

Show session dump 

--- 

Response 

Headers: 

{"Content-Type"=>"", 
"Cache-Control"=>"no-cache"} 

回答

2

有你的默認路由,這是我常反正刪除:)一個錯字

map.connect ':controller/:actoin' 

應該

map.connect ':controller/:action' 

要明確地允許的行動的工作:

map.resources :categories, :collection => {:testaction => [:get, :post]} 

的[:得到,張貼]應該拉URL的首選方法所取代。它可以是一系列選項或單數符號。

參考號:http://api.rubyonrails.org/classes/ActionController/Resources.html

相關問題