【问题标题】:javascript popup routes to wrong URI [rails]javascript弹出路由到错误的URI [rails]
【发布时间】:2013-01-13 20:49:07
【问题描述】:

我有一个创建弹出窗口的 link_to。它适用于某些页面,但是当我在其他页面上单击它时,它会转到错误的 URI。我的应用架构有问题吗?我希望所有链接都路由到

http://localhost:3000/microposts/361/into_it.

SHARED/_feed_item.html.erb---这行代码路由正确:

<%= link_to "#{feed_item.votes_for} Into it!", into_it_micropost_path(feed_item.id), :onclick => "javascript:window.open('microposts/#{feed_item.id}/into_it','popup','width=350,height=300,top=315,left=200');", id: "feeditemlikes_#{feed_item.id}", remote: true %>

用户/_vote_feed_item.html.erb--- 在类似页面上,它会路由到

http://localhost:3000/users/1/microposts/361/into_it

<%= link_to "#{vote_feed_item.votes_for} Into it!", into_it_micropost_path(vote_feed_item.id), :onclick => "javascript:window.open('microposts/#{vote_feed_item.id}/into_it','popup','width=350,height=300,top=315,left=200');", id: "feeditemlikes_#{vote_feed_item.id}", remote: true %>

MICROPOSTS/_micropost.html.erb---在配置文件页面上它路由到

http://localhost:3000/users/microposts/361/into_it

<%= link_to "#{micropost.votes_for} Into it!", into_it_micropost_path(micropost.id), :onclick => "javascript:window.open('microposts/#{micropost.id}/into_it','popup','width=350,height=300,top=315,left=200');", id: "feeditemlikes_#{micropost.id}", remote: true %>

实际上,问题似乎出在 javascript 弹出代码上,有什么提示吗?

【问题讨论】:

  • 你需要'remote: true'吗?
  • 我需要它,所以页面不会刷新/更改

标签: javascript ruby-on-rails routes link-to


【解决方案1】:

如果我理解正确,你总是想链接到http://localhost:3000/microposts/361/into_it

那么你应该在你的路径前加上一个斜杠:

"javascript:window.open('/microposts/#{micropost.id}/into_it', ..."

或者更好的是,使用 rails path helper,如下所示:

"javascript:window.open(#{into_it_path(micropost)}, ..."

【讨论】:

    猜你喜欢
    • 2016-02-15
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 2012-05-22
    • 2012-07-11
    • 1970-01-01
    相关资源
    最近更新 更多