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

发布于 2024-09-25 02:54:47 字数 1222 浏览 3 评论 0原文

我对 Rails3 还很陌生,我基本上创建了一个 subscribers 脚手架,我只希望我的应用程序响应 newcreate 操作。

因此,在 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]

这在某种程度上有效,但可能不是最干燥的事情:这是我遇到的问题:

  1. 在创建问题后返回表单时,浏览器显示 /subscribers URL 而不仅仅是 /,表单是使用 form_for(@subscriber) 辅助方法创建的,因此 path助手必须在某种程度上不受路线的影响
  2. 理想情况下,我什至不希望应用程序响应 /subscribers 上的请求
  3. 我注意到一个奇怪的错误,在断开连接时发布表单时(来自 /,然后在连接回来时进行刷新(浏览器要求重新提交 => 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:

  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. Ideally I don't even want the app to respond to a request on /subscribers
  3. 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 技术交流群。

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

发布评论

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

评论(3

关于从前 2024-10-02 02:54:47

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

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

在 Rails 3 上测试和工作:)

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 :)

§对你不离不弃 2024-10-02 02:54:47

您可以这样做

resources :subscribers, :path => ''

并确保您的根模板正在提供 GET / 服务,例如,通过将其添加到 SubscribersController:

  def index
    render 'welcome/index'
  end

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

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:

  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.

裸钻 2024-10-02 02:54:47

对于列表中的第 2 个,删除此行,并重写 erb 中的所有 _path 或 _url 方法:

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

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

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