I have a rails application that contains many user pages. When a user wants to point a domain at this page, how would I do that?
Right now I've tested out this, and it works -
root :to => "controller#show", :id => 4, :constraints => {:host => "www.exampleurl.com"}
but need to convert this to be dynamic, so that after I migrate a column into the model called
domain it checks domain and serves it the proper ID.
something like -
root :to => 'controller#show', :id => ':id', :constraints => {:host => ':domain'}
What would something like this look like?
Related
I want to rename a few of my routes, for example:
get 'legal/terms_of_service', :to => 'legal#terms_of_service', :as => :datenschutz
that works, but it doesn't change the acutal URI- and I want that to be changed as well. path: does not work here.
Thank you
If you want the URI to be /datenshutz then you can do this:
get '/datenschutz', :to => 'legal#terms_of_service', :as => :datenschutz
The get '/datenschutz' determines the url browsers or other http clients use to access the controller.
The :to => 'legal#terms_of_service' specifies a controller class and controller action used to respond to the route.
The :as => :datenschutz changes the method you use in views to create links to the route (such as datenschutz_path).
I am trying to display params in my url so i have added
patient_record_path(:limit => 10)
I am now trying to correctly route this.
Currently i am getting the error
No route matches {:action=>"show", :controller=>"patient_record", :limit=>10}
I am currently using the route
match 'patient_record/show&limit', :to => 'patient_record#show'
You should not add the limit to your route. Just simple define your route like this:
match 'patient_record/show', :to => 'patient_record#show', :as => 'patient_record_show'
A better solution however would be
resources :patient_records
This would create the following path helpers:
patient_records_path => "/patient_records" => 'patient_record#index'
new_patient_record_path => "/patient_records/new" => 'patient_record#new'
edit_patient_record_path(:id) => /patient_records/:id/edit => 'patient_record#edit'
patient_record_path(:id) => "/patient_records/:id" => 'patient_record#show'
Update: wrong use of path helper
I have looked at your question again and found another bug: the path helper for show needs a record. The correct use is:
# path to show
patient_record_path(#patient_record, :limit => 10)
# path to index
patient_records_path(:limit => 10)
Using Rails 3.1. I have the following in my route:
In my model Shop, I have a column called shop_type that has either boutique or saloon. Instead of having the url:
http://localhost/spots/1
I would like to separate it by the shop_type to:
http://localhost/boutique/1
http://localhost/saloon/2
So I added the following to my route:
resources :boutique, :controller => 'shops', :constraints => { :shop_type => 'boutique' }
resources :saloon, :controller => 'shops', :constraints => { :shop_type => 'saloon' }
The problem with this is I can access record ID 1 with shop_type = boutique with either of the URL. Ideally, it should return error when a user tries to access
http://localhost/saloon/1
But the above URL just works fine, which is not what I want.
Also, is there anyway to redirect all shops/1 to the new URL which is by shop_type?
Many thanks.
If you want to do this, then your application is probably telling you that it really wants two separate classes, Boutique and Saloon. Can you do that? If not, why not?
Maybe its better to tell Rails directo allow urls as:
get "/:shop_type/:id", :to => 'shop_controller#show'
And in controller check if the record exist, and return :status => 404 if not:
#shop = Shop.where(:id => params[:id], :shop_type => params[:shop_type]).first
render :status => 404 and return if #shop.nil?
Note that route provided is too greedy and put it after all other routes so it will not 'eat' other request.
I'd like to create a route in my rails app to handle a gps-coordinate parameter. The intention is to find restaurants near the given position.
This is were I started:
match "/restaurants/near/:lat/:lng(/:range)", :to => "restaurants#near", :as => "near", :constraints => {:range => /\d+/}
It seems the router has problems with float parameters, an url like /restaurants/near/53.0123/10.5678 isn't recognized. Do you have a solution or best practice for handling GPS coordinates in rails urls?
Thank you!
The problem is caused because Rails try to use the "dots" for search for the format (.:format)
So, you can add some constraints to fix it, for example:
match "/restaurants/near/:lat/:lng(/:range)", :to => "restaurants#near", :as => "near", :constraints => {:lat => /\-?\d+(.\d+)?/, :lng => /\-?\d+(.\d+)?/ , :range => /\d+/}
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