开发者

How to make links in rails hashed?

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


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)}" %>


<%= 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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜