views:

75

answers:

3

I have two routes:

map.all_devices '/all_devices', :controller => 'all_devices', :action => 'initialize_devices'
map.show_user_date_select '/all_devices', :controller => 'all_devices', :action => 'show_user_date_select'

I want a user to click on a button, do the show_user_date_select action then be redirect back to mysite.com/all_devices. The route above does work, but always sends the user to initialize_devices, which resets the 'show_user_date_select' action.

A: 

It looks like that route explicitly maps /all_devices to the initialize_devices action.

Perhaps you should set some piece of session-specific information the first time initialize_devices is reached and does nothing on subsequent requests.

Duncan Beevers
+1  A: 

Looks like you mapped both of those to the same route. Since you put the initialize_devices one on top, it renders that one with higher priority, which is why you always get that.

Probably what you want is something like this in the routing:

map.all_devices '/all_devices', :controller => 'all_devices', :action =>  'index'

Then have a different route which the form submits to, such as /all_devices/show_user_date_select, and redirect to /all_devices afterwards:

def show_user_date_select
    # do stuff here
    redirect_to all_devices
end
Karl
A: 

The routes seem a little odd to me. Try something like:

map.resources :all_devices, :member => { :all_devices => :get, :show_user_date_select => :get }

Then in your views:

<%= link_to "All Devices", path_to_all_devices_all_devices %>
<%= link_to "Show Dates", path_to_show_user_date_select_all_devices %>

The link names are awful, but should work with your existing app. I would rather see:

map.resources :devices, :member => { :all => :get, :select => :get }

<%= link_to "All Devices", path_to_all_devices %>
<%= link_to "Show Dates", path_to_select_devices %>

But that will require a bit or re-plumbing on your part.

askegg