0

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.

kamusett
  • 1,403
  • 1
  • 12
  • 22
  • Both your routes have same path. How do you expect Rails to know when to route to appropriate controller then? – Marek Lipka Jan 24 '14 at 12:25
  • The actions need the same route... How to fix it? – kamusett Jan 24 '14 at 12:35
  • It's not the good practice. Why you want them to have the same route? – Marek Lipka Jan 24 '14 at 12:40
  • Coz They're called from the same page (using the same route). I'm noticing I'm doing something wrong, but dunno wat. I'm newbie on RoR. Whats the best way to do it? – kamusett Jan 24 '14 at 12:44
  • It doesn't make any sense for two precisely identical routes to be served by two different actions. Where they're *called* from is irrelevant. – Dave Newton Jan 24 '14 at 12:59
  • 1
    I think there will not be much difference in the actions `exec_release` and `exec_client` except the fact that one will set the status to be published and other to draft. So why don't you have only once action and handle both situations in it. By the way it will be good if you can put the controller code here. :) – Manoj Monga Jan 24 '14 at 13:21

1 Answers1

1

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.

marvelousNinja
  • 1,510
  • 9
  • 15
  • That seem the best option of course, but in this case I'll to be redirected to another page (publish and release). I'd like to stay on the same page that I was before click on the buttons. How Can I solve this using routes? – kamusett Jan 24 '14 at 16:45
  • 1
    The point is - your controller code will decide: where to go after the code has been executed. For example, you can use [redirect_to](http://apidock.com/rails/ActionController/Base/redirect_to) in the controller code and redirect user to desired page. – marvelousNinja Jan 24 '14 at 17:58
  • Thank you very much for your explanation. I've realized it well, and I solved my problem. – kamusett Jan 27 '14 at 16:13