9

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!

Tarik Ansari
  • 281
  • 2
  • 7
  • You're fighting conventions, and it's always going to be an uphill battle. I would question heavily the reason for not wanting your app to respond to /subscribers and using / for 2 or 3 different actions. You should re-think your architecture. – Bo Jeanes Sep 28 '10 at 00:46
  • Hey bjeanes, I don't want to fight conventions, it's a mailing-list for a "coming soon" soon website, so pretty much the only thing there, I would think there is an easy way to put the resource at /? – Tarik Ansari Sep 28 '10 at 21:40

3 Answers3

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

Tarik Ansari
  • 281
  • 2
  • 7
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:

  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.

til
  • 81
  • 1
  • 2
0

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

resources :subscribers, :only => [:new, :create]
Brian Maltzan
  • 1,327
  • 10
  • 12