【问题标题】:scope on routes with mercury editor + rails 3使用水银编辑器 + 轨道 3 的路线范围
【发布时间】:2013-03-01 14:15:47
【问题描述】:

我正在检查小项目的水星编辑器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/1http://localhost:3000/editor/posts/1

【问题讨论】:

  • “我的网址错误”是什么意思?这个网址是否有效,但您不喜欢它的外观?还是网址根本不起作用?对于它的价值,我刚刚经历过和你一样的情况,最终做了你所做的事情。使用该网址 http://localhost:3000/editor/es/posts/2 一切正常

标签: jquery ruby-on-rails ruby-on-rails-3 mercury-editor


【解决方案1】:

&lt;%= link_to 'Edit', "/editor" + request.path %&gt; 替换为

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

获取http://localhost:3000/es/editor/posts/1

或者

&lt;%= link_to 'Edit', "/editor" + request.path %&gt; 替换为

<%= 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

希望有帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多