Rails 路由,我将如何创建这种 slug 式路由?

发布于 2024-10-11 14:05:31 字数 258 浏览 0 评论 0原文

我正在尝试创建一个如下所示的网址:

   www.example.com/something/:a_slug

所以一个网址是/某物,其中某物不是控制器(但它映射到控制器,如控制器别名)。某事是一个固定的词。

然后 :a_slug 可以是传递到控制器并在操作中拾取的任何 slug。

有点像非常自定义的显示资源 url。

看起来很简单,但我似乎无法找到正确的路线。

干杯!

I'm trying to create a url that looks like this:

   www.example.com/something/:a_slug

So a url that is /something where something is not a controller (but it is mapped to a controller, like a controller alias). Something is a fixed word.

Then :a_slug can be any slug passed into the a controller and picked up on an action.

Sort of a very custom show resource url.

It seems simple but I can't seem to get the route right.

Cheers!

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

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

发布评论

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

评论(1

隔岸观火 2024-10-18 14:05:31

routes.rb

match 'something/:a_slug' => 'widgets#show'

app/controllers/widgets_controller.rb

class WidgetsController < ApplicationController
  def show
    render :text => params[:a_slug]
  end 
end

就像魔术一样,GET /something/feh 显示 feh 在浏览器中。

哦,当然这是针对 Rails 3 的。 Rails 2 类似,但您没有在那里指定要求。

routes.rb

match 'something/:a_slug' => 'widgets#show'

app/controllers/widgets_controller.rb

class WidgetsController < ApplicationController
  def show
    render :text => params[:a_slug]
  end 
end

And as if by magic, GET /something/feh shows feh in the browser.

Oh, of course this is for rails 3. It's similar for rails 2 but you didn't specify a requirement there.

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