[For beginners] Procedure for creating a controller using rails

I learned how to create a rails controller with dot installation, so I will summarize it.

This article is recommended for people like this
  • People who have just started learning programming
  • People who are learning programming with dot installation
  • People who want to review how to create an application with rails

I wrote that, but since the purpose behind it is for my own review, there may be places where it is difficult to convey the wording. I will try to convey the correct information, so if you have any concerns, please let me know.

Prerequisites
  • Rails new has created an application
  • Start the server with rails s in the created application directory

Up to this point, you need to do it first.

Creating a controller ``` rails g controller posts #posts is the controller name ``` This should work. Please note that the controller name is specified in plural . If it works
create  app/controllers/posts_controller.rb
      invoke  erb
      create    app/views/posts
      invoke  test_unit
      create    test/controllers/posts_controller_test.rb
      invoke  helper
      create    app/helpers/posts_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    scss
      create      app/assets/stylesheets/posts.scss

It will be displayed like this. Looking at the directory app> controller> controller name_controller.rb (specified by rails g controller ~) File should have been created.

class PostsController < ApplicationController
end

In the initial state of the controller, this code is included. By inputting a method (action) here, it is possible to take an action according to the request from the user.

Routing settings Routing… Decide which action of which controller to perform in response to a request from a user

config>routes.rb Since there is a file called, set it here.

Rails.application.routes.draw do
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

It looks like this in the initial state. If you want to put together general processing

resources :posts

It is good to set.

If you want to check the set routing, in the terminal

rails routes

Enter. Then routes are listed like this

 Prefix Verb   URI Pattern                                                                              Controller#Action
                                posts GET    /posts(.:format)                                                                         posts#index
                                      POST   /posts(.:format)                                                                         posts#create
                             new_post GET    /posts/new(.:format)                                                                     posts#new
                            edit_post GET    /posts/:id/edit(.:format)                                                                posts#edit
                                 post GET    /posts/:id(.:format)                                                                     posts#show
                                      PATCH  /posts/:id(.:format)                                                                     posts#update
                                      PUT    /posts/:id(.:format)                                                                     posts#update
                                      DELETE /posts/:id(.:format)                                                                     posts#destroy
        rails_postmark_inbound_emails POST   /rails/action_mailbox/postmark/inbound_emails(.:format)                                  action_mailbox/ingresses/postmark/inbound_emails#create
           rails_relay_inbound_emails POST   /rails/action_mailbox/relay/inbound_emails(.:format)                                     action_mailbox/ingresses/relay/inbound_emails#create

There are actually a few more, but omitted

posts GET    /posts(.:format) posts#index

If you pay attention to this one line, the meaning is

Perform the posts controller's index action on the page with the URL / posts when a GET (a pattern of requests from users) is executed

means.

Write the action on the controller

Write the action in the controller you created earlier.

class PostsController < ApplicationController
end

We will write the action as a method of the PostsController class. I want to use the get> index action mentioned above this time

class PostsController < ApplicationController
  def index
    @posts = Post.all.order(created_at: 'desc')
  end
end

I will give it as.

The variable here is an instance variable (with "@") because we want to use this variable outside the controller class as well.

Post… The model created this time Model: Corresponds to the type of data in the database. For example, in the case of a chat application, models such as User (user information) and Text (interaction in chat) can be considered. Think of it as a type called Post. Post.all… Get all the data of the Post model (created_at'desc')… Get from newest to newest

You have now created a controller and action.

I'm glad if you can use it as a reference.

[Promotion] In my main business, I am a lecturer at a programming school. (But I'm still training.) We plan to update note and twitter, focusing on what we noticed as a lecturer. If you find this article helpful, please follow us. Nice to meet you!

Click here for note Click here for twitter

Recommended Posts