如何使 Rails 中的链接散列化?

发布于 2024-11-04 04:30:01 字数 565 浏览 5 评论 0原文

在我的 Rails 应用程序中,我使用 jQuery BBQ 进行 hashchange 深度链接等...

这是我在 Rails 中现有的链接:

<%= link_to('View on Site ', project_topic_url(@project.id, @topic, :only_path => false),) %>

我在 user_mailer 中工作,通过电子邮件发送...

问题是这样生成的: http://www.site.com/project/1/topic/23

我想要的是: http://www.site.com/#/project/1/topic/23

关于如何使 url 中的哈希 # 成为 ajax、深度链接友好的任何想法?

谢谢

in my rails app I'm using jQuery BBQ for hashchange deeplinking etc...

Here are my existing links in rails:

<%= link_to('View on Site ', project_topic_url(@project.id, @topic, :only_path => false),) %>

I have this working in a user_mailer that gets emailed out...

problem is this generated: http://www.site.com/project/1/topic/23

And What I want is: http://www.site.com/#/project/1/topic/23

Any ideas on how I can get the hash # in the url to be ajax, deeplinking friendly?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独闯女儿国 2024-11-11 04:30:01

您始终可以创建一个辅助方法来为您处理这个问题。

def project_topic_ajax(id, topic)
   "http://www.site.com/#/project/#{id}/#{topic.name}/#{topic.id}"
end

然后只需调用它而不是默认的链接助手。

更新:我为您找到了解决方案。

<%= link_to "View on Site", "##{project_topic_url(@project.id, @topic)}" %>

它更干净,但我意识到这并不完全是您想要的。

对于绝对路径,它看起来像这样(有点脏):

<%= link_to "View on Site", "http://www.site.com/##{project_topic_url(@project.id, @topic)}" %>

You could always create a helper method to handle this for you.

def project_topic_ajax(id, topic)
   "http://www.site.com/#/project/#{id}/#{topic.name}/#{topic.id}"
end

Then just call that instead of the default link helper.

Update: I found a solution for you.

<%= link_to "View on Site", "##{project_topic_url(@project.id, @topic)}" %>

It's cleaner, but not exactly what you were looking for I realize.

For absolute paths, it would look like this (a bit dirtier):

<%= link_to "View on Site", "http://www.site.com/##{project_topic_url(@project.id, @topic)}" %>
呆萌少年 2024-11-11 04:30:01
<%= link_to 'View on Site ', [project_topic_url(@project.id, @topic).gsub(/#{project_topic_path(@project.id, @topic)}/, ""), project_topic_path(@project.id, @topic)].joins("#") %>

而且你最好把它包装成一个助手

<%= link_to 'View on Site ', [project_topic_url(@project.id, @topic).gsub(/#{project_topic_path(@project.id, @topic)}/, ""), project_topic_path(@project.id, @topic)].joins("#") %>

And you'd better to wrap it as a helper

来世叙缘 2024-11-11 04:30:01

也许有点晚了;但是您可以将整个路由文件打包在哈希范围内,如下所示:

MyRails::Application.routes.draw do
  scope :hash, :path => "/#" do
    resources :project do
      resources :topic
      # ...
    end
    # ...
  end
end

使用 Rails 3.2.8,运行rake paths 它会产生所需的结果。

不确定这会如何影响您的观点或 jQuery-BBQ

bit late perhaps; but you could pack your entire routes file in a hash scope like so:

MyRails::Application.routes.draw do
  scope :hash, :path => "/#" do
    resources :project do
      resources :topic
      # ...
    end
    # ...
  end
end

With Rails 3.2.8, running rake routes it yielded the desired results.

Not sure how this would affect your views or jQuery-BBQ for that matter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文