I don't understand how to write Rails routing.

Asked 2 years ago, Updated 2 years ago, 341 views

When I was looking at Rails routing, there was a description that I didn't understand, so I would like to ask you a question.
Am I correct in understanding that I will go to see the sample#show when I transition to /sample(ya/sample/hoge)?

 resource:sample,controller::sample,only:[:show] do
  get '*other', to: 'sample#show'
end

By the way, this sample#show is going to read SPA files

def show
  render file:Rails.public_path.join('dist', 'web', 'sample', 'index.html', layout:false
end

It is written as follows:

on the SPA side

/sample
/sample/confirm
/sample/complete
There are screen files equivalent to .

ruby-on-rails

2022-09-30 21:55

1 Answers

The get'*other', to:'sample#show' portion uses a routing glove, which is routed to sample#show when you try to get sample/somehow.The params[:other] part now goes into params[:other].

https://railsguides.jp/routing.html#Routing Globes and Wildcard Segments

A routing globe (route globbing) is a wildcard deployment that is used to match a particular parameter from a routing position to all parts below.Example:

get 'photos/*other', to: 'photos#unknown'


2022-09-30 21:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.