Mario Uher
Mario Uher

Reputation: 12397

Rails do something and redirect?

is there a way to execute code and then redirect inside the routes-file like this:

get 'cache/clear' => Proc.new { Rails.cache.clear && redirect('/') }

I just really don't want to create a controller for this particular action.

Upvotes: 3

Views: 570

Answers (1)

cailinanne
cailinanne

Reputation: 8372

Yup. See here: http://www.railsdispatch.com/posts/rails-routing

match "/foo", :to => proc {|env| [200, {}, ["Hello world"]] }

or, more specifically for your case

match "cache/clear", :to => redirect {Rails.cache.clear && '/'}

Upvotes: 1

Related Questions