加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 07:08:53 所属栏目:百科 来源:网络整理
导读:我正在尝试使用带有{{id}}的命名路由作为其中一个参数,允许由Handlebars使用渲染的内容. url_for正在转义参数,因此生成的网址包含{{id}}.我试过添加:escape =对于通话是假的,但它没有效果. 的routes.rb resources :rants,:except = [:show] do post '/votes
我正在尝试使用带有{{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
    "{{#{$1}}}"
  end
end

handlebar_path :rant_vote,:rant_id => '{{id}}',:vote => 'up'

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读