2013-03-01 62 views
2

我正在检查汞编辑器https://github.com/jejacks0n/mercury小项目。范围与汞编辑器+导轨3

这些是我的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/1http://localhost:3000/editor/posts/1

+0

你是什么意思 “我得到一个错误的URL” 是什么意思?这个网址是否有效,但你不喜欢它的外观?或者,网址不工作? 为了什么是值得的,我只是在同一个案例中,你一直在做,并最终做了你做的。而且这个网址'http:// localhost:3000/editor/es/posts/2'都可以正常工作 – 2013-05-18 12:41:06

回答

0

<%= link_to 'Edit', request.path.gsub(/^\/((\w)+)/, '/\1/editor') %> 

更换<%= link_to 'Edit', "/editor" + request.path %>得到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 

一个helper方法,然后调用

<%= link_to 'Edit', mercuryfied_url %> 

得到http://localhost:3000/es/editor/posts/1

或者

<%= link_to 'Edit', mercuryfied_url(false) %> 

得到http://localhost:3000/editor/posts/1

希望帮助:)