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
> On Feb 22, 2018, at 9:53 PM, Ralph Shnelvar <ralphs@dos32.com> wrote:
>
> 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 <ralphs@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-talk+unsubscribe@googlegroups.com.
> >> To post to this group, send email to rubyonrails-talk@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
>
> --
> 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-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1366615011.20180222195308%40dos32.com.
> For more options, visit https://groups.google.com/d/optout.
--
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-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/F70BBBB1-299E-4D18-91CF-5AD2051DDFB6%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment