【问题标题】:prefix param rails routes前缀参数 rails 路线
【发布时间】:2013-07-24 20:01:35
【问题描述】:

我正在使用 rails3,我想要一个如下所示的路由前缀:

我的路线中有以下内容:

require 'resque/server'

Woofound::Application.routes.draw do
  scope "/:my_param" do
    resource :users
  end
end

然后我的应用程序控制器中有以下内容:

class ApplicationController < ActionController::Base
   before_filter :check_param

   private
   def check_param
     unless Myobject.find_by_param(param[:my_param])
       raise "You must add this param"
     end
   end
end

然后在我的路线中,我希望能够访问 user_path(@user) 而不必这样做

user_path(:param_for_route_name_spacing, @user)

如果您需要更清晰的信息,请告诉我。

简而言之,我希望我的路由有一个参数化前缀,参数会自动传递回参数化命名空间,而无需将其作为第一个参数添加到 am 对象路径。

【问题讨论】:

    标签: ruby-on-rails routes prefix


    【解决方案1】:

    试试

    class ApplicationController < ActionController::Base
      def user_path(user)
        super(:param_for_route_name_spacing, user)
      end
    end
    

    【讨论】:

    • 好的,你的路由文件应该有resources :users而不是resource :users,并在控制器中调用helper_method :user_path
    • 我不小心把一个 :as => :something_else 放在我的路由中 你的修复很完美,非常感谢
    • 我最终使用了default_url_options guides.rubyonrails.org/…
    猜你喜欢
    • 2011-06-11
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 2010-10-25
    • 2016-08-05
    相关资源
    最近更新 更多