ruby-on-rails – 带有导轨的把手胡须url_for

ruby-on-rails – 带有导轨的把手胡须url_for,第1张

概述我正在尝试使用带有{{id}}的命名路由作为其中一个参数,允许由Handlebars使用渲染的内容. url_for正在转义参数,因此生成的网址包含{{id}}.我试过添加:escape =>对于通话是假的,但它没有效果. 的routes.rb resources :rants, :except => [:show] do post '/votes/:vote', :controller => 我正在尝试使用带有{{ID}}的命名路由作为其中一个参数,允许由Handlebars使用渲染的内容. url_for正在转义参数,因此生成的网址包含{{ID}}.我试过添加:escape =>对于通话是假的,但它没有效果.

的routes.rb

resources :rants,:except => [:show] do  post '/Votes/:Vote',:controller => 'Votes',:action => 'create',:as => 'Vote'end

index.haml

%script{:ID => 'Vote_template',:type => 'text/x-handlebars-template'}  .Votes    = link_to 'up',rant_Vote_path(:rant_ID => '{{ID}}',:Vote => 'up')    %span {{Votes}}    = link_to 'down',:Vote => 'down')

的application.Js

var Vote_template =  Handlebars.compile($('#Vote_template').HTML());

产量

<script ID="Vote_template" type="text/x-handlebars-template">  <div class='Votes'>    <a href="/rants/%7B%7BID%7D%7D/Votes/up">up</a>    <span>{{Votes}}</span>    <a href="/rants/%7B%7BID%7D%7D/Votes/down">down</a>  </div></script>

为了便于阅读,我简化了这个例子,但问题仍然存在;有没有办法使用{{}}作为参数的命名路由?我知道我可以做link_to’up’,’/ rants / {{ID}} / Vote / up’所以请不要提供它作为答案.

解决方法 问题是小胡子字符在URL中无效并且正在被转义.我建议创建一个包装器.

def handlebar_path(helper,arguments={})  send("#{helper}_path",arguments).gsub(/%7B%7B(.+)%7D%7D/) do    "{{#{}}}"  endendhandlebar_path :rant_Vote,:rant_ID => '{{ID}}',:Vote => 'up'
总结

以上是内存溢出为你收集整理的ruby-on-rails – 带有导轨把手/胡须url_for全部内容,希望文章能够帮你解决ruby-on-rails – 带有导轨的把手/胡须url_for所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1272300.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存