Rails RESTful 路由使用 '/'和蛞蝓

发布于 2024-08-13 23:01:53 字数 1260 浏览 3 评论 0原文

我希望做一些类似 wordpress slug 的事情,其中​​我有一个这样的 URL,同时维护 RESTful 路由:

http://foo.com/blog/2009/12/04/article-title

我对保持 RESTFUL 路由感兴趣的原因是我无法使用许多插件,因为我正在使用自定义路由。

我已经完成了 RESTful 外观:

map.connect   '/blog/:year/:mon/:day/:slug',
              :controller => 'posts', :action => 'show',
                :year => /\d{4}/, :month => /\d{2}/,
                :day => /\d{2}/, :slug => /.+/,
                :requirements => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/, :slug => /.+/ }

为了编写链接,我必须编写自定义 link_to 帮助程序来生成正确的 URL。我真的很想让这个 RESTful 并让 link_to post_path( @post ) 产生上面的 URL 和 link_to edit_post_path(@post) ...article-title/edit

我也有 :has_many => [:comments] 我希望它也能发挥作用。我尝试过的 link_to 看起来像这样:

 'posts', :action => 'show', :year => recent_post.datetime.year.to_s,
            :month => sprintf('%.2d', recent_post.datetime.mon.to_i),
            :day => sprintf('%.2d', recent_post.datetime.mday.to_i),
            :slug => recent_post.slug %>

并产生这个(这不是我想要的):

http://foo.com/posts/show?day=30&month=11&slug=welcome-to-support-skydivers&year=2009

我不确定我做错了什么。有可能做到这一点吗?

I am looking to do something similar a wordpress slug where I have a URL like this while maintaining RESTful routing:

http://foo.com/blog/2009/12/04/article-title

The reason I am interested in keep RESTFUL routing is that I am unable to use many plugins because I am using custom routes.

I have already done the RESTful appearance with:

map.connect   '/blog/:year/:mon/:day/:slug',
              :controller => 'posts', :action => 'show',
                :year => /\d{4}/, :month => /\d{2}/,
                :day => /\d{2}/, :slug => /.+/,
                :requirements => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/, :slug => /.+/ }

In order to write the links, I had to write custom link_to helpers to generate the proper URLs. I really would like to make this RESTful and have the link_to post_path( @post ) yield the URL above and the link_to edit_post_path(@post) ...article-title/edit

I also have :has_many => [:comments] and I would that to work as well. The link_to that I have tried looks like this:

 'posts', :action => 'show', :year => recent_post.datetime.year.to_s,
            :month => sprintf('%.2d', recent_post.datetime.mon.to_i),
            :day => sprintf('%.2d', recent_post.datetime.mday.to_i),
            :slug => recent_post.slug %>

and yields this (which isn't what I want):

http://foo.com/posts/show?day=30&month=11&slug=welcome-to-support-skydivers&year=2009

I'm not sure what I am doing wrong. Is it even possible to accomplish this?

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

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

发布评论

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

评论(2

天荒地未老 2024-08-20 23:01:53

我认为它不起作用,因为您没有使用自定义路线。我一直这样做。我只是设置了一个简单的自定义路线:

map.present_page '/blog/:year/:month/:day/:slug',
          :controller => 'posts', :action => 'show'

然后您应该能够执行以下操作:

present_page_path(:year => 2009, 
                  :month => "December", 
                  :day => "13", 
                  :slug => "just-an-example")

您获得查询字符串的原因很可能是因为 Rails 出于某种原因没有连接到您的路线。使用命名路线明确告诉 Rails 使用该路线。让我知道这是否能为您解决问题!

I think it's not working because you're not using a custom route. I do this all of the time. I simply setup a simple custom route:

map.present_page '/blog/:year/:month/:day/:slug',
          :controller => 'posts', :action => 'show'

Then you should be able to do:

present_page_path(:year => 2009, 
                  :month => "December", 
                  :day => "13", 
                  :slug => "just-an-example")

The reason you're getting a query string is most likely because rails isn't making the connection to your route for whatever reason. Using a named route explicitly tells rails to use that route. Let me know if that solves it for you!

对你再特殊 2024-08-20 23:01:53

以下是我的做法...

首先,我并没有尝试使用路由生成的 url 方法。另外,在检查日期参数的格式方面,我不会达到与您相同的程度。由于我自动生成日期戳和 URL 创建,因此我不关心格式有效性,我只是格式化 ActiveSupport::TimeWithZone 对象。

让我们从相关的路线开始:

  map.post_by_date 'content/:year/:month/:day/:slug', 
                  :controller => 'posts',
                  :action     => 'show_by_date_slug'

由于我不想担心参数格式或重复,因此我创建了一个辅助方法并将该辅助方法包含在相关的控制器中:

  def pubdate_slug_url(post)
    year   = post.published_on.strftime('%Y')
    month  = post.published_on.strftime('%m')
    day    = post.published_on.strftime('%d')

    url    = "/" + ["content", year, month, day, post.slug].join("/")

    return url
  end

最后,在我看来,我只需调用该方法,传入我的 Post 对象:

  <h2><%= link_to post.headline, pubdate_slug_url(post) %></h2>

我最终得到的网址如下: http://wallscorp.us/ content/2009/12/06/links

干杯。

Here's how I went about this...

First, I'm not trying to use the route-generated url method. Also, I'm not going to the same extent as you in terms of checking formatting of the date parameters. Since I'm auto-generating the datestamps and the URL creation, I'm not concerned about format validity, I'm simply formatting a ActiveSupport::TimeWithZone object.

Let's start with the relevant route:

  map.post_by_date 'content/:year/:month/:day/:slug', 
                  :controller => 'posts',
                  :action     => 'show_by_date_slug'

Since I didn't want to worry about argument formatting, or repetition, I created a helper method and included the helper in the relevant controller:

  def pubdate_slug_url(post)
    year   = post.published_on.strftime('%Y')
    month  = post.published_on.strftime('%m')
    day    = post.published_on.strftime('%d')

    url    = "/" + ["content", year, month, day, post.slug].join("/")

    return url
  end

Finally, in my view, I simply call the method, passing in my Post object:

  <h2><%= link_to post.headline, pubdate_slug_url(post) %></h2>

I end up with a url like: http://wallscorp.us/content/2009/12/06/links

Cheers.

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