My Url is http://www.example.com/players/playersdetail/100006 i
have a players controller. I need to remove or hide controller and
method name (players/playersdetail) from url and i want name in place
of id(100006)
Ex- http://www.example.com/gagan-gupta
Ex- http://www.example.com/gagan-gupta/about
Ex- https://www.facebook.com/gagan-gupta/friends
How to achieve in Ruby On Rails?
Routes is
Something::Application.routes.draw do
get "players/search"
post "players/search"
get "players/playerslist"
get "players/playersdetail"
get "players/list"
get "players/followers"
get "players/following"
end
Your::Application.routes.draw do
scope ":name" do
get '', to: 'players#show'
get :about, to 'players#about'
get :friends, to 'players#friends
end
end
Reference
Your routes.rb something like this:
resources :players
which produces routes of the form /entries/24225252/foo.
There exists a :path argument that allows you to use something besides the default name entries. For example:
resources :players, :path => 'my-cool-path'
will produce routes of the form /my-cool-path/2012/05/10/foo.
But, if we pass an empty string to :path, we see the behaviour you're looking for:
resources :players, :path => ''
will produce routes of the form /2012/05/10/foo.
Also another option:
get ':year/:month/:day/:title' => 'some_controller#show', :as => 'players'
put ':year/:month/:day/:title/edit' => 'some_controller#edit', :as => 'edit_players'
delete ':year/:month/:day/:title' => 'some_controller#destroy', :as => 'destroy_players'
Related
A user clicks on a tag like example.com/pizza, and sees all the posts with the tag of pizza in the posts controller. I want people to also see all the "alternateposts" with the tag of pizza as well, but in a different controller.
rails routes throws an error saying its already in use. what's the best way to go about this?
routes.rb
# TAGS
get 'tags/:tag', to: 'posts#index', as: :tag
get 'tags/:tag', to: 'alternateposts#index', as: :tag
You can't declare multiple route with same URL.
In your case, the seconde URL will overload your first one.
You have to declare a single route with a single controller and return posts and alternateposts in the same way.
Yes #Antoine Dewaele is right. you don't declare multiple routes with same URL.
your route file is like this
get 'tags/:tag' => 'posts#index', :as => :tag
your route file should be like this
get 'tags/:tag' => 'posts#index', :as => :tag
get 'all_pizza' => 'all_pizza#index', :as => :all_pizza
For more info you can visit here Rails Routing from the Outside In
I have a model in rails and I called it free_mess
And my routes file contain resources :free_mess
Obviously this model name is not ideal to show in the browser, it shows up like this:
localhost:3000/free_mess/show
localhost:3000/free_mess/index
localhost:3000/free_mess/message1
I need to change the free_mess in the browser to something more readable like 'messages'. So that the browser shows this:
localhost:3000/messages/show
localhost:3000/messages/index
localhost:3000/messages/message1
resources :free_mess, path: 'messages'
This will add alias routes in your app.
But if you want to rename the path AND the shhelper methods, then you should do:
resources :stories, :path => :books, :as => :books
See: Overriding the Named Helpers
Do this in config/routes.rb
To get this
localhost:3000/messages/show
localhost:3000/messages/index
localhost:3000/messages/message1
Do this
get 'messages/show' => 'free_mess#show'
get 'messages/index' => 'free_mess#index'
get 'messages/message1' => 'free_mess#message1'
You can specify a path option:
resources :free_mess, path: 'messages'
I've created a custom route in my routes.rb to clone my activities by the activity ID as following:
resources :activities
get "/activities/:id/clone" => "activities#clone", :as => :clone_activity
post "/activities/:id/clone" => "activities#clone"
When I use <%= clone_activity_url(#activity) %> now; It shows me http://localhost:3000/activities//clone. instead of the ID attached.
Why isn't my ID showing?
You need to wrap the routes inside a block like this
resources :activities do
get "clone" => "activities#clone", :as => :clone_activity
post "clone" => "activities#clone"
end
This will make it so that a GET or POST to the path /activities/:activity_id/clone will trigger the method at activities#clone
Furthermore, the route will be called "activity_clone_activity_path" (or url) - you can change this in your code if you want, also this will only reference the get path since you only attached the :as to the get path, attach to both if you need.
I have a basic controller with named actions representing static pages:
class YourTripController < ApplicationController
def when_to_visit
end
def booking_a_trip
end
...
end
I want a named route to access these actions like:
your_trip_path(:when_to_visit)
your_trip_path(:booking_a_trip)
Seems simple enough right? But I also want to have the URLs use dashs rather then underscores for the generated paths:
example.com/your-trip/when-to-visit
example.com/your-trip/booking-a-trip
Given these constraints what does my route/s look like?
I've tried many approaches and this is the closest I've got so far:
controller :your_trip, :path => 'your-trip', :as => :your_trip do
get "when_to_visit", :path => 'when-to-visit'
get "booking_a_trip", :path => 'booking-a-trip'
end
This correctly routes me to the right action but using a url helper such as your_trip_path(:when_to_visit) results in:
No route matches {:action=>"when_to_visit", :controller=>"your_trip"}
Match url to controller#action and specify route name with :as:
get "your-trip/when-to-visit" => "your_trip#when_to_visit", :as => "when_to_visit"
Url helpers work too:
rails c
Loading development environment (Rails 3.0.3)
ruby-1.8.7-p302 :001 > app.when_to_visit_path
=> "/your-trip/when-to-visit"
Looks like with the help of routing-filter gem it might be possible write a custom route filter for hyphenated urls. That's of course an overkill for simple cases with just a couple of static pages.
Your version with tiny modification works:
controller :your_trip, :path => 'your-trip', :as => :your_trip do
get "when_to_visit", :path => 'when-to-visit', :as => :when_to_visit
get "booking_a_trip", :path => 'booking-a-trip', :as => :booking_a_trip
end
Routes are then named as:
your_trip_when_to_visit
your_trip_booking_a_trip
I have a model called Aircraft, inflected as uncountable, and it's
driving me nuts. What is the correct way to handle this in Rails 3 "beta
4"?
resources :aircraft do
member do
get :flights
end
end
# Of course these work for some actions and not others.
match 'aircraft', :to => 'aircraft#index', :as => :aircraft
match 'aircraft/:id', :to => 'aircraft#show', :as => :aircraft_instance
I think it's just:
resources :aircraft, :singular => :aircraft_instance
Then you link_to them like so:
link_to 'Singular aircraft', aircraft_instance_path(#aircraft)
link_to 'All aircraft', aircraft_path(#aircraft)
Edit
It looks like beta4 thinks aircrafts in the plural of aircraft:
rails console
> :aircraft.to_s.pluralize
=> "aircrafts"
If you just put resources :aircraft into your routes, can you link_to both aircraft_path(#aircraft) and aircrafts_path successfully? If so, you may need to write an initializer for ActiveSupport::Inflector to define your own custom inflection.