简体   繁体   English

Rails路线-来自宝石的动态-无法修改冻结的字符串

[英]Rails Routes - Dynamic from Gems - can't modify frozen String

I have a Rails app (Rails 3.2.9/Ruby 1.9.3) which consists of several small parts (Once it was one big single app, but i was necessary to split it up into components). 我有一个Rails应用程序(Rails 3.2.9 / Ruby 1.9.3),它由几个小部分组成(曾经是一个大的单个应用程序,但是我有必要将其拆分为多个组件)。

Now I've got several parts of the app, loaded as gems into a main app. 现在,我已经拥有了该应用程序的几个部分,它们都是作为宝石加载到主应用程序中的。

I'm trying to load the routes to the parts of the app dynamically, also i try to prevent URL-Hacking, that's why i would like to create all the available routes dynamically and put the controller#action part into a SHA1 string like that: 我正在尝试动态加载到应用程序各部分的路由,也尝试防止URL黑客攻击,这就是为什么我想动态创建所有可用路由,并将controller#action部分放入这样的SHA1字符串中的原因:

get   "/#{controller+action as SHA!}/:id", :controller => c_name, :action => ac

The whole code in one of my components looks like this (in routes.rb): 我的组件之一中的整个代码如下所示(在routes.rb中):

  Rails.application.routes.draw do
    BootLog.info "loading -> Smartconcepts#Concept routes"  
    SmartconceptsConcept::SmartconceptsConceptEngine::LOADED_CONTROLLERS.each do |c_name|
      c_controller = c_name.camelize

      c_name = c_name.gsub("_controller", "")


      hex_name = Digest::SHA1.hexdigest(c_name)
      real_controller_actions = c_controller.constantize.action_methods

      BootLog.info " -- register: #{c_name} as:     #{hex_name}"
      BootLog.info " --- #{real_controller_actions.size} actions"

      # Default
      get   "/#{hex_name}/:action(.:id)", :controller => c_name
      post  "/#{hex_name}/:action(.:id)", :controller => c_name

      real_controller_actions.each do |ac|

        a_hex_name = Digest::SHA1.hexdigest((ac+c_name))

        BootLog.info " ----  #{c_name}##{ac} as:      #{a_hex_name}"

        get   "/#{a_hex_name}/:id", :controller => c_name, :action => ac
        post  "/#{a_hex_name}/:id", :controller => c_name, :action => ac
      end  


    end 


  end

so... 所以...

SmartconceptsConcept::SmartconceptsConceptEngine::LOADED_CONTROLLERS

is loading very fine from my engine, loading the action_methods also works fine. 从我的引擎加载得很好,加载action_methods也可以。

rake routes

exactly shows what i expect but when loading pages in browsers I get 完全显示了我的期望,但是在浏览器中加载页面时却得到了

RuntimeError (can't modify frozen String):

which is obviously caused by loading routes the WAY I do? 这显然是由我的装载路线引起的?

What can I do to make it work (disable class caching didn't solve it) 我该怎么做才能使其正常工作(禁用类缓存无法解决问题)

problem where the loaded controller action names. 加载的控制器动作名称所在的问题。

using .dup finally did it. 使用.dup终于做到了。

real_controller_actions = c_controller.constantize.action_methods
#...
real_controller_actions.each do |ac|
    a_hex_name = Digest::SHA1.hexdigest((ac.dup+c_name))
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM