Question

I've the same two routes (see below) which can be activated by two different buttons, which call two different buttons.

My scenario is:

I've two buttons on the same page ("Release Version" and "Publish Version"). For each of these buttons I call a different remote method (exec_client and exec_release).

So, for questions of routes ambiguity (I think...) I couldn't call the second function I've defined on my routes.rb. Every time when I click on "Publish Version" button I call the exec_client method, whereas this button was supposed to call the exec_release method.

My question is: What can I do to fix it?

Below my routes code, where I think is the problem of code.

match 'projects/:id/repository', :action => 'exec_client', :controller => 'repositories', :via => :post
match 'projects/:id/repository/:branch', :action => 'exec_client', :controller => 'repositories', :via => :post
match 'projects/:id/repository', :action => 'exec_release', :controller => 'repositories', :via => :post
match 'projects/:id/repository/:branch', :action => 'exec_release', :controller => 'repositories', :via => :post

If you need another piece of my code, please ask and I'll put here.

Was it helpful?

Solution

Rails routes have a priority based on a position in routes.rb file. Simple explanation of what is happening: some of your routes are more 'common' than others. So, for example:

match 'projects/:id/repository', :action => 'exec_client', :controller => 'repositories', :via => :post
match 'projects/:id/repository/:branch', :action => 'exec_client', :controller => 'repositories', :via => :post

Second route has no way to be triggered, because requests to /projects/:id/repository will be routed to exec_client action even if they have :branch parameter (actually, any number of parameters). So, you need to follow this simple convention: more specific routes at the beginning of the file, more common routes at the end of the file.

Also, it's a bad practice to use identical routes (identical uri and HTTP verbs). According to route priorities, you will always trigger the highest variant (the one, who was defined earlier). The simplest thing you can do, is to make a separate route for each of your actions.

So, here's an example what should get you going:

match 'projects/:id/repository/publish', :action => 'exec_client', :controller => 'repositories', :via => :get
match 'projects/:id/repository/release', :action => 'exec_release', :controller => 'repositories', :via => :get

This will create a two separate routes for your actions. From what I've seen - :branch parameter is optional. You can check for it's presence in the controller code. Also, I suggest you to read Rails Guide: Routing. This way, you learn about REST and Rails routing basics.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top