在 Rails3 中覆盖到 /(root)的资源路由:不更改路径助手?
我对 Rails3 还很陌生,我基本上创建了一个 subscribers
脚手架,我只希望我的应用程序响应 new
和 create
操作。
因此,在 config/routes.rb 中,我定义了:
resources :subscribers, :only => [:new, :create]
这是这样工作的
GET /subscribers => subscribers#new POST /subscribers => subscribers#create
现在我希望我的应用程序在 /
(根)而不是 /subscribers 处展示订阅者资源
,所以这就是我所做的:
match '/' => "subscribers#new" match '/' => "subscribers#create" match '/' => "subscribers#thankyou" resources :subscribers, :only => [:new, :create]
这在某种程度上有效,但可能不是最干燥的事情:这是我遇到的问题:
- 在创建问题后返回表单时,浏览器显示
/subscribers
URL 而不仅仅是/
,表单是使用form_for(@subscriber)
辅助方法创建的,因此path
助手必须在某种程度上不受路线的影响 - 理想情况下,我什至不希望应用程序响应
/subscribers
上的请求 - 我注意到一个奇怪的错误,在断开连接时发布表单时(来自
/
,然后在连接回来时进行刷新(浏览器要求重新提交 => OK),Rails 应用程序崩溃(虽然我没有错误堆栈,因为这是在生产环境中),为什么会这样?
另外,我尝试以这种方式设置路线:
resources :subscribers, :only => [:new, :create] do collection do post '/' => :create get '/' => :new end end
这可能是 DRYer,但它不能解决任何这些问题。
我确信这很简单,请帮忙!
I am quite new to Rails3, I basically created a subscribers
scaffolding, I only want my app to respond to new
and create
actions.
So in config/routes.rb
I defined:
resources :subscribers, :only => [:new, :create]
Which works this way
GET /subscribers => subscribers#new POST /subscribers => subscribers#create
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:
- 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
Which is probably DRYer, but it doesn't fix any of these issues.
I am sure this is something quite simple, please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢您的回答,它帮助我找到了问题的确切解决方案:
在 Rails 3 上测试和工作:)
Thank you for your answers, it helped me find the exact solution to my question:
Tested and working on Rails 3 :)
您可以这样做
并确保您的根模板正在提供
GET /
服务,例如,通过将其添加到 SubscribersController:我尝试使用
match "/"
声明来覆盖资源索引操作并将其映射到另一个控制器,但显然资源声明始终完全覆盖手动声明的路由。You could do
and make sure that
GET /
is being served by your root template, e.g. by adding this to SubscribersController:I experimented with using a
match "/"
declaration to override the resource index action and map it to another controller instead but apparently aresources
declaration is always fully overriding manually declared routes.对于列表中的第 2 个,删除此行,并重写 erb 中的所有 _path 或 _url 方法:
For number 2 in your list, delete this line, and rewrite any _path or _url methods in your erb: