【问题标题】:Custom Rails 3 routes自定义 Rails 3 条路线
【发布时间】:2012-04-04 21:33:57
【问题描述】:

假设我有一个events 控制器。如果我通过GET 请求/events,它将在新闻控制器中以index 操作响应,此视图将显示未来的事件。

我想添加一个名为past 的新操作,它类似于index,它返回一个Events 数组,但带有另一个查询。

我已经添加到routes.rb这个块:

resources :events do
  collection do
    get :past
  end
end

我把这个夹到events_controller:

def history
  @events => Event.past
  render :template => 'index'
end

但是当我在浏览器中请求http://127.0.0.1:3000/events/past 时它不起作用,日志中会抛出这些行:

Started GET "/events/past" for 127.0.0.1 at 2012-04-02 19:32:01 -0500
  Processing by EventsController#show as HTML
  Parameters: {"id"=>"past"}

最后你有rake routes 输出:

            events GET    /events(.:format)                  {:action=>"index", :controller=>"events"}
             event GET    /events/:id(.:format)              {:action=>"show", :controller=>"events"}
       past_events GET    /events/past(.:format)             {:action=>"past", :controller=>"events"}
  contact_us_event POST   /events/:id/contact_us(.:format)   {:action=>"contact_us", :controller=>"events"}
                   GET    /events(.:format)                  {:action=>"index", :controller=>"events"}
                   POST   /events(.:format)                  {:action=>"create", :controller=>"events"}
         new_event GET    /events/new(.:format)              {:action=>"new", :controller=>"events"}
        edit_event GET    /events/:id/edit(.:format)         {:action=>"edit", :controller=>"events"}
                   GET    /events/:id(.:format)              {:action=>"show", :controller=>"events"}
                   PUT    /events/:id(.:format)              {:action=>"update", :controller=>"events"}
                   DELETE /events/:id(.:format)              {:action=>"destroy", :controller=>"events"}

其他测试是使用 Rails 控制台检查路线:

$ script/rails c
1.9.2-p318 :001 > rs = ActionController::Routing::Routes
1.9.2-p318 :002 > rs.recognize_path "/events/past", :method => :get
 => {:action=>"show", :controller=>"events", :id=>"past"} 

怎么了?

【问题讨论】:

  • rake routes 命令向您显示事件路线的哪些内容?
  • @mguymon 嗨,我已将rake routes 输出添加到帖子中。谢谢。
  • 您说您输入了get :past,但rake routes 却显示history。你确定不是get :history?
  • @BrandonTilley 对不起,这是一个错字,因为我需要手动编写。
  • 所以这是基本的 Rails 管道,您的代码看起来有效。从路由信息和历史方法中可以明显看出,显示的代码实际上并不是使用的并且存在问题。

标签: ruby-on-rails-3 routes


【解决方案1】:

问题出在路由优先级上,这里有一个有效的 routes.rb 块:

resources :events, :only => [:index, :show] do
  get 'past', :on => :collection
  post 'contact_us', :on => :member
end

【讨论】:

    【解决方案2】:

    试试:

    resources :events do
      get :past, :to => "events#past"
    end
    

    我认为您不需要集合上下文。 (而且你可能不需要 :to 部分)

    更新

    另一种方法是将事件调用放在资源之上。假设你不需要它的集合。

    get 'events/past', :to => "events#past"
    resources :events
    

    应该为您提供正确的路线。

    供参考:Rails 3 routes: How to avoid conflict with 'show' action?

    【讨论】:

    • 感谢您的回答。您的建议生成此路由规则:event_past GET /events/:event_id/past {:action=>"past", :controller=>"events"} 我想要这样的东西:past_event GET /events/past(.:format) {:action=>"past", :controller=>"events"} 或类似默认索引路由的东西,它可以获取数据库中的所有模型:events GET /events(.:format) {:action=>"index", :controller=>"events"} 主要思想是列出index 中的事件。
    • 哦,是的,忘记了上面的 :id。抱歉,来晚了。您是否尝试过直接使用get 'events/past', :to => "events#past"?
    猜你喜欢
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    相关资源
    最近更新 更多