如何在Rails 3.1中将URL的格式设置为username / controller /:id? [英] How do I get the format of my URLs to be username/controller/:id in Rails 3.1?

查看:64
本文介绍了如何在Rails 3.1中将URL的格式设置为username / controller /:id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望它类似于Twitter处理其推文URL的方式。

I want it similar to the way Twitter handles the URLs for its tweets.

例如,现在我的URL如下所示: mydomain.com/feedbacks/1 / ,其中 feedbacks 是控制器的名称。

For instance, right now my URL looks like this: mydomain.com/feedbacks/1/, where feedbacks is the name of the controller.

我希望它看起来像: mydomain.com/username/feedbacks/1 / 与Twitter相似: twitter.com/username / status /:id /

I want it to look like: mydomain.com/username/feedbacks/1/ which is similar to Twitter's: twitter.com/username/status/:id/.

我的 routes.rb 看起来像这样:

  resources :users do
      resources :feedbacks
  end

像这样,它给我的URL为 mydomain.com/users/1/feedbacks ,但我想要URL中的实际用户名。

When I have it like this, it gives me the URLs as mydomain.com/users/1/feedbacks, but I want the actual username in the URL.

如何获取?

谢谢。

编辑1:如果您要为这个问题添加另一个答案,请确保它已解决我对已给出答案的评论。否则它将是多余的。

Edit 1: If you are adding another answer to this question, please make sure it addresses my comments/questions to the answer already given. Otherwise it will be redundant.

推荐答案

scope ":username" do
  resources :feedbacks
end

来自文档


这将为您提供/ bob / posts / 1之类的URL,并允许

控制器,帮助器和视图中将路径的用户名部分作为params [:username]引用。

This will provide you with URLs such as /bob/posts/1 and will allow you to reference the username part of the path as params[:username] in controllers, helpers and views.






更新:


UPDATE:

我已经测试并确认了paozac的 answer

I have tested and confirmed the accuracy of paozac's answer. I'll clarify it a bit.

假设您有一个 @feedback 对象和一个 id 12 ,并且关联用户的用户名 foouser 。如果要为该 @feedback 对象生成一个指向编辑页面的URL,可以执行以下操作:

Suppose you had a @feedback object with an id of 12, and the associated user had a username of foouser. If you wanted to generate a URL to the edit page for that @feedback object, you could do the following:

edit_feedback_url(:username => @feedback.user.username, :id => @feedback)

输出为 / foouser / feedbacks / 12 / edit

# A URL to the show action could be generated like so:
feedback_url(:username => feedback.user.username, :id => feedback)
#=> "/foouser/feedbacks/12"
# New
new_feedback_url(:username => current_user.username)
#=> "/foouser/feedbacks/new"

此外,如nathanvda在评论中指出的,您可以< href = https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/route_set.rb#L181 rel = nofollow noreferrer>传递有序参数与相应的动态细分匹配。在这种情况下,必须先传递用户名,然后传递反馈ID,即:

Additionally, as noted by nathanvda in the comments, you can pass ordered arguments which will be matched with the corresponding dynamic segment. In this case, the username must be passed first, and the feedback id should be passed second, i.e.:

edit_feedback_url(@feedback.user.username, @feedback)

此外,如果您需要帮助来处理控制器的参数,我建议创建一个特定于此的新问题。

Also, if you need help handling the params from the controller, I suggest creating a new question specific to that.

这篇关于如何在Rails 3.1中将URL的格式设置为username / controller /:id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆