简体   繁体   English

Rails 3.0:在模型中覆盖to_param时路由错误

[英]Rails 3.0: error with routes when overriding to_param in model

I'm getting an error with my routes when I try to override to_param in my user model to use the email address as the id. 当我尝试在我的用户模型中覆盖to_param以使用电子邮件地址作为id时,我的路由出错了。 It appears to be trying to match the entire object for the id when it tries to match the route. 它似乎试图在尝试匹配路由时匹配id的整个对象。 Can anyone help me figure out what I'm missing? 任何人都可以帮我弄清楚我错过了什么吗?

Here's the error: 这是错误:

No route matches {:controller=>"users", :action=>"show", :id=>#<User id: 1, email: ....>}

Here's how I've set up the code. 这是我如何设置代码。

models/user.rb: 车型/ user.rb:

attr_accessible :email    

def to_param 
  email
end

controllers/users_controller.rb: 控制器/ users_controller.rb:

before_filter :get_user, :only=>[:show,:update,:edit,:destroy]

...

def get_user
  @user = User.find_by_email params[:id]
end

config/routes.rb 配置/ routes.rb中

resources :users

And here's the output from rake routes: 这是rake路线的输出:

     user GET    /users(.:format)          {:controller=>"users", :action=>"index"}
          POST   /users(.:format)          {:controller=>"users", :action=>"create"}
 new_user GET    /users/new(.:format)      {:controller=>"users", :action=>"new"}
edit_user GET    /users/:id/edit(.:format) {:controller=>"users", :action=>"edit"}
     user GET    /users/:id(.:format)      {:controller=>"users", :action=>"show"}
          PUT    /users/:id(.:format)      {:controller=>"users", :action=>"update"}
          DELETE /users/:id(.:format)      {:controller=>"users", :action=>"destroy"}

The problem is that the email adds a '.' 问题是电子邮件添加了'。' (dot) in the url, and that confuses rails, because it tries to find a "com" format (if the email ends in .com) (点)在网址中,这会混淆rails,因为它试图找到“com”格式(如果电子邮件以.com结尾)

I've added this code to one of my apps (I have People instead of Users) and it works properly, so the trick is to replace the dot with something else. 我已将此代码添加到我的某个应用程序(我有人而不是用户)并且它正常工作,所以诀窍是用其他东西替换点。 I chose to replace it with an '@' as other symbols such as - or + are valid in email addresses. 我选择将其替换为“@”,因为其他符号(如 - 或+)在电子邮件地址中有效。

file person.rb 文件person.rb

def to_param
  email.sub ".", "@"
end

def self.param_to_email(param) 
  segments = param.split '@'
  host = segments[1..-1].join('.')
  segments[0] + '@' + host
end

file people_controller.rb 文件people_controller.rb

def get_person
  email = Person.param_to_email params[:id]
  @person = Person.find_by_email email
end

There are some more hints about how this works in http://jroller.com/obie/entry/seo_optimization_of_urls_in . http://jroller.com/obie/entry/seo_optimization_of_urls_in中有一些关于它如何工作的提示。

Thanks for the question, I'm just starting with rails, so this is really helping me to understand how it works :). 谢谢你的问题,我刚开始使用rails,所以这真的帮助我理解它是如何工作的:)。

You can include dots '.' 你可以包括点'。' in your to_param return value if you specify a custom regular expression for the 'id' parameter in your route, for example: 如果为路径中的“id”参数指定自定义正则表达式,则在to_param返回值中,例如:

match '/images/:id',
  :via => :get,
  :constraints => { :id => /[^\/]+/ },
  :format => false,
  :to => 'images#show',
  :as => :image

See http://edgeguides.rubyonrails.org/routing.html#specifying-constraints for more details. 有关详细信息,请参阅http://edgeguides.rubyonrails.org/routing.html#specifying-constraints

To avoid problems passing '.' (dot) 为了避免问题传递'.' (dot) '.' (dot) through the URL you can add in your routes definition: '.' (dot)您可以在路线定义中添加的URL:

resources :users, :id => /.*/

credits to: https://stackoverflow.com/a/8943634/333061 积分: https//stackoverflow.com/a/8943634/333061

I encountered problems with sending email address trough GET. 通过GET发送电子邮件地址时遇到了问题。

#this url will cause the following problem
/resend-validation/abcd@abcd.com 
params[:email] = abcd@abcd

# I had to encode the email:
<%= link_to('Resend Code', resend_activation_path(:email => user.email.unpack('H*'))) %>

# than decode it in controller:
email = params[:email].split.pack('H*')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM