[RUBY] pass two arguments to the URI with link_to

Introduction

I posted about the path argument of the link_to method before, but today I was in a situation where I passed two arguments, so I will summarize it so that I will not forget it.

What is the link_to method?

<% link_to 'Character to be displayed', ○○_path %>

By doing so, it can be treated like an HTML a tag.

If path requires an argument

The URI of rails routes in the terminal

/class_rooms/:class_room_id/messages/:id(.:format)

If ":" and "id" are attached, it is necessary to pass an argument there. Requires arguments for : class_room_id and: id.

When you need to pass two arguments to path

In the previous example, The first : class_room_id is needed because the routing is nested.

routes.rb


resources :class_rooms, only: [:index, :new, :create] do
    resources :messages, only: [:index, :create, :destroy]
  end

If the routing is nested, you must specify two path arguments for link_to. </ font>

<%= link_to 'Character to be displayed', class_room_message_path(@class_room, message) %>

The order to specify is the order in the URI.

Recommended Posts

pass two arguments to the URI with link_to
You can't (yet) pass arguments to buildkit with docker-compose
Pass parameters to Rails link_to
I want to pass the startup command to postgres with docker-compose.
How to use the link_to method
Pass the i18n locale to JavaScript
Java SE8 Silver ~ The Road to Pass ~
[Form with two arguments] rails, form_for / form_with arguments
How to pass the value to another screen
[Rails] When transitioning to a page with link_to, move to the specified location on the page
Print forms directly to the printer with JasperReports
Assignment to multiple variables with the ternary operator
[Swift] How to link the app with Firebase
Getting Started with Doma-Introduction to the Criteria API
Try to summarize the common layout with rails
Use the --build-arg option of docker-compose to pass environment variables to the container built with Dockerfile
Pass arguments to the method and receive the result of the operation as a return value
How to remove the underline displayed by Rails link_to
Pass User credential to deployment destination with Jenkins + Capistrano
`Failed to create the host-only adapter` with docker-machine start`
I want to dark mode with the SWT app
Organized how to interact with the JDK in stages
HTTPS connection with Java to the self-signed certificate server
Addicted to the webpacker that comes standard with Rails 6