Tyler K
Tyler K

Reputation: 526

Rails Routing - Not showing an action

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.

Upvotes: 0

Views: 160

Answers (3)

askegg
askegg

Reputation: 664

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.

Upvotes: 0

Karl
Karl

Reputation: 6165

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

Upvotes: 1

Duncan Beevers
Duncan Beevers

Reputation: 1820

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.

Upvotes: 0

Related Questions