我在小项目上检查水银编辑器https://github.com/jejacks0n/mercury。
这是我的routes.rb文件
Myapp::Application.routes.draw do
mount Mercury::Engine => '/'
scope '(:locale)' do
resources :post
end
end
我的帖子网址是:
http://localhost:3000/es/posts/1
http://localhost:3000/en/posts/2
http://localhost:3000/de/posts/3
.
.
.
我的水银路线:
Routes for Mercury::Engine:
mercury_editor /editor(/*requested_uri)(.:format) mercury#edit
/mercury/:type/:resource(.:format) mercury#resource
/mercury/snippets/:name/options(.:format) mercury#snippet_options
/mercury/snippets/:name/preview(.:format) mercury#snippet_preview
我试着像:
<%= link_to 'Edit', "/editor" + request.path %>
但我收到了错误的网址http://localhost:3000/editor/es/posts/2
。
有人可以告诉我如何为我的路线添加指定路径,例如:
http://localhost:3000/es/editor/posts/1
或http://localhost:3000/editor/posts/1
答案 0 :(得分:0)
将<%= link_to 'Edit', "/editor" + request.path %>
替换为
<%= link_to 'Edit', request.path.gsub(/^\/((\w)+)/, '/\1/editor') %>
获取http://localhost:3000/es/editor/posts/1
或
将<%= link_to 'Edit', "/editor" + request.path %>
替换为
<%= link_to 'Edit', request.path.gsub(/^\/((\w)+)/, '/editor') %>
获取http://localhost:3000/editor/posts/1
甚至可以定义一个辅助方法,如
def mercuryfied_url(with_locale = true)
if with_locale
request.path.gsub(/^\/((\w)+)/, '/\1/editor')
else
request.path.gsub(/^\/((\w)+)/, '/editor')
end
end
然后致电
<%= link_to 'Edit', mercuryfied_url %>
获取http://localhost:3000/es/editor/posts/1
或者
<%= link_to 'Edit', mercuryfied_url(false) %>
获取http://localhost:3000/editor/posts/1
希望有所帮助:)