友好的 URL 问题,但有一些变化

发布于 2024-08-04 01:00:55 字数 918 浏览 5 评论 0原文

我有一个用户模型和一个配置文件模型。

用户 has_one:个人资料 profile own_to :user

由于这种关系,我通过调用用户 ID 来获取我的个人资料。所以我的网址如下所示:website.com/users/[user_id]/profile

我不确定是否有解决我的问题的方法。这里是: 我想显示 SEO 友好的网址。我正在考虑使用 Friendly_id 来实现此目的。但是,我们的个人资料是针对企业的,我们要在网址中放入企业名称。问题是我们的用户模型故意不询问公司名称,而当前显示在网址中的是 user_id

有没有办法从配置文件模型中获取企业名称(即@profile.name),并使用它来填充网址的 [user_id] 部分?

更新:我已经设置了Friendly_id gem,看起来这可以工作。在我的配置文件模型中,我有: has_friend_id :name, :use_slug => true

当我更新个人资料时,Friendly_id 确实通过将个人资料中的公司名称放入网址的 user_id 部分来对网址进行 slugify。但是,当然,这引发了一个例外。

因此,如果配置文件模型中的企业名称是 Mike's Bar,则 URL 现在为 /users/mike-s-bar/profile。例外情况是这样的:无法找到带有 ID=mike-s-bar 的用户。

这是我的个人资料控制器:

def show
  @user = User.find(:all)
  @profile = User.find(params[:user_id])
end  

有办法让它工作吗?

I have a user model and a profile model.

user has_one :profile
profile belongs_to :user

Because of this relationship, I pull up my profiles by calling the user id. So my urls look like this: website.com/users/[user_id]/profile

I'm not sure if there's a solution to my problem. Here it is:
I would like to display SEO friendly urls. I'm looking at using friendly_id for this. But, our profiles are for businesses, and it's the business name that we want to put in the url. The problem is that our user model intentionally does not ask for the business name, and it's the user_id that gets displayed in the url currently.

Is there a way to grab the business name from the profile model (i.e. @profile.name), and use it to populate the [user_id] part of the url?

UPDATE: I've set up the friendly_id gem and it seems like this could work. In my profile model, I have this: has_friendly_id :name, :use_slug => true

When I updated a profile, friendly_id did slugify the url by putting the business name from the profile into the user_id part of the url. But, of course, this threw an exception.

So if the the business name in the profile model is Mike's Bar, the url now says /users/mike-s-bar/profile. The exception says this: Couldn't find User with ID=mike-s-bar.

Here's my profiles controller:

def show
  @user = User.find(:all)
  @profile = User.find(params[:user_id])
end  

Is there a way to make this work?

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

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

发布评论

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

评论(1

挽梦忆笙歌 2024-08-11 01:00:55

嗯,一个企业有多个用户吗?如果是这样,那么 /users/xzy/profile 如何是唯一的?

否则:我不认为Friendly_id可以处理模型表之外的列。然而,这里我会尝试一种方法:

class User

  # used by rails to generate URLs - defaults to ID
  def to_param
    "#{self.id}-#{self.business.name}"
  end

  ...
end

find 方法本身会跳过破折号后面的所有内容。这会导致您的网址可读性稍差:/users/xzy/profile 将变为 /users/7-xzy/profile,这与 SO 的方式非常相似做到了。

Hmm, does a Business have multiple users? If so, how can /users/xzy/profile then be unique?

Otherwise: I don't think that friendly_id can handle columns outside of the models table. However here an approach I would try:

class User

  # used by rails to generate URLs - defaults to ID
  def to_param
    "#{self.id}-#{self.business.name}"
  end

  ...
end

The find method skips everything behind a dash by itself. This would cause your URLs to be a little bit less readable: /users/xzy/profile will become /users/7-xzy/profile which is quite similar to how SO does it.

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