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).
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 know rails uses the controller action style urls like www.myapp.com/home/index for example
I would like to have a url like this on my rails app, www.myapp.com/my_page_here is this possible and if so how would I go about this?
You just use a get outside of any resources or namespace block in your routes.rb file:
get 'my_page_here ', :to => 'home#index'
Assuming you are using Rails 3+, do NOT use match. It can be dangerous, because if a page accepts data from a form, it should take POST requests. match would allow GET requests on an action with side-effects - which is NOT good.
Always use get, put, post or these variants where possible.
To get a path helper, try:
get 'my_page_here ', :to => 'home#index', :as => :my_page
That way, in your views, my_page_path will equal http://{domain}/my_page_here
you just need to make a routing rule to match that url
in this case it will be something like
match 'my_page_here' => 'your_controller#your_action'
your controller and action will specify the behavior of that page
so you could do
match 'my_page_here' => 'home#index'
or
get 'my_page_here', :to => 'home#index'
as suggested in other responses.
for index action in home controller if you have such a controller
see http://guides.rubyonrails.org/routing.html for more details
also see Ruby on Rails Routes - difference between get and match
I have a Rails app that does everything I need it to do via a HTML interface, now I'd like to bolt on an API providing access to bits of the functionality.
How would I do this selective forwarding of some API controller actions to another controller's actions using the Routes.rb?
I have tried the following:
My regular controller routes work fine using:
match 'stuff' => 'stuff#index'
get 'stuff/index'
get 'stuff/some_get_action'
post 'stuff/some_post_action'
But then when I try for my API:
match 'api' => 'api#index'
match 'api/some_get_action' => 'stuff#some_get_action', :via => :get
match 'api/some_post_action' => 'stuff#some_post_action', :via => :post
or...
match 'api' => 'api#index'
get 'api/some_get_action', :to => 'stuff#some_get_action'
post 'api/some_post_action', :to => 'stuff#some_post_action'
I get an error. When I navigate to /api/index to server a HTML page that contains forms for testing the API URLs the url_for raises a 'Routing error' exception saying 'No route matches...'.
You may want to include ':as => ' and define your route names that you may be using as your link paths.
get 'api/some_get_action' => 'stuff#some_get_action', :as => 'some_get_action'
and the link path in your index file will have 'some_get_action_path'. Not sure that 'match' or 'get' automatically resolves to a path name, which by setting ':as' it definitely will.
I like your idea for setting up this page for testing. I'm always doing it in the console, which is surely more difficult than simply clicking a link. Your links probably need to infer that they are :json requests, not :http.
Hey, I want to name my route using the :as parameter. Ive read the Rails Routing Guide about this but unfortunately it wont display me /my_courses in the url
match 'course_enrollments', :to => 'course_enrollments#index', :as => 'my_courses'
thx for your time!
match 'my_courses', :to => 'course_enrollments#index', :as => 'my_courses'
This will route /my_courses to the index action of your CourseEnrollments controller, and allow you to refer to the path by referencing my_courses_path or my_courses_url in your views and controllers.
To clarify: The first parameter in match is what maps the route to an actual URL. The :as option simply allows you to override the name of the route helper.
That matches course_enrollments in the URL, not my_courses. The :as parameter means you can refer to the route in views using (in this example) my_courses_path.
i want to change the default route in RoR to what i want:
consider the following example...
:controller/:action/:id
which will give you the route in the browser as:
http://localhost:3000/controller/action/id
now i want to change it to...
http://localhost:3000/this-is-what-i-want/id
we can get an alias for the controller and the action as well like...
resources :controller, :as => "my-custom-name"
and if you want to have the alias for the action, then
resources :controller, :path_names => { :action => 'my-custome-name-1', :action => 'my-custome-name-2' }
BUT i want to change the controller and the action at once... if u noticed the above http://localhost:3000/this-is-what-i-want/id path in the question...
need help...
thanks in advance...
You need a named route.
In Rails2:
map.a_name 'this-is-what-i-want/:id', :controller => 'controller_name', :action => 'action_name'
In Rails3:
match 'this-is-what-i-want/:id' => 'controller_name#action_name'
You want to be using Rest routes, rather than controller/action
I'm going to use "balls" instead of "this-is-what-i-want"
resources :balls
Then, when you link to a ball, do link_to(ball.name, ball).
This will give you a link of http://localhost:3000/balls/45
This rails rest cheatsheet is a good start.