Rails same incoming route but different controller actions? - ruby-on-rails

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

Related

Cant rename rails routes

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).

Getting Routing Error(Route doesn't match)

I am trying to update multi-select list on change but I am getting a routing error.
I call this with an onchange event $.post("/levels/category_lists_for_level"
I have an action called category_lists_for_level in a controller called level.
My routes file looks like this.
match '/levels/category_lists_for_level/:id' => 'levels#category_lists_for_level'
resources :levels
resources :levels , :collection => {:category_lists_for_level => :get}
What am I doing wrong here? I never had any problem in Rails 2, all I used to add the collection.
It is a bit hard to say exactly what you need since as others have said you are missing some info, but you have a few apparent things going on here:
You are duplicating routes
You have the route set on a collection and a member
You are allowing multiple requests types (get and post) to access this route.
If you would like to have this operate on a collection you just need:
resources :levels do
post "category_lists_for_level", :on => :collection
end
or on a member:
resources :level do
get "category_lists_for_level", :on => :member
end
This will reduce your routes. Just use rake routes | grep level to get the routes for this controller.
Take a look at this for some more info.

Custom url in ruby on rails

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

named routing doesnt work in rails3

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.

Rails hide controller name

i have match ":id" => "people#show" in my routes.rb
now i can access http://localhost:3000/1
but,in views <%= link_to 'Show', people %> it will generate http://localhost:3000/people/1 ,
i want to it to be http://localhost:3000/1
You could do something like this to ensure that only numeric ids are matched:
match '/:id' => 'people#show', :constraints => {:id => /\d+/}
A good alternative might be to use some kind of identifier, even if it's not the controller name: http://localhost:3000/p/1. This will at least ensure that if you add other controllers and actions you don't end up having to change your routing structure.
You could write a custom route to match that in config/routes.rb. At the bottom of your routes.rb file you will have a route like match ':controller(/:action(/:id(.:format)))'
or something like resources :people. You might have to write a route that matches the route type you want.
You have to create a named route.
match ':id' => 'people#show', :as => :person
And fix your views to use your new method person_path(user_id).

Resources