0

我正在使用 Ruby on Rails 构建一个 Web 应用程序,并且我想要一条用户可编辑的路线。所以我想添加一个简单的初始化程序来添加用户指定的路由,但我真的不知道我该怎么做。有什么简单的方法吗?我试过这个application.rb

config.after_initialize do
    Rails.application.reload_routes!
    Rails.logger.info "#{Rails.application.routes.routes.map(&:path)}"
    MyApp::Application.routes.draw do
        get '/droplet' => 'admin#index'
    end
end

但这似乎不起作用。这就是我的routes.rb

MyApp::Application.routes.draw do
    devise_for :users 

    root :to => 'target_finder#show_page'
    match '*path' => 'target_finder#show_page'

end

提前致谢!

4

2 回答 2

0

我建议看一下friendly_id gem,它可以根据用户的任何属性定义自定义路由,因此您可以拥有一个:url用户属性,然后通过添加slug索引然后将extend FriendlyIdfriendly_id :url, use: :slugged放入用户模型中将其用于路由。

于 2013-01-18T20:39:56.320 回答
0

如何制作一个接收参数的控制器操作,测试它们是否与配置的路由匹配,然后从那里采取行动。

MyApp::Application.routes.draw do
    devise_for :users 

    root :to => 'target_finder#show_page'
    match '*path' => 'test_if_custom#CustomController'

end

class CustomController < ActionController::Base

  def test_if_custom
    if url_for(params) == Rails.application.routes.routes.map(&:path)
      take_custom_action
    else
      take_default_action # Redirects or errors, maybe.
    end
  end

end
于 2013-01-18T20:40:30.443 回答