[RUBY] How to create a route directly from the URL you want to specify + α

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.

Conclusion

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.

Status

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 missinghotels 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! !!

After improvement

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

After this experience

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

How to create a route directly from the URL you want to specify + α
How to create a form to select a date from the calendar
[Output] Learn Ruby from what you want to do at the fastest speed-Part 2 "Create a flowchart"
3. Create a database to access from the web module
[Rails] How to create a signed URL for CloudFront
How to create a JDBC URL (Oracle Database, Thin)
How to create a method
How to create a placeholder part to use in the IN clause
I want to create a form to select the [Rails] category
When you want to change the wording to be displayed when making a select box from enum
[Java] How to create a folder
How to create a jar file or war file using the jar command
I want to create a chat screen for the Swift chat app!
How to create a registration / update function where the table crosses
If you want to change the Java development environment from Eclipse
How to run a Kotlin Coroutine sample from the command line
A memo when you want to clear the time part of the calendar
How to return a value from Model to Controller using the [Swift5] protocol
How to run a GIF file from the Linux command line (Ubuntu)
How to create a Maven repository for 2020
How to download and run a Jar package directly from the Maven repository with just the command line
[Swift5] How to create a splash screen
[rails] How to create a partial template
[Rails] How to create a table, add a column, and change the column type
[Swift] If you want to use a URL that includes Japanese, use addingPercentEncoding.
You cannot change the project facet version from a dynamic web module to x.x. How to deal with
[swift5] How to transition from the app to an external site by specifying the URL
[Spring Boot] How to get properties dynamically from a string contained in a URL
How to create a database for H2 Database anywhere
[Rails] How to create a graph using lazy_high_charts
How to get a heapdump from a Docker container
How to create pagination for a "kaminari" array
How to create a class that inherits class information
When you want to use the method outside
How to create a theme in Liferay 7 / DXP
[1st] How to create a Spring-MVC framework project
How to easily create a pull-down in Rails
[Rails] How to create a Twitter share button
[Java] How to get the redirected final URL
[Java] How to get the URL of the transition source
How to create a Java environment in just 3 seconds
[Java] I want to calculate the difference from the date
How to jump from Eclipse Java to a SQL file
How to write Scala from the perspective of Java
How to deploy to Heroku from a local docker image
[Java] How to extract the file name from the path
How to create a Spring Boot project in IntelliJ
[Spring Boot] How to create a project (for beginners)
What to do if you accidentally create a model
How to create a data URI (base64) in Java
[SwiftUI] How to specify the abbreviated position of Text
[Java] How to erase a specific character from a character string
How to determine the look-ahead request (Prefetch) from the browser
How to specify the resource path in HTML import
If you want to recreate the instance in cloud9
Install MySQL 5.6 on CentOS6 [How to specify the version]
How to make a mod for Slay the Spire
[Apple Subscription Offer] How to create a promotional offer signature
I want to connect to Heroku MySQL from a client
I want to create a generic annotation for a type
I want to add a delete function to the comment function