evanrmurphy
evanrmurphy

Reputation: 1846

Rails 3 routing: include flash params with root redirect

In Rails 3 you can set up your root path to redirect elsewhere right from config/routes.rb:

root :to => redirect("/dashboard")

This works, except that it wipes out any flash params passed to root. How can you pass them along through the redirect?

Update: James Chen's solution, i.e. rewriting the route declaration as root :to => redirect { |p, req| req.flash.keep; "/dashboard" }, works for me. But there are two things I don't understand about it:

  1. What does p stand for, params?
  2. I tried rewriting the block with do/end and linebreaks:

    root :to => redirect do |p, req|
      req.flash.keep
      "/dashboard"
    end
    

    But this fails with "ArgumentError: redirection argument not supported". Why is this?

Upvotes: 3

Views: 3480

Answers (2)

n0denine
n0denine

Reputation: 341

why wouldnt you just do something like

root :to => 'dashboard#index'

assuming that '/dashboard' leads to the dashboard controller index action.

Update

I mean if you reallllly wanted to you could add to the html.erb page that is displayed at '/dashboard'

and do something like this

<% if request.referer.scan('yoursitename').size == 0 %>
 <%= create your own notice here %>
<% end %>

Edit again... sigh..

Forgot, you could just root :to => 'whateveryouroriginal#rootareawas' then just do:

redirect_to '/dashboard'

in your whateveryouroriginal controller rootareawas action

This is really dirty but its 2am and i have to drive to Georgia from Illinois.

Hope this helps

Upvotes: 3

James Chen
James Chen

Reputation: 10874

Use flash.keep.

Write a proc for the redirect in your routes file:

root :to => redirect { |p, req| req.flash.keep; "/dashboard" }

So flash params from normal redirect to root url will be passed:

redirect_to root_url, :notice => "test flash notice"

Upvotes: 12

Related Questions