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

发布于 2024-10-29 03:07:16 字数 1503 浏览 7 评论 0原文

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

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

并且我想替换(不仅仅是添加命名路由)Rails生成的默认路由,特别是用于更新的路由行动。

这是我的 rake 路线:

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"}

我想要 或

/:shortlink(.:format)

/:linktext(.:format)

执行更新操作,但

/referrals/:id(.:format)

这是为了实现一种非密码“安全”形式。当 PUT 进行更新操作时,我希望发生某些事情,但我不想要求授权才能执行此操作,并且我不想允许根据控制器名称和简单的低值轻松猜测 url编号 id。

如何完全替换 Rails 给出的默认路线?

I have a resource defined like so:

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

and I'd like to replace (not just add a named route) a default route that Rails produces, specifically the one for the update action.

Here is my rake routes:

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"}

I'd like either the

/:shortlink(.:format)

or

/:linktext(.:format)

to hit the update action, but not the

/referrals/:id(.:format)

This is to implement a form of non-password "security". When the PUT goes to the update action, I want certain things to happen, but I don't want to require authorization to do this, and I don't want to allow easy guessing of the url based on controller name and simple low-numbered ids.

How can I fully replace the default route given by rails?

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

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

发布评论

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

评论(1

孤云独去闲 2024-11-05 03:07:16

资源:推荐,:例外 => [:show, :edit, :destroy, :update]

匹配“/whatever_you_want/:variable_here”=> "referrals#updated", :as=> the_link, :via=> :put

访问参数

然后在你的控制器中,你将使用params[:variable_here]

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

the_link_paththe_link_url

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

在此处匹配更多信息
http://guides.rubyonrails.org/routing.html

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

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

then in your controller you will access the param with

params[:variable_here]

here the param is whatever you want to compare against in the db, and the path will be create like this:

the_link_path or the_link_url

the :via part will constrain the path per HTTP method so only put request will match

more info here
http://guides.rubyonrails.org/routing.html

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