将 to_param 用于包含分隔符的字段的 Rails RESTful 资源 [英] Rails RESTful resources using to_param for a field that contains separator characters

查看:50
本文介绍了将 to_param 用于包含分隔符的字段的 Rails RESTful 资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 Rails 2.3.2 应用程序响应并生成这样的 URL:

I want my Rails 2.3.2 app to respond to and generate URLs like so:

/websites/asd.com
/websites/asd.com/dns_records/new

在我的 config/routes.rb 中,我有:

In my config/routes.rb, I have:

map.resources :websites, :has_many => :dns_records
map.resources :dns_records, :belongs_to => :website

然后我可以访问资源,例如:

I can then access resources such as:

/websites/1
/websites/1/dns_records

通过修改我的网站模型,我可以生成更好的 URL,如下所示:

By modifying my Website model, I can generate better URLs like so:

class Website < ActiveRecord::Base
    def to_param
        domain_name
    end
    ...
end

# app/views/websites/index.erb
<% @websites.each do |w| %>
<%= link_to "Show #{w}", website_path(w) %>
<% end %>

# Produces a link to:
/websites/example_without_periods_in_name

但是,对于包含."的域名字符,Rails 变得不高兴.我相信这是因为'.'字符在 ActionController::Routing::SEPARATORS 中定义,它列出了用于拆分 URL 的特殊字符.这允许您执行/websites/1.xml 之类的操作.

However, for domain names that contain '.' characters, Rails gets unhappy. I believe this is because the '.' character is defined in ActionController::Routing::SEPARATORS, which lists special characters to split the URL on. This allows you to do stuff like /websites/1.xml.

SO,有没有一种干净的方法来允许 '.'RESTful URL 中的字符?

我尝试重新定义 ActionController::Routing::SEPARATORS 以不包含.",这是解决问题的一种非常糟糕的方法.这会通过将.:format"附加到它们来混淆生成的 URL.

I've tried redefining ActionController::Routing::SEPARATORS to not include '.', which is a totally bad way to solve the problem. This messes up generated URLs by appending ".:format" to them.

我也知道我可以将 :requirements => { :id => regexp } 添加到我的 config/routes.rb 以匹配包含."的域名(没有这个,params[:id] 被设置为第一个 '.' 之前的域名部分),但这无助于以 REST 方式生成 URL/路径.

I also know I can add :requirements => { :id => regexp } to my config/routes.rb to match a domain name that includes '.' (without this, params[:id] is set to the part of the domain name before the first '.'), but this doesn't help in generating URLs/paths RESTfully.

非常感谢:)尼克

推荐答案

解决了问题,非常感谢 http://poocs.net/2007/11/14/special-characters-and-nested-routes(见http://dev.rubyonrails.org/ticket/6426 额外参考)

Solved the problem, with a big thanks to http://poocs.net/2007/11/14/special-characters-and-nested-routes (and see http://dev.rubyonrails.org/ticket/6426 for additional reference)

我需要为每个嵌套路由添加 :requirements => { :website_id => regexp } ,这些路由也将包含一个带有句点的域名.

I needed to add :requirements => { :website_id => regexp } for each nested route that was also going to include a domain name with periods in it.

这是我的工作路线:

map.resources :websites, :requirements => { :id => /[a-zA-Z0-9\-\.]+/ } do |websites|
    websites.with_options :requirements => { :website_id => /[a-zA-Z0-9\-\.]+/ }  do |websites_requirements|
        websites_requirements.resources :dns_records
    end
end

<%= link_to 'New DNS Record', new_website_dns_record_path(@website) %>

# Produces the URL
/websites/asd.com/dns_records/new

调用

websites.with_options

只是与 DRY 保持一致,因此不必为网站的所有嵌套路由指定 :requirements .所以我也可以

is simply in keeping with DRY so that :requirements don't have to be specified for all nested routes for websites. So I could also have

websites_requirements.resources :accounts
websites_requirements.resources :monthly_bandwidth_records
etc.

这篇关于将 to_param 用于包含分隔符的字段的 Rails RESTful 资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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