2

我正在检查小项目的水银编辑器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

4

1 回答 1

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

希望有帮助:)

于 2013-06-05T06:21:40.237 回答