0

I'm wondering if there is a way to make a rails app that works without the controller path.

Typically some paths might be
www.mysite.com/event/slug
www.mysite.com/event/this_is_the_first_event_slug
www.mysite.com/event/another_slug_here

I'm Wondering if there's a way (within the rails paradigm) to create pages off the main path
For example
www.mysite.com/slug
www.mysite.com/this_is_the_first_event_slug
www.mysite.com/another_slug_here

I know it's a small thing, but it bugs me a bit, and I'd like to set it up.

baash05
  • 4,394
  • 11
  • 59
  • 97
  • 1
    Actually, a duplicate: http://stackoverflow.com/questions/3796979/overriding-a-resource-route-to-root-in-rails3-not-changing-the-path-helper – D-side Oct 06 '14 at 07:18
  • This is not the same thing. That is asking how to remove the action aspect of the url. I'm attempting to remove the controller aspect of the url. it's about as different as 1/0 vs 0/1 – baash05 Oct 08 '14 at 03:09
  • **It is the same thing.** `new` and `create` don't have any route suffix by default. That leaves resource prefix (`subscribers`, what you call "controller aspect", possibly because of `SubscribersController`). And accepted answer to that question describes how to get rid of it. – D-side Oct 08 '14 at 07:26

2 Answers2

1

You have to define another Route in your routes.rb and maybe it's one of the last rules you set, because other routes should match first.

get 'event_:slug' => 'events#show'

In your show method the param slug is awailable as params[:slug].

If you want, you can validate the rules with rake routes from the command line. After you added your new route to the routes.rb file, the command will show something like:

GET    /event_:slug(.:format)                 events#show
Robin
  • 8,162
  • 7
  • 56
  • 101
0

yes you can.

in routes.rb, do like

match '/event_slug' => 'event#slug'

that's it. so if you type in browser say www.mysite.com/event_slug then it will definitely work considering routes pattern.

Kushal
  • 218
  • 2
  • 12
  • The match keyword is deprecated since Rails 4. Maybe `slug` is a parameter. – Robin Oct 06 '14 at 07:09
  • the problem with that is that event_slug is like the id of the event. They are not set when the app starts, but grow as more events are added. – baash05 Oct 06 '14 at 07:09
  • so if the `event_slug` is a parameter then routing pattern may get differ. `get '/:event_slug_parameter', :to => 'event#event_slug'`. i hope this works – Kushal Oct 06 '14 at 07:12
  • yeah.. it's not really an urgent thing.. Just a bug bear I have with my app. (It may be me procrastinating deploy) – baash05 Oct 06 '14 at 07:14