A truly default route

7 views
Skip to first unread message

Ralph Shnelvar

unread,
Feb 22, 2018, 6:22:16 PM2/22/18
to rubyonra...@googlegroups.com
What would routes.rb look like if I want EVERYTHING to go to a controller action called, hmm, 'everything' in static_pages.rb?

Ralph Shnelvar

Walter Lee Davis

unread,
Feb 22, 2018, 9:06:09 PM2/22/18
to rubyonra...@googlegroups.com
Here's the bottom of one of my routes.rb files:

# this has to be the last option in order to work with unprefixed routes
get '/:slug', to: 'pages#show', constraints: lambda { |request| Page.pages.include? request.path_parameters[:slug] }

get "*any", via: :all, to: "errors#not_found"

That's not exactly what you asked for, but it's fairly close.

Walter

> On Feb 22, 2018, at 6:21 PM, Ralph Shnelvar <ral...@dos32.com> wrote:
>
> What would routes.rb look like if I want EVERYTHING to go to a controller action called, hmm, 'everything' in static_pages.rb?
>
> Ralph Shnelvar
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1746028995.20180222162144%40dos32.com.
> For more options, visit https://groups.google.com/d/optout.

Ralph Shnelvar

unread,
Feb 22, 2018, 9:53:43 PM2/22/18
to Walter Lee Davis
Walter,

This is WAAAY above my head.

Would you please give an explanation?  Are both lines necessary?

Ralph



Thursday, February 22, 2018, 7:05:30 PM, you wrote:

WLD> Here's the bottom of one of my routes.rb files:

WLD>   # this has to be the last option in order to work with unprefixed routes
WLD>   get '/:slug', to: 'pages#show', constraints: lambda {
WLD> |request| Page.pages.include? request.path_parameters[:slug] }
WLD>  
WLD>   get "*any", via: :all, to: "errors#not_found"

WLD> That's not exactly what you asked for, but it's fairly close.

WLD> Walter


>> On Feb 22, 2018, at 6:21 PM, Ralph Shnelvar <


>> What would routes.rb look like if I want EVERYTHING to go to a controller action called, hmm, 'everything' in static_pages.rb?

>> Ralph Shnelvar

>> --
>> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to

>> To post to this group, send email to

>> To view this discussion on the web visit

Walter Lee Davis

unread,
Feb 23, 2018, 7:59:15 AM2/23/18
to rubyonra...@googlegroups.com
Sure.

This is from a CMS where pages can be accessed by their "slug" parameter. So a link to www.example.com/some-unique-name will resolve to PagesController#show with the :slug parameter populated with the value 'some-uniqe-name'.

I also added the constraints bit (optional if you really just mean for all requests to be mapped to the one controller/action pair) so that only slugs that were real (as opposed to typos or whatever) would be routed to the PagesController. In the Page model, there is a method called pages:

def self.pages
@pages = Page.pluck(:slug)
end

That just gives me the complete list of possible real slugs, so I can ignore anything else. And in the PagesController, I have the set_page method to allow pages to be found by their slug or their id:

def set_page
if params[:slug]
@page = Page.find_by(slug: params[:slug])
else
@page = Page.find(params[:id])
end
end

That runs in a before_action callback, so my show method is empty.

The last line in the routes is a catch-all that sends a 404. It is only reached if none of the other routes matched, so it only fires when I get a request that I don't legitimately handle. I was getting a LOT of Wordpress "fuzzing" attacks on this server, and I didn't like them clogging my logs.

Walter
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1366615011.20180222195308%40dos32.com.
Reply all
Reply to author
Forward
0 new messages