我正在使用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
提前致谢!
答案 0 :(得分:0)
我建议查看friendly_id gem,它可以从用户的任何属性定义自定义路由,因此您可以拥有:url
用户属性,然后通过添加将其用于路径slug
索引,然后将extend FriendlyId
和friendly_id :url, use: :slugged
放入用户模型中。
答案 1 :(得分: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