如何在 Rails 3.2 中动态添加路由 [英] How to dynamically add routes in Rails 3.2

查看:40
本文介绍了如何在 Rails 3.2 中动态添加路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rails 应用中使用 acts_as_commentable 来允许对不同类型的模型发表评论.>

我有一个多态的 CommentsController ala the 多态关联 Railscast.

我正在尝试为这个控制器编写规范;但是,我想使用 acts_as_fu 来定义一个通用的 Commentable 类在规范中使用的控制器.这样它就不会绑定到我们的具体可评论模型之一.

问题是,当我尝试测试控制器操作时,我收到路由错误,因为我使用 act_as_fu 创建的 Commentable 类没有路由.

我需要一种方法来为规范在 before(:all)(顺便使用 RSpec)中为这个动态创建的模型绘制路线.

这是我目前的规格:

describe CommentsController do之前(:全部)做# 使用acts_as_fu 创建通用的Commentable 类build_model : 可评论做act_as_commentable :comment归属地:用户attr_accessible : 用户结尾# TODO - 初始化可评论路由结尾结尾

更新:找到了一个hacky"的解决方案.不过,我想知道是否有更简洁的方法来做到这一点.

解决方案

找到了 在运行时在 Rails 3 中添加路由,虽然有点麻烦:

describe CommentsController do之前(:全部)做# 使用acts_as_fu 创建通用的Commentable 类build_model : 可评论做act_as_commentable :comment归属地:用户attr_accessible : 用户结尾#哈奇哈奇开始_routes = Rails.application.routes_routes.disable_clear_and_finalize = true_routes.clear!Rails.application.routes_reloader.paths.each{ |path|加载(路径)}_routes.draw 做# 初始化可评论路由资源:可评论做# 评论管理的路由资源:评论结尾结尾ActiveSupport.on_load(:action_controller) { _routes.finalize!}确保_routes.disable_clear_and_finalize = false结尾结尾结尾

I'm using acts_as_commentable in a Rails app to allow commenting on different types of models.

I've got a polymorphic CommentsController ala the Polymorphic Association Railscast.

I'm trying to write specs for this controller; however, I'd like to use acts_as_fu to define a generic Commentable class for the controller to use in the specs. This way its not tied to one of our concrete commentable models.

The problem is that when I try to test the controller actions I get routing errors because there are no routes for the Commentable class I created using acts_as_fu.

I need a way to draw the routes for this dynamically created model in a before(:all) (using RSpec by the way) for the specs.

Here's what my spec looks like so far:

describe CommentsController do
  before(:all) do
    # Using acts_as_fu to create generic Commentable class
    build_model :commentable do
      acts_as_commentable :comment
      belongs_to :user
      attr_accessible :user
    end

    # TODO - Initialize Commentable routes
  end
end

UPDATE: Found a 'hacky' solution. I'm wondering if there's a cleaner way of doing this though.

解决方案

Found a solution for adding routes at runtime in Rails 3, albeit a hacky one:

describe CommentsController do
  before(:all) do
    # Using acts_as_fu to create generic Commentable class
    build_model :commentable do
      acts_as_commentable :comment
      belongs_to :user
      attr_accessible :user
    end

    # Hacky hacky
    begin
      _routes = Rails.application.routes
      _routes.disable_clear_and_finalize = true
      _routes.clear!
      Rails.application.routes_reloader.paths.each{ |path| load(path) }
      _routes.draw do
        # Initialize Commentable routes    
        resources :commentable do
          # Routes for comment management
          resources :comments
        end
      end
      ActiveSupport.on_load(:action_controller) { _routes.finalize! }
    ensure
      _routes.disable_clear_and_finalize = false
    end
  end
end

这篇关于如何在 Rails 3.2 中动态添加路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆