作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望能够根据我的数据库中的信息动态地将 URL 映射到 Controller 。
我希望做一些功能上与此等效的事情(假设是 View
模型):
map.route '/:view_name',
:controller => lambda { View.find_by_name(params[:view_name]).controller }
最佳答案
这个问题很老了,但我发现它很有趣。可以在 Rails 3 中使用路由器的功能来创建一个完整的解决方案来路由到 Rack 端点。
创建以下 Rack 类:
class MyRouter
def call(env)
# Matched from routes, you can access all matched parameters
view_name= env['action_dispatch.request.path_parameters'][:view_name]
# Compute these the way you like, possibly using view_name
controller= 'post'
my_action= 'show'
controller_class= (controller + '_controller').camelize.constantize
controller_class.action(my_action.to_sym).call(env)
end
end
match '/:view_name', :to => MyRouter.new, :via => :get
关于ruby-on-rails - 动态 URL -> Rails 中路由的 Controller 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2596312/
我是一名优秀的程序员,十分优秀!