Good evening Aloha man, Yasunori!
Today, I struggled with how to describe the routing when creating the portfolio, so I will write it down as a memorandum for those who are suffering from the same thing or when I forget it.
It's been a long time, so I'll just post the conclusion first.
If you don't want the URL of the page that goes to when you press the search button to be under the control of the controller,
config/routes.rb
Rails.application.routes.draw do
get '/:name', to: 'Controller name#edit'
end
And it ’s okay! !!
↓ Click here for reference page Rails Document Routing [Routing design] How to switch and display multiple views with one action
Well then, I will write in detail below.
I wanted to create a function that transitions to the hotel category search page when I press the button labeled __ "Hotel" __ from the top page, but I wanted the URL to be in the form of localhost: 3000 / hotel
. First, I created each controller separately and described it in the routing.
config/routes.rb
Rails.application.routes.draw do
resources :hotels, only [:index]
end
Then
hotels GET /hotels(.:format) hotels#index
The routing was created.
With this, the URL could be realized in a form close to the desired one, but the more items there are, the more a separate controller must be created.
That means you just have to change the controller that is called! I thought
config/routes.rb
Rails.application.routes.draw do
resources :hotels, only [:index], controller: 'experiences'
end
If you do this, the index actionof the ʻexperiences controller will be called when you access it, so I think that the view file can be displayed by using a template file etc., but in this case nothing is described You end up with a missing
hotels controller`.
This time, I'm writing one, but as the number of items increases, the number of routing descriptions will increase and the number of files will increase.
I was wondering what happened ... and found out that I was researching various things! !! First of all, if you want to specify the URL
config/routes.rb
Rails.application.routes.draw do
get '/hotel', to: 'experiences#index'
end
It was all right just to write! !!
This was mentioned on the routing page of the Rails documentation.
I realized once again the importance of checking the documents firmly ...
By the way, this alone has completed the initial goal of "setting the URL to localhost: 3000 / hotel
", but if it is left as it is, the number of pages will increase in addition to / hotel
. At that time, the number of descriptions will increase steadily.
It's a big deal, so let's improve this too! !!
You can define a new ʻedit action in the ʻexperiences controller
and put it together so that all the routing goes there! !!
controllers/experiences_controller.rb
class ExperiencesController < ApplicationController
def edit
render "/#{params[:name]}"
end
end
config/routes.rb
Rails.application.routes.draw do
get '/:name', to: 'experiences#edit'
end
Also, in the /: name
part of the URL, by setting: name
to the key in params when sending the request and passing the file name of the partial template as the value, the render in the ʻedit action
The corresponding view file is displayed by the method `.
I am impressed with the importance of reading the official documents and the gratitude of the documents left by my predecessors ...
If you have any problems, I would like to investigate more and more! !! (It feels like another power application, but is it okay ...)
Recommended Posts