在Rails 3中替换资源的默认路由

Bod*_*dhi 5 routes custom-routes ruby-on-rails-3

我有一个像这样定义的资源:

resources :referrals, :except => [:show, :edit, :destroy]
Run Code Online (Sandbox Code Playgroud)

我想替换(不只是添加命名路由)Rails生成的默认路由,特别是更新操作的路由.

这是我的佣金路线:

referrals    GET  /referrals(.:format)     {:action=>"index", :controller=>"referrals"}
             POST /referrals(.:format)     {:action=>"create", :controller=>"referrals"}
new_referral GET  /referrals/new(.:format) {:action=>"new", :controller=>"referrals"}
referral     PUT  /referrals/:id(.:format) {:action=>"update", :controller=>"referrals"}
share             /share(.:format)         {:controller=>"referrals", :action=>"new"}
special           /special(.:format)       {:controller=>"referrals", :action=>"index"}
thanks            /thanks(.:format)        {:controller=>"pages", :action=>"thanks"}
                  /:shortlink(.:format)    {:controller=>"referrals", :action=>"update"}
                  /:linktext(.:format)     {:controller=>"referrals", :action=>"update"}
root              /(.:format)              {:controller=>"pages", :action=>"home"}
Run Code Online (Sandbox Code Playgroud)

我想要的

/:shortlink(.:format)
Run Code Online (Sandbox Code Playgroud)

要么

/:linktext(.:format)
Run Code Online (Sandbox Code Playgroud)

打更新动作,但没有

/referrals/:id(.:format)
Run Code Online (Sandbox Code Playgroud)

这是为了实现一种非密码"安全"的形式.当PUT进行更新操作时,我希望某些事情发生,但我不想要求授权这样做,我不想让根据控制器名称轻松猜测网址和简单的低 - 编号的ids.

如何完全替换 rails给出的默认路由?

rad*_*dha 2

resources :referrals, :except => [:show, :edit, :destroy, :update]

match "/whatever_you_want/:variable_here" => "referrals#updated", :as=> the_link, :via=> :put

然后在你的控制器中你将访问参数

params[:variable_here]

这里的参数是您想要在数据库中进行比较的任何内容,并且路径将像这样创建:

the_link_path或者the_link_url

:via 部分将限制每个 HTTP 方法的路径,因此只有 put 请求才会匹配

更多信息请参见 http://guides.rubyonrails.org/routing.html