【问题标题】:DRYing up scoped routes in Rails在 Rails 中干燥范围内的路线
【发布时间】:2014-02-06 00:28:49
【问题描述】:

在我的 Rails 4 应用程序中,我有以下路线:

scope ':section' do
  resources :articles
  resources :features
  resources :galleries
end

该部分在应用程序控制器中设置:

@current_section = params[:section]

在我看来,我使用文章(@channel),article_path(@channel,article)链接到页面。它有效,但我觉得它可能是 DRYer。有没有一种方法,一旦设置, :section 对所有路径助手都隐含?

【问题讨论】:

    标签: ruby-on-rails scope routes


    【解决方案1】:

    你试过了吗

    def default_url_options
      { section: @current_section }
    end
    

    ApplicationController?

    参见。 http://edgeguides.rubyonrails.org/action_controller_overview.html#default-url-options

    【讨论】:

    • 谢谢!这正是我所需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多