Ruby 如何在Roda中划分嵌套管线
我使用Roda编写了一个应用程序。 我有一个这样的嵌套路由器:Ruby 如何在Roda中划分嵌套管线,ruby,roda,Ruby,Roda,我使用Roda编写了一个应用程序。 我有一个这样的嵌套路由器: route 'chats' do |r| env['warden'].authenticate! r.is do @current_user = env['warden'].user r.get do # code is here end r.post do # code is here
route 'chats' do |r|
env['warden'].authenticate!
r.is do
@current_user = env['warden'].user
r.get do
# code is here
end
r.post do
# code is here
end
end
r.on :String do |chat_id|
r.is 'messages' do
# code is here
r.get do
# code is here
end
r.post do
# code is here
end
end
end
end
route 'chats' do |r|
# code is here
end
route 'chats/:chat_id/messages' do |r, chat_id|
# code is here
end
我想将一个大代码块分成两条路径,如下所示:
route 'chats' do |r|
env['warden'].authenticate!
r.is do
@current_user = env['warden'].user
r.get do
# code is here
end
r.post do
# code is here
end
end
r.on :String do |chat_id|
r.is 'messages' do
# code is here
r.get do
# code is here
end
r.post do
# code is here
end
end
end
end
route 'chats' do |r|
# code is here
end
route 'chats/:chat_id/messages' do |r, chat_id|
# code is here
end
请帮忙。如何正确操作?您需要启用两个插件:
- -允许在单独的文件之间拆分路由
- -沿路线传递变量
# routes/root.rb
class YourApp::Web
route do |r|
r.on('chats') do
r.is do
r.route 'chats'
end
r.is(String, 'messages') do |chat_id|
shared[:chat_id] = chat_id
r.route 'chats_messages'
end
end
end
end
之后,您可以将聊天和聊天信息放入单独的文件中:
# routes/chats.rb
class YourApp::Web
route ('chats') do |r|
r.get do
# ....
end
r.post do
# ....
end
end
end
# routes/chats_messages.rb
class YourApp::Web
route ('chats_messages') do |r|
chat_id = shared[:chat_id]
r.get do
# ....
end
r.post do
# ....
end
end
end
也许还有其他解决办法。我分享了对我有用的东西。希望有帮助 谢谢大家!!我会试试的