配置用户url,其中包含用户名,但不关联模型

发布于 2024-10-31 04:16:26 字数 1816 浏览 4 评论 0原文

使用 Rails 2.3.8 和Friendly_id。

人员模型:

class Person < ActiveRecord::Base
  # Nice URL's in Rails.
  def to_param
    "#{login.downcase.gsub(/[^[:alnum:]]/,'-')}".gsub(/-{2,}/,'-')
  end
end

人员控制器:

class PeopleController < ApplicationController
  def show
    @person = User.find(params[:id]) 

    if current_user == @person
      @shops = @person.shops.paginate(:page => params[:page], :order => order)
    else
      @shops = @person.shops.by_status('published').paginate(:page => params[:page], :order => order)
    end

  end

end

用户模型(已删除):

class User < ActiveRecord::Base
  extend ActiveSupport::Memoizable
  acts_as_authentic do |c|
    c.validate_email_field = false
    c.validate_login_field = false
  end

  attr_accessible :login, :email, :password, :password_confirmation, :name, :location, :website,
                  :allow_follower, :allow_message, :allow_updates, :allow_newsletter, :avatar, :role, :remember_me

  # Returns name or login if name is blank
  def full_name
    if self.name.blank?
      login.strip
    else
      (self.name || '').strip
    end
  end
  memoize :full_name

  # Nice URL's in Rails.
  def to_param
    "#{login.downcase.gsub(/[^[:alnum:]]/,'-')}".gsub(/-{2,}/,'-')
  end

end

People.rbUser.rb 中的 to_param 定义不起作用。

Users 控制器下的所有页面纯粹用于用户的帐户设置并且用于私人视图,因此我不需要其中任何漂亮的 URL。但我创建了一个 People 控制器来展示用户的个人资料以供公众查看。

people URL 当前为 http://abc.com/people/1,我想将其转换为 http://abc.com/people/ login-name-here,但又不会破坏 people 控制器中的 @person = User.find(params[:id]) 因为我需要在其中执行狮身人面像搜索。

我可以做什么来实现它?谢谢。

Using Rails 2.3.8, and friendly_id.

Person model:

class Person < ActiveRecord::Base
  # Nice URL's in Rails.
  def to_param
    "#{login.downcase.gsub(/[^[:alnum:]]/,'-')}".gsub(/-{2,}/,'-')
  end
end

People controller:

class PeopleController < ApplicationController
  def show
    @person = User.find(params[:id]) 

    if current_user == @person
      @shops = @person.shops.paginate(:page => params[:page], :order => order)
    else
      @shops = @person.shops.by_status('published').paginate(:page => params[:page], :order => order)
    end

  end

end

User model (stripped):

class User < ActiveRecord::Base
  extend ActiveSupport::Memoizable
  acts_as_authentic do |c|
    c.validate_email_field = false
    c.validate_login_field = false
  end

  attr_accessible :login, :email, :password, :password_confirmation, :name, :location, :website,
                  :allow_follower, :allow_message, :allow_updates, :allow_newsletter, :avatar, :role, :remember_me

  # Returns name or login if name is blank
  def full_name
    if self.name.blank?
      login.strip
    else
      (self.name || '').strip
    end
  end
  memoize :full_name

  # Nice URL's in Rails.
  def to_param
    "#{login.downcase.gsub(/[^[:alnum:]]/,'-')}".gsub(/-{2,}/,'-')
  end

end

The to_param definition in People.rb and User.rb don't work.

All pages under Users controller are purely for user's account settings and is for private view, so I don't need any nice URL in it. But I have created a People controller to showcase as the user's profile for public view.

The people URL currently is http://abc.com/people/1, I want to turn it to http://abc.com/people/login-name-here, yet without breaking the @person = User.find(params[:id]) in people's controller because I need to perform a sphinx search in it.

What can I do to achieve it? Thank you.

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

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

发布评论

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

评论(1

月亮是我掰弯的 2024-11-07 04:16:26

如果您已经解决了这个问题,希望这对其他人有帮助。

如果使用Friendly_id,它不应该破坏User.find(params[:id])。事实上,person/login-nameperson/ 123 条路线均适用。

因为您使用的是 Rails 2.3.8,所以您需要使用Friendly_id 版本 3.2.1.1 是支持 ActiveRecord 2.3.x 的最新版本 - (在此处找到 https://rubygems.org/gems/Friendly_id/versions/3.2.1.1)。版本 4.x 需要 activerecord 3+,因此不兼容。

  • 首先,在您的用户表上安装 gem(如果尚未完成),运行生成器等...(如果需要,可以提供更多帮助)

我建议添加 cached_slug 列(作为string)

然后,接下来...

只需将以下内容添加到 app/models/user.rb

has_friendly_id :login, :use_slug => true, :cache_column => 'cached_slug'

,您应该能够摆脱模型上的 to_param 方法。
请记住更新 attr_accessible 以避免出现问题。

此外,您还可以创建一个实例方法,该方法返回一个与Friendly_id 一起使用的字符串,例如:

has_friendly_id :stripped_login, :use_slug => true, :cache_column => 'cached_slug'

然后只需添加:

def stripped_login
  login.gsub(/regexhere/)
end

FriendlyId 为您小写slug。

注意:我记得在 attr_accessible 项目之前包含 has_Friendly_id 存在问题。我几乎可以肯定它需要放在该行之后,但我现在似乎不记得了。

Hoping this will help someone else coming here, if you have already solved this.

It shouldn't break the User.find(params[:id]) if using friendly_id .. In fact, both person/login-name and person/123 routes with both work.

Because you're using rails 2.3.8, You will need to use friendly_id version 3.2.1.1 is the latest version that supports activerecord 2.3.x - (found here https://rubygems.org/gems/friendly_id/versions/3.2.1.1). Version 4.x needs activerecord 3+ so won't be compatible.

  • Firstly, install the gem (if not already done), run the generator etc ... (more help can be provided on this if needed)

on your user table I recommend adding a cached_slug columns (as a string)

Then, following that ...

Just add the following to app/models/user.rb

has_friendly_id :login, :use_slug => true, :cache_column => 'cached_slug'

and you should be able to get rid of the to_param methods on your models.
Remember to update attr_accessible to avoid issues.

Also, you can create an instance method that returns a string for use with friendly_id, eg:

has_friendly_id :stripped_login, :use_slug => true, :cache_column => 'cached_slug'

Then just add:

def stripped_login
  login.gsub(/regexhere/)
end

FriendlyId downcases the slug for you.

NOTE: I recall there being a problem with including the has_friendly_id before the attr_accessible items. I'm almost certain it needs to be placed after that line, but I can't seem to recall at the moment.

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