In config/routes.rb, I tried both:
root :to => 'things#index', :as => 'things'
and
root :to => 'things#index'
When I hit http://localhost:3000/, both approaches work, and nothing seems to be different.
What is the :as option used for?
The :as option forms a named route.
Usually it's used in a non-root route. For example:
match '/search' => 'search#search', :as => 'search' # SearchController#search
You could then do something like:
<%= link_to search_path, 'Click Here to Search!' %>
search_path and search_url are defined because of the :as
For a root route, you don't really need :as because the the URL helpers root_path and root_url are defined for you by Rails.
Rails 4 compatible.
In path_to_your_app/config/routes.rb
get "/profile/edit" => "users#profile_edit", :as => "edit_me"
Since ruby 2.0 you can use:
get "/profile/edit", to: "users#profile_edit", as: "edit_me"
In path_to_your_app/app/views/**in required view
<%= link_to "Edit profile", edit_me_path %>
Do not use match if you aren't sure you need it:
It creates a vulnerability when you use it in next pattern:
match ':controller/:action/:id'
From documentation:
You should not use the match method in your router without
specifying an HTTP method. If you want to expose your action to both
GET and POST, add via: [:get, :post] option. If you want to expose
your action to GET, use get in the router:
Instead of: match "controller#action"
Do: get "controller#action"
Read more about:
About match
http://github.com/rails/rails/issues/5964
About routes mapping
http://apidock.com/rails/v4.0.2/ActionDispatch/Routing/Mapper/Base/match
http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Base.html
About routes in general
http://api.rubyonrails.org/classes/ActionDispatch/Routing.html
The :as option creates a named path. You can then call this path in your controllers and views (e.g. redirect_to things_path). This isn't very useful for the root path (as it already named root), but is very useful for new routes you add.
Related
I would like to have a custom route querystring based, to access a specified resource. For example:
/opportunities/rent/san-miguel-de-tucuman?id=45045
That route should map to the action OpportunitiesController#show, with the resource id 45045.
Thanks in advance!!!
Updated
This are my current routes:
get 'oportunidades/alquiler/san-miguel-de-tucuman/:id', to: "opportunities#show"
get 'oportunidades/alquiler/san-miguel-de-tucuman', to: "opportunities#index"
So, if I navigate to the /oportunidades/alquiler/san-miguel-de-tucuman?id=123456 route, it go to the Opportunities#index action.
P/S: sorry, I forget to mention that I have a similar route for the index action.
Make your custom routes as:
resources: opportunities, except: :show
get '/opportunities/rent/san-miguel-de-tucuman/:id' => 'opportunities#show', :as => 'opportunities_show'
and pass your 'id' as opportunities_show_path(id)
EDIT
Change your routes as:
get 'oportunidades/alquiler/san-miguel-de-tucuman/:id' => "opportunities#show", :as => 'opportunities_show'
get 'oportunidades/alquiler/san-miguel-de-tucuman' => "opportunities#index", :as => "opportunities_index"
Now when you want to access your show page just use opportunities_show_path(:id =>123456 )
And for index page use opportunities_index_path
Use this
match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :via => :get
and pass a object to the path so created. Eg:-
something_path(#object), here #object is object that with id which will be passed in routes
Option 1
Query string parameter
// /opportunities/rent/san-miguel-de-tucuman?id=45045
match '/opportunities/rent/san-miguel-de-tucuman', :to => 'opportunities#show', :as => "show_opportunity", :via => :get
Option 2
Add id like new parameter. More friendly.
// /opportunities/rent/san-miguel-de-tucuman/45045
match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :as => "show_opportunity", :via => :get
In both cases, you need to call the route like this:
show_opportunity_url(:id => 45045)
In your controller you will get the id in params[:id]
I have this in routes.rb
match '/external_login' => 'admin#external_login', :as => :external_login, :via => [:get,:post]
And I want to generate a friendly url (for my spanish public) "proveedores"
so I have added in routes.rb:
get '/proveedores', to: redirect('/external_login')
To use a link like http.//...n/proveedores
but now I need to use url_for helper that builds http.//...n/proveedores. But How?
url_for (controller: "admin", action: "external_login") don't return "/proveedores" reuturn "/external_login" instead
And if I put in routes.rb
match '/proveedores' => 'admin#external_login', :as => :external_login, :via => [:get,:post]
It crash when type in browser http:...localhost:3000/proveedores
Routing Error
No route matches [GET] "/external_login"
A solution, but I think I'm missing somethig
In routes, two lines:
match '/proveedores', to: 'admin#external_login', :as => :external_login, :via => [:get,:post]
get '/external_login', to: "admin#external_login"
And now I can do this:
2.0.0-p451 :003 > url_for(controller: "admin", action: "external_login")
=> "http://10.210.nn.nnn:3000/proveedores"
Can somebody explain, if this is correct, or maybe there is a simpler solution
Thanks
Thanks to Ashutosh Tiwari, the solution is simpler than it looks. Using the _url helper instead of url_for(....
routes.rb:
match '/external_login', to: 'admin#external_login', :as => :external_login, :via => [:get,:post]
get '/proveedores', to: redirect('/external_login')
and using proveedores_url i get the full url: "http://10.210.nn.nnn:3000/proveedores"
Routes generates for two type of helpers for you, first one is routes_name_path and another is routes_name_url.
_url helper provides you the absolute path while _path helper provides relative path.
You can use _url helper method instead of url_for that will be something like admin_proveedores_url.
Here is my link_to
<%= link_to sub_category.name, controller: :posts, action: :product, id: "#{sub_category.slug}-#{sub_category.id}" %>
Which is pointing to the url
http://localhost:3000/posts/product/fifith-category-s-sub-category-2
I need the url as follows
http://localhost:3000/fifith-category-s-sub-category-2
How can i do it.
my route.rb
resources :posts
match ':controller(/:action(/:id))(.:format)', via: [:get,:post]
what #MarekLipka suggests is correct but defining your routes like this will take up all the single level namespace in your app i.e. "/anything" will route by default to posts#product.
I recommended using some form of identifier to figure out which routes should go to posts#product. What will work for you depends on why you want to do this. Couple of options are:
Use a short namespace:
scope '/pp' do
get ':id', to: 'posts#product
end
# "/pp/:id" routes to 'posts/product'
# pp is a random short name I picked, it could be anything
# link
<%= link_to sub_category.name, "pp/#{sub_category.slug}-#{sub_category.id}" %>
Use a constraint:
get ':id', to: 'posts#product`, constraints: { :id => /sub\-category/ }
# only id's with 'sub-cateogry' route to 'posts/product'
# link (assuming that sub_category.slug has 'sub-category' words in it)
<%= link_to sub_category.name, "#{sub_category.slug}-#{sub_category.id}" %>
If you want path /:id match your posts#product, you should have something like this in your routes:
resources :posts
match ':id', to: 'posts#product`, via: :get
match ':controller(/:action(/:id))(.:format)', via: [:get, :post]
I have a URL I want to be able to redirect to.
Something similar to:
"http://localhost:3000/username/admin/page".
I have a match in routes.rb as:
match ':account/admin/:page' => "admin#index"
I have redirect code:
redirect_to :controller => account.username, :action=>"admin", :page=>"index"
This, however comes up with a routing error:
No route matches {:action=>"admin", :controller=>"sdunn", :page=>"index"}
I know what I have done is wrong, but how can I fix this?
Many thanks.
Route is expecting 2 parameters, first one is :account, second is :page, i think you are only passing :page. I would add :as => 'some_name' to your route and then use _path :
routes.rb
match ':account/admin/:page' => "admin#index", :as => 'my_route'
controller:
redirect_to my_route_path(#user, #page)
my_route_path could be something different depending on your exact route file, so use
rake routes | grep my_route
to see exact name, then add _path to the end.
I have this custom route in my routes.rb
match '/businesses/:permalink', :to => 'businesses#show', :as => :business_permalink
resources :businesses
And I have constructed a link like this:
<%= link_to business.name, business_permalink_path %>
However, whenever I visit the page with that link, I get this error:
No route matches {:controller=>"businesses", :action=>"show"}
I tried inverting the route order:
resources :businesses
match '/businesses/:permalink', :to => 'businesses#show', :as => :business_permalink
This does not work. It works if I change the link to this:
The show action exists and is defined in the file controllers/businesses_controller.rb.I want to create a custom URL using my permalink.
I am new in Rails and I know I am just missing something. What am I missing?
Try this:
<%= link_to business.name, business_permalink_path(business.permalink) %>
Try this:
match '/businesses/:permalink' => 'businesses#show', :as => :business_permalink
More here: http://railscasts.com/episodes/203-routing-in-rails-3