Posted for memorandum.
ruby 2.7.1 Rails 6.0.3.2
It is a prerequisite that Rails has been installed.
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.
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)
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