Rails 使用Friendly_id gem 按资源名称路由访问

发布于 2024-12-27 07:10:49 字数 257 浏览 1 评论 0原文

我有一个存储在 City 表中的城市列表。假设我想生成可通过此示例中的 resource.namecity.name 访问的动态路由。

我希望能够访问 /amsterdam/berlin。如何?

有关信息,我正在使用 Friendly_id gem,因此已经有了 slug 列(如果这更有意义)。

I have a list of cities stored in City table. Let's say I want to generate dynamic routes to be accessible through resource.name, city.name in this example.

I want to be able to visit /amsterdam or /berlin. How?

For info I'm using friendly_id gem so already have slug column if that makes more sense.

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

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

发布评论

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

评论(3

筑梦 2025-01-03 07:10:50

假设您已正确设置Friendly_id:

match '/cities/:name' => 'cities#show'

resources :cities

Friendly_id的快速入门 a> gem:

class City < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, use: :slugged
end

另外:

# If you're adding FriendlyId to an existing app and need
# to generate slugs for an existing model, do this from the
# console, runner, or add a Rake task:

City.find_each(&:save)

这里有一个 RailsCast: http://railscasts.com/episodes/314-pretty-urls-with-friendid?view=asciicast

Assuming you have friendly_id set up correctly:

match '/cities/:name' => 'cities#show'

or

resources :cities

From the Quick Start for the friendly_id gem:

class City < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, use: :slugged
end

Also:

# If you're adding FriendlyId to an existing app and need
# to generate slugs for an existing model, do this from the
# console, runner, or add a Rake task:

City.find_each(&:save)

Here is a RailsCast on it: http://railscasts.com/episodes/314-pretty-urls-with-friendlyid?view=asciicast

迎风吟唱 2025-01-03 07:10:50

在路线文件的末尾添加以下内容:

match '*id' => 'cities#show

然后在您的 CitiesController 中:

def show
  @city = City.find(params[:id])
  # ...
end

At the end of your routes file, add this:

match '*id' => 'cities#show

Then in your CitiesController:

def show
  @city = City.find(params[:id])
  # ...
end
携君以终年 2025-01-03 07:10:50

不知道这是否有帮助..但我已经整理了我在项目中使用的要点。

https://gist.github.com/1908782

它主要适用于我所做的事情,因为我的路线文件是一般来说相当简洁。

它的美妙之处在于,如果您尝试访问一条不存在的路径,它不会碰到任何路线!

顺便说一句,这个在 4.0 版本中被打破了。在撰写本文时,您需要将以下内容放入您的 gemfile 中。

gem 'friendly_id', :git => 'git://github.com/norman/friendly_id.git'

gem 'friendly_id', :git => 'https://github.com/norman/friendly_id.git'

希望这有帮助。

Don't know if this helps .. but I have put together a gist on what I use in my projects.

https://gist.github.com/1908782

It mostly works for what I do as my routes file is generally quite concise.

The beauty of it is, that if you attempt to visit a path that doesn't exist, it won't hit any routes!

Just a side note, this is broken in the 4.0 release. At the time of writing this, you will need to put the following in your gemfile.

gem 'friendly_id', :git => 'git://github.com/norman/friendly_id.git'

or

gem 'friendly_id', :git => 'https://github.com/norman/friendly_id.git'

Hope this helps.

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