Rails Routing Basics

Posted for memorandum.

environment

ruby 2.7.1 Rails 6.0.3.2

It is a prerequisite that Rails has been installed.

Create the required files with the generate command

bundle exec rails g controller <controller_name> <method_name>

↓

bundle exec rails g controller home index

The necessary files are created. If you want to delete it, delete command

 bundle exec rails d controller home index

routes.rb

bundle exec rails routes

or

bundle exec rails routes | grep xxx
(Narrow down)

You can see the routing settings in the directory.

Points to see

 URI Pattern                        Controller#Action
  /articles/index(.:format)         articles#index

You can jump to the Action index of Controller articles by accessing / articles / index. You can see it by starting the server with rails s and accessing localhost :: 3000. As a Rails rule, the Controller name has a hierarchical structure on the View side as it is.

Example: HomeController (controller name) View directory → home (directory name) /index.html.erb (file name)

How to pass values from Controller to View

Use instance variables

class HomesController < ApplicationController
  def index
    #Instance variables
    @message = "message"
  end
end

You can call the value anywhere in the Controller by prepending @ to the variable. You can also pass the value of a variable to View. In this example, the string "message" contained in the instance variable @message is passed.

<h1>Homes#index</h1>
<%= @message %>

If you want to call Ruby in HTML, you can use <%%>. If you want to output something, add <% =%> and it will be output. Since we are passing the instance variable @message, the string "message" is displayed in HTML.

Recommended Posts

Rails Routing Basics
About Rails routing
Rails database basics
Rails Logger Basics
Rails 6.0 Routing Summary
Rspec Basics [Rails]
Catch Rails Routing Error
[Rails] devise-related routing summary
Ruby on Rails basics
[Note] Rails3 routing confirmation
Understand Rails "shallow" routing
[Rails] Complete routing settings
[Rails] Introduction of devise Basics
Organize Rails routing using draw
routing
Rails routing controller view relationship
How to write Rails routing
Rails singular resource routing by resource
[Rails] Summary of complicated routing configurations
Set Rails routing other than id
Ruby On Rails devise routing conflict
[Rails g.error]
Rails Review 1
Rails API
Rails migration
Ruby basics
Ruby basics
[Rails] first_or_initialize
rails tutorial
[Ruby on Rails] 1 model CRUD (Routing Main)
Fragment basics
JPA Basics 1
About Rails 6
Docker basics
ViewPager basics
Rails / users /: id / to / {random_srting}: Dedefault Routing
Rails foundation
Rails memorandum
rails tutorial
Java basics
rails tutorial
rails tutorial
Java basics
RSpec Basics
[rails] How to configure routing in resources
Rails Basics of creating a new application
Nesting routing ...
JavaScript basics
[Rails] devise
rails tutorial
JPA Basics 2
rails tutorial
Hash basics
Integrated framework Apache Camel message routing basics
Java basics
Rails Tips
rails method
rails tutorial
Ruby basics
[Rails] ActiveRecord
[Rails] form_with