I'm building currently one Rails app and I'd like to stick to all those fancy things like REST and Resources, but I'd like to customise my routes a little. I want my GET route to be little more verbose - the app I'm creating is a simple blog, so instead of GET /posts/1
I'd prefer something like GET /posts/1-my-first-post
.
我正在构建一个Rails应用程序,我想坚持所有那些花哨的东西,如REST和资源,但我想稍微自定义我的路线。我希望我的GET路线更加冗长 - 我正在创建的应用程序是一个简单的博客,所以我不喜欢GET / posts / 1,而是喜欢GET / posts / 1-my-first-post。
Any ideas how to do this? Didn't find anything on the web.
任何想法如何做到这一点?没有在网上找到任何东西。
5 个解决方案
#1
16
Routes:
map.resources :posts
Model:
class Post < ActiveRecord::Base
def to_param
"#{id.to_s}-#{slug}"
end
end
Should do the trick.
应该做的伎俩。
#2
4
Define a to_param
method in your Model and all the url helpers will youse what you return with that method, e.g.:
在您的模型中定义一个to_param方法,所有url帮助器将使用该方法返回的内容,例如:
class Post < ActiveRecord::Base
der to_param
slug
end
end
You will also need to adapt your controllers for that. Replace:
您还需要为此调整控制器。更换:
Post.find(params[:id])
with:
Post.find_by_slug(params[:id])
Also note that the find method raises ActiveRecord::RecordNotFound exception when the record can't be found while using the find_by_* method no Exceptions will be raised so you need to check that manually.
另请注意,当使用find_by_ *方法时无法找到记录时,find方法会引发ActiveRecord :: RecordNotFound异常,不会引发异常,因此您需要手动检查。
#3
2
You could find the friendly_id plugin useful as it will also handle redirections if you rename your slugs (thus seo friendly), handles name collisions and seamlessly integrates with the find method so you don't need to touch your controller methods (except for the redirection thingy).
您可以找到friendly_id插件很有用,因为它还可以处理重定向,如果您重命名slug(因此seo友好),处理名称冲突并与find方法无缝集成,因此您不需要触摸您的控制器方法(重定向除外)啄)。
#4
0
Alternatively...
Add a method like this to post.rb
将这样的方法添加到post.rb
def path
"/posts/#{id}-#{slug}"
end
Then use the following in your views:
然后在您的视图中使用以下内容:
#5
0
Alternatively...
Add a method like this to application_helper.rb
将这样的方法添加到application_helper.rb
def permalink(post)
"#{post_path(post)}-#{post.slug}"
end
Then use the following in your views (using permalink(@post)
instead of post_path
)
然后在视图中使用以下内容(使用永久链接(@post)而不是post_path)
<%= link_to @post.title, permalink(@post) %>
#1
16
Routes:
map.resources :posts
Model:
class Post < ActiveRecord::Base
def to_param
"#{id.to_s}-#{slug}"
end
end
Should do the trick.
应该做的伎俩。
#2
4
Define a to_param
method in your Model and all the url helpers will youse what you return with that method, e.g.:
在您的模型中定义一个to_param方法,所有url帮助器将使用该方法返回的内容,例如:
class Post < ActiveRecord::Base
der to_param
slug
end
end
You will also need to adapt your controllers for that. Replace:
您还需要为此调整控制器。更换:
Post.find(params[:id])
with:
Post.find_by_slug(params[:id])
Also note that the find method raises ActiveRecord::RecordNotFound exception when the record can't be found while using the find_by_* method no Exceptions will be raised so you need to check that manually.
另请注意,当使用find_by_ *方法时无法找到记录时,find方法会引发ActiveRecord :: RecordNotFound异常,不会引发异常,因此您需要手动检查。
#3
2
You could find the friendly_id plugin useful as it will also handle redirections if you rename your slugs (thus seo friendly), handles name collisions and seamlessly integrates with the find method so you don't need to touch your controller methods (except for the redirection thingy).
您可以找到friendly_id插件很有用,因为它还可以处理重定向,如果您重命名slug(因此seo友好),处理名称冲突并与find方法无缝集成,因此您不需要触摸您的控制器方法(重定向除外)啄)。
#4
0
Alternatively...
Add a method like this to post.rb
将这样的方法添加到post.rb
def path
"/posts/#{id}-#{slug}"
end
Then use the following in your views:
然后在您的视图中使用以下内容:
#5
0
Alternatively...
Add a method like this to application_helper.rb
将这样的方法添加到application_helper.rb
def permalink(post)
"#{post_path(post)}-#{post.slug}"
end
Then use the following in your views (using permalink(@post)
instead of post_path
)
然后在视图中使用以下内容(使用永久链接(@post)而不是post_path)
<%= link_to @post.title, permalink(@post) %>