Tag: slug

Rails中的漂亮(过时)RESTful URL

我希望我的网站url看起来像这样: example.com/2010/02/my-first-post 我的Post模型有slug字段(’my-first-post’)和published_on字段(我们将从中扣除url中的年份和月份部分)。 我希望我的Post模型是RESTful的,所以像url_for(@post)这样的东西就像它们应该的那样工作,即:它应该生成上面提到的url。 有没有办法做到这一点? 我知道你需要覆盖to_param并使用map.resources :posts设置:requirements选项设置,但我无法全部工作。 我几乎完成了,我90%在那里。 使用resource_hacks插件我可以实现这个目的: map.resources :posts, :member_path => ‘/:year/:month/:slug’, :member_path_requirements => {:year => /[\d]{4}/, :month => /[\d]{2}/, :slug => /[a-z0-9\-]+/} rake routes (…) post GET /:year/:month/:slug(.:format) {:controller=>”posts”, :action=>”show”} 并在视图中: @post.slug, :year => ‘2010’, :month => ’02’) %> 生成正确的example.com/2010/02/my-first-post链接。 我也想这样做: 但它需要覆盖模型中的to_param方法。 应该相当容易,除了事实, to_param必须返回String ,而不是Hash因为我喜欢它。 class Post ‘my-first-post’, :year => ‘2010’, :month […]

Rails中URL中的ID + Slug名称(如StackOverflow中)

我正在尝试在Rails中实现这样的URL: http://localhost/posts/1234/post-slug-name 同时使用ID和slug名称而不是其中之一 http://localhost/posts/1234 要么 http://localhost/posts/post-slug-name (现在我在URL中只有slug名称,所以这部分结束了)。 我怎样才能做到这一点? UPD 我发现了一篇关于此的文章: http : //augustl.com/blog/2009/styling_rails_urls/ ,而不是/id/slug它建议使用/id-slug这对我来说非常合适,所以我会选择这个。

使用Rails 5,我怎样才能使FriendlyId附加一个 – “count + 1”来复制slus而不是UUID?

显然,FriendlyId已经改变了以前默认的方法,即附加一个数字序列来复制slug(这就是我想要的)现在使用UUID: Previous versions of FriendlyId appended a numeric sequence to make slugs unique, but this was removed to simplify using FriendlyId in concurrent code. 此function不是我现在感兴趣的东西,而是更喜欢使用原始方法来生成更清晰的URL。 我发现了一个类似的问题,其中有人提供了以下代码来覆盖friendlyId normalize_friendly_id方法以获得我之后的function,但使用它会导致错误( wrong number of arguments (given 1, expected 0) ): def normalize_friendly_id count = self.count “name = #{name}” super + “-” + count if name > 0 end 我试图将其“转换”成一个友好的“候选人”,但我真的不知道我在做什么,以下不起作用。 […]