在Rails3中覆盖到/(根)的资源路由:不更改路径助手? [英] Overriding a resource route to / (root) in Rails3: not changing the path helper?
问题描述
我对Rails3还是很陌生,我基本上创建了一个 subscribers
脚手架,我只希望我的应用程序响应 new
和 create
操作。
I am quite new to Rails3, I basically created a subscribers
scaffolding, I only want my app to respond to new
and create
actions.
因此在 config / routes.rb
我定义了:
resources :subscribers, :only => [:new, :create]
这种方式工作
GET /subscribers => subscribers#new
POST /subscribers => subscribers#create
现在,我希望我的应用在 /
(根)而不是 / subscribers
,所以这就是我所做的:
Now I want my app to exhibit the subscribers resources at /
(root) instead of /subscribers
, so here is what I did:
match '/' => "subscribers#new"
match '/' => "subscribers#create"
match '/' => "subscribers#thankyou"
resources :subscribers, :only => [:new, :create]
某种程度上可行,但可能不是最干的事情:这是我遇到的问题:
Which somehow works, but is probably not the DRYest thing: here are the issues I have:
- 返回表格时在创建问题后,浏览器显示
/ subscribers
网址,而不只是/
网址,该表单是使用form_for(@subscriber)
辅助方法,因此路径
辅助方法必须不受路径的影响 - 理想情况下,我什至不希望该应用程序响应
/ subscribers
上的请求 - 我注意到一个奇怪的错误,当断开连接时发布表单(来自
/
,然后在连接恢复时刷新(浏览器要求重新提交=>确定),Rails应用程序崩溃(虽然我没有错误堆栈,因为这是在生产中),为什么?
- When going back to the form after an issue on a create the browser displays the
/subscribers
URL instead of just/
, the form is created using theform_for(@subscriber)
helper method, so thepath
helper must be somehow unaffected by the route - Ideally I don't even want the app to respond to a request on
/subscribers
- I noticed a weird bug, when posting the form while disconnected (from
/
, and then doing a refresh when the connection comes back (browser ask for resubmitting => OK), the Rails app crashes (I don't have the error stack though as this was on production), why is that?
此外,我尝试设置这样的路线:
Also, I tried setting up the route this way:
resources :subscribers, :only => [:new, :create] do
collection do
post '/' => :create
get '/' => :new
end
end
可能是DRYer,但不能解决这些问题。
Which is probably DRYer, but it doesn't fix any of these issues.
我确信这很简单,请帮忙!
I am sure this is something quite simple, please help!
推荐答案
谢谢您的回答,它帮助我找到了我的问题的确切解决方案:
Thank you for your answers, it helped me find the exact solution to my question:
resources :subscribers, :only => [:new, :create], :path => '', :path_names => {:new => ''}
经过测试并在Rails 3上工作:)
Tested and working on Rails 3 :)
这篇关于在Rails3中覆盖到/(根)的资源路由:不更改路径助手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!