Rails routing /forum/[category_id]/[topic_id] - ruby-on-rails

I would like to create nested routes for my forum, where a topic belongs to a category.
I'm not liking the default nested routes
resources :forum_category do
resources :forum_topic
end
which would give me something like /forum_category/[forum_category_id]/forum_topic/[forum_topic_id]
What I want:
I'm looking to create the following rules (leaving out POST, PATCH, PUT routes)
/forum => forum_topic#index
/forum/new => forum_topic#new
/forum/[forum_category_id] => forum_topic#index
/forum/[forum_category_id]/[forum_topic_id] => forum_topic#show
So /forum is my index, forum/open-mic is my index limited to category with seo slug open mic and finally /forum/open-mic/lets-talk-about-fun would be my forum topic lets-talk-about-fun which is categorized under open-mic.
Is there any solution for this build into Rails?

If you're just looking for GET requests you can do this:
get '/forum', to: "forum_topic#index", as: :forum_topics
get '/forum/new', to: "forum_topics#new", as: :new_forum_topic
get '/forum/:forum_category_id', to: "forum_topics#show", as: :forum_topic_category
get '/forum/:forum_category_id/:forum_topic_id', to: "forum_topic#show_topic", as: :show_forum_topic_category
You could map the last two to the same controller action and redirect based on params, but I'd recommend setting up two separate actions for readability.

you can do like this for customize routes:
match "<your-url>", to:"<controller>#<action>", as: "<path-name>", via: :<your method-like GET,POST,DELETE>
for example to GET:
match "forum_category/:forum_category_id/forum_topic/:forum_topic_id", to: "forum_topic#show", as: "show_topic", via: :get
and for POST:
match "forum_category/:forum_category_id/forum_topic", to: "forum_topic#update", as: "update_topic", via: :post
in your controller you have this params:
param[:forum_category_id] and param[:forum_topic_id]

Related

Rails same incoming route but different controller actions?

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

Use a namespace only as param

At the moment my routes are looking like this:
search_index GET /search(.:format) search#index
But I want to get routes like these:
search_index GET /topic1/search(.:format) search#index
search_index GET /topic2/search(.:format) search#index
As you can see, I don't want to introduce namespaces. The "topic" part of the url shall only be transported as a param.
in your routes.rb append:
match '/:topic_name/search' => 'search#show', via: :get
this will add topic_name to your params, accessible in controller - params[:topic_name]

How to use named routes and Rails path prefixes when a view is in a subfolder

If I have a document called 'letter_1.html' stored in 'static_pages/letters/letter_1.html', how do I correctly do routing and use named path notation to link?
Below is my code in routes.rb:
match '/letters/letter_1', to: 'static_pages/letters#letter_1', via: 'get'
If I want to call it I tried the following:
<%= link_to "Letter", letter_1_path %>
My guess is that 'letter_1_path' is the problem because of the two underlines. But what is the correct syntax to fix it? Also, I'm not sure if routing is done correctly with the subfolder.
Thanks!
You have to actually name the route:
match '/letters/letter_1', to: 'static_pages/letters#letter_1', via: 'get',
as: :letter_1
Then you can use letter_1_path and letter_1_url
There is also the rails 4 way:
namespace :static_pages do
resources :letters, only: [] do
collection do
get :letter_1
end
end
end
Which will give you letter_1_static_pages_letters.
Checkout the guides http://guides.rubyonrails.org/routing.html

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

Different controller actions for POST and GET requests at the same route in Rails

I want to route the same address, e.g., 'http://server/path' to different controller actions depending on the request type, whether it is a GET or POST request.
How can I do that in Rails?
Thanks!
get "/path" => "controller#get_action"
post "/path" => "controller#post_action"
I think you could do this:
match '/path' => 'controller#action', :via => :get
match '/path' => 'controller#another_action', :via => :post
Generate a resource using the Rails scaffold and you'll see how it should be done:
./script/generate scaffold Person name:string
EDIT
Got downvoted so maybe I should expand my answer. The scaffold demonstrates how to build a RESTful resource. By convention, a POST will map to the create method in the controller, a GET will map to the index method (or the show method if an ID is present), etc. All you need add to your routes.rb is:
resources :people

Resources