Rails 4.1.2-to_param转义斜杠(并中断应用程序) [英] Rails 4.1.2 - to_param escapes slashes (and breaks app)

查看:98
本文介绍了Rails 4.1.2-to_param转义斜杠(并中断应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序to_param中使用它来创建自定义URL(此自定义路径包含斜杠):

I use in my app to_param to create custom URL (this custom path contains slashes):

class Machine < ActiveRecord::Base
  def to_param
    MachinePrettyPath.show_path(self, cut_model_text: true)
  end
end

事实是,由于更改了Rails 4.1.2行为,并且Rails不允许在URL中使用斜杠(使用自定义URL时),因此它会转义斜杠.

The thing is, that since Rails 4.1.2 behaviour changed and Rails doesn't allow to use slashes in the URL (when use custom URL), so it escapes slashes.

我有这样的路线:

Rails.application.routes.draw do
  scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
      resources :machines, except: :destroy do
          collection do
            get  :search
            get  'search/:ad_type(/:machine_type(/:machine_subtype(/:brand)))', action: 'search', as: :pretty_search

            get  ':subcategory/:brand(/:model)/:id', action: 'show', as: :pretty
            patch ':subcategory/:brand(/:model)/:id', action: 'update'                                  # To be able to update machines with new rich paths.
          end
      end
  end
end

我通过在线程中的推荐尝试了 以仅使用glob参数显示方法以确保其有效:

I tried by recommendation in the thread to use glob param just for show method to make sure it works:

resources :machines, except: :destroy do
 #...
end

scope format: false do
 get '/machines/*id', to: "machines#show"
end

但这绝对不起作用.我仍然有这样断开的链接:

But it absolutely doesn't work. I still have such broken links:

http://localhost:3000/machines/tractor%2Fminitractor%2Fmodel1%2F405

当然,如果我替换自己身上的转义斜杠:

Of course, if I replace escaped slashes on myself:

http://localhost:3000/machines/tractor/minitractor/model1/405

尝试访问路径,然后将打开页面.

And try to visit path, then page'll be opened.

任何想法我该如何解决?

Any ideas how can I fix that?

推荐答案

在使用自动生成的url辅助程序时,我遇到了同样的问题.我使用调试器将新行为追踪到其源代码(在ActionDispatch :: Journey :: Visitors :: Formatter附近),但是没有找到任何有前途的解决方案.看来参数化模型现在已被严格视为路径的单个斜杠分隔段,并相应地进行了转义,没有其他选项可以告诉格式化程序.

I've been having the same problem when using the auto-generated url helpers. I used a debugger to trace the new behavior to its source (somewhere around ActionDispatch::Journey::Visitors::Formatter), but didn't find any promising solutions. It looks like the parameterized model is now strictly treated as a single slash-delimited segment of the path and escaped accordingly, with no options to tell the formatter otherwise.

据我所知,获取url帮助器产生旧结果的唯一方法是使用原始的routes文件并分别传递每个段,例如:

As far as I can tell, the only way to get the url helper to produce the old result is to use your original routes file and pass each segment separately, something like:

pretty_machine_path(machine.subcategory, machine.brand, machine.model, machine.id)

这真是丑陋的地狱,显然你不想一遍又一遍地做.您可以在MachinePrettyPath中添加一个方法,以将分段作为数组生成,并为帮助程序爆炸结果(例如pretty_machine_path(*MachinePrettyPath.show_path_segments(machine))),但这仍然很冗长.

This is ugly as hell and obviously not something you'll want to do over and over. You could add a method to MachinePrettyPath to generate the segments as an array and explode the result for the helper (say, pretty_machine_path(*MachinePrettyPath.show_path_segments(machine))) but that's still pretty verbose.

在上述头痛与链接到的Rails票证中的开发人员的您做错了"的态度之间,对我而言,最简单的选择是硬着头皮编写一个自定义URL助手,而不是使用to_param.我还没有找到一个很好的例子来说明正确"的方法,但是像这样的简单例子应该可以达到目的:

Between the above headaches and the "You're Doing it Wrong" attitude from the devs in that Rails ticket you linked to, the simplest option for me was to bite the bullet and write a custom URL helper instead of using to_param. I've yet to find a good example of the "right" way to do that, but something like this bare-bones example should serve the purpose:

#app/helpers/urls_helper.rb
module UrlsHelper
  def machine_path(machine, options = {})
    pretty_machine_path(*MachinePrettyPath.show_path_segments(machine), options)
  end
end

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  helper :urls #for the views
  include UrlsHelper #for controllers
  #...
end

这篇关于Rails 4.1.2-to_param转义斜杠(并中断应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆