这可能吗?干净的URL在to_param中使用正斜杠'/'与Rails(3)
def to_param
"#{id}%2F#{slug}"
end
这适用于Chrome和Safari,但是如果Firefox在地址栏中看到“%2F”。有更清洁的方法吗?
这可能吗?干净的URL在to_param中使用正斜杠'/'与Rails(3)
def to_param
"#{id}%2F#{slug}"
end
这适用于Chrome和Safari,但是如果Firefox在地址栏中看到“%2F”。有更清洁的方法吗?
在节中的“重定向方法”这确实是一个古老的职位,但我想要建立一点。
如果你不想在你的PARAMS来处理废料的变量,你真的需要定义模型
def to_param "#{id}/#{title}" end
该方法to_param并设置路径,像这样:
resources :posts, :id => /[0-9]+\/.+/
这样,你的链接定义长得挺像一个正常:
link_to post.title, post_url(post)
很简单:http://www.miguelsanmiguel.com/2011/03/17/slug-that-slash
在rails 3中是否有任何可能会导致此问题的内容?我无法使用您的确切代码来运行它。 – brittohalloran 2012-06-13 15:15:41
顺便说一句 - 它在'to_param'方法中使用“ - ”或“_”很好,但当我去“/”时会中断(我当然必须相应地编辑正则表达式) – brittohalloran 2012-06-13 15:28:07
首先:上面的代码运行在Rails3中。第二:在你的slu having(帖子标题)中有一个斜杠不是一个好主意,因为它代表了一个URL的更深层次,并且它与嵌套资源不兼容,所以你应该以某种方式替代它。实际上,我们应该使用任何[保留的URL字符](http://www.blooberry.com/indexdot/html/topics/urlencoding.htm)。我建议像这样:'def to_param; “#{ID}/{#} name_without_reserved_url_characters”; end'。好点! – miguelsan 2012-06-25 14:59:32
看看Friendly ID - 它会完全消除ID,只是使用slu。。 Rails 3也是兼容的。
好,我想这只是那么现在就需要这样的:
def to_param
"#{id}/#{slug}"
end
在routes.rb中(更换你想要什么和你需要什么)
match "/expenses/:id/:slug" => "expenses#show", :as => :expense
你的link_to应该像这个现在
= link_to "something", expense_url(:id => expense.id, :slug => expense.slug)
希望这有助于有点
也许这样的事情可以帮助
match "/foo/:id", :to => redirect("/bar/%{id}s")
你看看这篇文章Rails3中和路线
http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/
你究竟在做什么? – 2010-07-27 21:47:04
试图在使用“id/slug”而非“id-slug”格式的rails中实现干净的URL – 2010-07-30 14:59:22