在Rails3中覆盖到/(root)的资源路由:不更改路径助手?

时间:2023-01-19 09:27:22

I am quite new to Rails3, I basically created a subscribers scaffolding, I only want my app to respond to new and create actions.

我是Rails3的新手,我基本上创建了一个订阅者脚手架,我只希望我的应用程序响应新的和创建操作。

So in config/routes.rb I defined:

所以在config / routes.rb我定义了:

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:

现在我希望我的应用程序在/(root)而不是/ subscriber中展示订阅者资源,所以这就是我所做的:

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:

哪种方式有效,但可能不是最糟糕的事情:这里有我遇到的问题:

  1. 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 the form_for(@subscriber) helper method, so the path helper must be somehow unaffected by the route
  2. 在创建问题后回到表单后,浏览器显示/ subscriber URL而不是/,表单是使用form_for(@subscriber)帮助程序方法创建的,因此路径帮助程序必须以某种方式不受路径影响

  3. Ideally I don't even want the app to respond to a request on /subscribers
  4. 理想情况下,我甚至不希望应用程序响应/订阅者的请求

  5. 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?
  6. 我注意到一个奇怪的错误,当在断开连接时发布表单(从/,然后在连接回来时刷新(浏览器要求重新提交=> OK),Rails应用程序崩溃(我没有错误堆栈)因为这是生产),为什么呢?

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.

哪个可能是DRYer,但它不能解决任何这些问题。

I am sure this is something quite simple, please help!

我相信这很简单,请帮忙!

3 个解决方案

#1


19  

Thank you for your answers, it helped me find the exact solution to my question:

感谢您的回答,它帮助我找到了我的问题的确切解决方案:

resources :subscribers, :only => [:new, :create], :path => '', :path_names => {:new => ''}

Tested and working on Rails 3 :)

测试并使用Rails 3 :)

#2


3  

You could do

你可以做到

resources :subscribers, :path => ''

and make sure that GET / is being served by your root template, e.g. by adding this to SubscribersController:

并确保您的根模板提供GET /,例如通过将此添加到SubscribersController:

  def index
    render 'welcome/index'
  end

I experimented with using a match "/" declaration to override the resource index action and map it to another controller instead but apparently a resources declaration is always fully overriding manually declared routes.

我尝试使用匹配“/”声明来覆盖资源索引操作并将其映射到另一个控制器,但显然资源声明总是完全覆盖手动声明的路由。

#3


0  

For number 2 in your list, delete this line, and rewrite any _path or _url methods in your erb:

对于列表中的数字2,删除此行,并在erb中重写任何_path或_url方法:

resources :subscribers, :only => [:new, :create]

#1


19  

Thank you for your answers, it helped me find the exact solution to my question:

感谢您的回答,它帮助我找到了我的问题的确切解决方案:

resources :subscribers, :only => [:new, :create], :path => '', :path_names => {:new => ''}

Tested and working on Rails 3 :)

测试并使用Rails 3 :)

#2


3  

You could do

你可以做到

resources :subscribers, :path => ''

and make sure that GET / is being served by your root template, e.g. by adding this to SubscribersController:

并确保您的根模板提供GET /,例如通过将此添加到SubscribersController:

  def index
    render 'welcome/index'
  end

I experimented with using a match "/" declaration to override the resource index action and map it to another controller instead but apparently a resources declaration is always fully overriding manually declared routes.

我尝试使用匹配“/”声明来覆盖资源索引操作并将其映射到另一个控制器,但显然资源声明总是完全覆盖手动声明的路由。

#3


0  

For number 2 in your list, delete this line, and rewrite any _path or _url methods in your erb:

对于列表中的数字2,删除此行,并在erb中重写任何_path或_url方法:

resources :subscribers, :only => [:new, :create]