English 中文(简体)
铁路和铁路: 改变连接中所使用的钥匙,以便使用不同的领域,然后是主要的关键
原标题:Rails and SEF URLs: Changing the key used in link_to to use a different field then the primary key

在我的铁路3号座标中,我有一个称为用户的模型,其名称和用户名称都是指数化和独特的。 在某些地方,我有以下看法:

<%= link_to  Show this user , user %>

这形成了这样一个联系:

<a href="/users/980190962">Show this user</a>

where that horrible number is the id (primary key). Now, for the SEFness of the site, I would like to use the username instead of the id wherever I use link_to "...", user, but only for that User model. Adding parameters to the link_to does not help, because I already have it in many places (and I do not want to have to add another parameter everywhere all the time).

我怎么能够凌驾于链接的行为——只帮助一个模式?


我从接受的答复中扣除了这个问题的全部答案。 我在我的用户模式中添加如下内容:

def to_param  # overridden to use username as id, and not the id. More SEF.
  username
end
def self.find(id)
  @user = User.find_by_id(id)
  @user = User.find_by_username(id) if @user == nil
  throw ActiveRecord.RecordNotFound.new if @user == nil
  @user
end

这使得与链接产生的所有联系——使用用户名称,而发现用户名称和用户名称将同时尝试,使之也落后。

有些人像我一样是新的铁路公司,会发现这种好处。

最佳回答

不能确定我所说的话,但否决了<代码>#to_param。

问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签