【问题标题】:How to fix error with period in error in my rails app?如何在我的 Rails 应用程序中修复错误期间的错误?
【发布时间】:2013-05-07 11:19:11
【问题描述】:

我有以下 javascript,它接受 2 个变量(chapterid 和 txt)并将它们用作 url 中的参数。 'txt' 来自用户选择。

我的问题:当用户选择中有句点时,routes.rb 文件会出现匹配 url 的问题,或者可能会忽略句点之后的所有内容。

例子:

routes.rb

match "readers/chapannotations/new/:chapterid/:selection/"  => "readers#newchapannotation"

javascript

      url = '/readers/chapannotations/new/' + chapterid + '/' + txt + '/';
      console.log(url);
      window.location = url;


txt = 'O Arjuna.'
URL that opens: http://mysite.com/readers/chapannotations/new/5/O%20Arjuna./

这会导致没有 URL 被打开,因为在 routes.rb 中没有匹配的 URL。我相信这是因为“。”在 txt 的末尾。

txt = '阿朱那' http://mysite.com/readers/chapannotations/new/5/O%20Arjuna/

这个网址打开就好了,我相信是因为没有句号。

【问题讨论】:

    标签: ruby-on-rails url routing


    【解决方案1】:

    你可以在你的routes.rb试试这个:

    match "readers/chapannotations/new/:chapterid/:selection/"  => "readers#newchapannotation", :selection => /.*/
    

    【讨论】:

    • 当我选择带有句点不匹配的文本时,我仍然收到错误消息。
    • 你的 Rails 版本是什么?我在这里对其进行了测试,并在 rails 3.2.13 上完美运行
    • 嗯,这很奇怪,我也有相同的版本。
    • 试试这个match "readers/chapannotations/new/:chapterid/:selection/" => "readers#newchapannotation", :constraints => { :selection => /.*/ } 并重新启动你的服务器(这应该不是必需的,但值得一试)
    猜你喜欢
    • 2013-05-20
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2020-11-29
    相关资源
    最近更新 更多