Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why rails is not generating a route helper for my 'match' route?

This is the two routes i declared in my config/routes.rb file :

  namespace :projects do
    match "proj_rapports_contributeur/select" => 'proj_rapports_contributeur#select', :via => :get
    match "proj_rapports_contributeur/generate/:id" => 'proj_rapports_contributeur#generate', :via => :get
  end

This is the resulting routes and helpers rails generate with rake routes :

projects_proj_rapports_contributeur_select_fr        /hierarchie/rapports_contributeur/selectionner(.:format)                                               projects/proj_rapports_contributeur#select {:locale=>"fr"}
projects_proj_rapports_contributeur_select_en        /en/projects/proj_rapports_contributeur/select(.:format)                                               projects/proj_rapports_contributeur#select {:locale=>"en"}

                                                     /hierarchie/rapports_contributeur/generer/:id(.:format)                                                projects/proj_rapports_contributeur#generate {:locale=>"fr"}
                                                     /en/projects/proj_rapports_contributeur/generate/:id(.:format)                                         projects/proj_rapports_contributeur#generate {:locale=>"en"}

I don't understand why rails didnt generate a route helper for the second route ?

Don't be surprised with the translated route. I'm using the gem 'rails-translate-routes' to translate routes (Resource : https://github.com/francesc/rails-translate-routes)


=== UPDATE for FINAL ANSWER ===

According to the answeer, for those wanting to know the end word, here it the routes I will be using :

  namespace :projects do

    get "proj_rapports_contributeur/select" => 'proj_rapports_contributeur#select'
    get "proj_rapports_contributeur/generate/:id" => 'proj_rapports_contributeur#generate', :as => 'proj_rapports_contributeur_generate'
  end

And these are the resulting helpers :

projects_proj_rapports_contributeur_select_fr GET    /hierarchie/proj_rapports_contributeur/selectionner(.:format)                                               projects/proj_rapports_contributeur#select {:locale=>"fr"}
projects_proj_rapports_contributeur_select_en GET    /en/projects/proj_rapports_contributeur/select(.:format)                                                    projects/proj_rapports_contributeur#select {:locale=>"en"}
projects_proj_rapports_contributeur_generate_fr GET    /hierarchie/proj_rapports_contributeur/generer/:id(.:format)                                                projects/proj_rapports_contributeur#generate {:locale=>"fr"}
projects_proj_rapports_contributeur_generate_en GET    /en/projects/proj_rapports_contributeur/generate/:id(.:format)                                              projects/proj_rapports_contributeur#generate {:locale=>"en"}
like image 878
Douglas Avatar asked Apr 03 '13 10:04

Douglas


People also ask

What is a route helper?

The Helper is responsible for assisting the refuse truck driver on pre-assigned and special routes to pick up garbage, yard waste, or recycling.

What is match in Rails routes?

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.

How do I list a route in Rails?

TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.


1 Answers

This is because the route is not a simple route, it contains a parameter (in your case :id). In this case, you should specify the route name manually using as

match "proj_rapports_contributeur/generate/:id" => 'proj_rapports_contributeur#generate', :via => :get, :as => "your_route_name"

As a side note, replace match + via with the corresponding method name.

get "proj_rapports_contributeur/generate/:id" => 'proj_rapports_contributeur#generate'

It's shorter, and match is deprecated in Rails 4.

like image 139
Simone Carletti Avatar answered Sep 30 '22 19:09

Simone Carletti