我有一个资源的定义如下:
resources :referrals, :except => [:show, :edit, :destroy]
我希望你能替换(而不仅仅是添加)Rails生成的默认路由,特别是更新操作的路由。
这是我的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)
或者
/:linktext(.:format)
要点击“更新操作”,而不是其他操作。
/referrals/:id(.:format)
这是一种实现非密码“安全性”的方式。当PUT请求传递到更新操作时,我希望发生某些事情,但我不想要求授权来执行此操作,并且我不希望基于控制器名称和简单的低编号id轻易猜测URL。
我该如何完全替换Rails提供的默认路由?
to_param
方法来返回短链接属性以进行URL构造中的参数,而不是模型ID。现在,通过使用find_by_shortlink(params[:id])
我可以达到想要的效果,同时防止简单的数字ID起作用。 - Bodhito_param
覆盖只是为了让referral_url
之类的东西正常工作才是必要的(当然能够做到这一点肯定是件好事)。严格来说,我在控制器中完全可以通过find_by_shortlink(params[:id])
实现我所要求的内容。 - Bodhito_param
只是一种便利,这样你就可以键入referral_path(@referral)
而不是referral_path(@referral.shortlink)
。虽然这很方便,但最好知道。很高兴能帮到你。 - Douglas F Shearer