[RUBY] Basics such as devise pagination

A digression that is easy to forget

Instance variables defined within a controller's action can be used in the view for that action.

The value stored in the column to be written is called property value </ strong>.

Helper method

Rails provides methods for making HTML tags appear in view and processing text in advance. These are called helper methods. simple_format A method that adds <br> with line breaks and encloses the character string in <p> form_tag Method to make the form appear link_to Method to make a tag appear And so on.

The CSS file is read from stylesheet_link_tag in the HTML head tag.

require_tree means to read all CSS files under the directory given as an argument in alphabetical order. . Is an argument and represents the current directory.

application.css


require_tree .

MVC Rails processes in the order of routing → controller → model → view. A system that uses a model-view controller to perform processing is called MVC, which is an acronym for each.

Form

A form is for a user to enter information and send that information to a server. You can create it by creating a form element in the HTML code and putting an input element and a textarea element in it. You can create a form with the form tag, but for security reasons, Rails recommends using the helper methods form_tag, form_for, and form_with.

The values entered by the user in the form are contained in a variable called params in the controller. Think of params as a hash object. The information entered in the form in the view is sent to the controller as a parameter along with the key. This parameter can be obtained by using a method called params. How to use

params[:Key name]

The value of the name attribute in the input or textarea in the form corresponds to the key name.

Strong parameters

A strong parameter is one that receives only parameters with the specified key. Security can be strengthened by preventing the receipt of unauthorized information. It can be defined by creating a method called arbitrary strong parameter name _params.

xxxxx.rb


private
def sample_params    ###Any strong parameter name_params
  params.permit(:Key name, :Key name)
end

In the method, write params.permit and follow the key name of the parameter that is allowed to be received. The permit method generates a new hash with the name of the key that follows the params, which is a hash of the information sent from the view, along with the value.

private method

If you write private in class, the methods defined after that cannot be called from outside the class. You can protect the methods that are troublesome if called from the outside, and you do not have to read private and below, which improves the readability of the code.

image_tag In Rails, images such as logos and banners that you want to display fixedly are usually placed under app / assets / images.

sample.html.erb


<%= image_tag 'sample.png' %>

pry-rails pry-rails is one of the gems and is a debugging tool for Rails. By using this, you can check for bugs or stop the process and check if the source code is correct. The function I especially use is binding.pry. You can use it to stop the process and do the same thing as the rails console. Write binding.pry in the source code you want to stop. You can check if the process is performed correctly by getting the value of the variable or setting multiple binding.prys. If you want to resume the process, enter ʻexit` in the terminal.

sample_controller.



def create
  Sample.create(sample_params)
binding.pry
end

private
def sample_params
  params.permit(:name, :image, :text)
end

Post on the posting screen. (new.html.erb)

Then the console will be launched in the terminal

Terminal.


[1] pry(#<SamplesController>)> params
=> <ActionController::Parameters {"authenticity_token"=>"DnHBKql5oQq9lARgEnK8gQiFAhnTvIVa1XH8d13u7tuVjDUGWPM1eEjVbF44iFms57+VE7+vT3SYUePDy3lwWQ==", "name"=>"aa", "image"=>"https://kumamoto.photo/archives/_data/i/upload/2019/03/13/20190313172208-2113bf9d-la.jpg ", "text"=>"dddddddd", "controller"=>"samples", "action"=>"create"} permitted: false>
[2] pry(#<SamplesController>)> params[:text]
=> "dddddddd"

If you enter params on the console, the value entered on the posting screen will be output as a parameter along with the key name.

Type ʻexit` to resume processing.

Root path

A URL with only a host name without a path is called a root path. How to write in routes.rb

routes.rb


root 'Controller name#Action name'

order method

A method to sort the instances. If you have fetched all the records, you can sort the records.

.order("Column name order specification ")

ASC is in ascending order, from the oldest date DESC is in descending order, from the newest date

Pagination

Something that splits the page. You can decide the number of items to be displayed per page. It can be implemented by using gem kaminari.

page method You can specify the number of pages with this method. It's not the total number of pages, but how many pages you are on now.

per method You can decide the number of items to be displayed per page.

sample_controller.


@samples = Sample.order("id ASC").page(params[:page]).per(10)

As for the argument of the page method, the key called page was added to the model class when kaminari was introduced, and it is described. The value of that key will be the page number specified in the view.

devise-login function-

It is a gem that can easily implement the login function.

Describe devise in Gemfile After bundle install In the app directory When you hit rails g devise: install A configuration file is created. config / initialize / devise.rb and config / locales / devise.en.yml.

Because I have to create a model for creating an account Create related files such as models with rails g devise model class name. A model file and a migration file are created. At the same time, in routes.rb

devise_for :Plurale of model name

Will be added. devise_for is a devise helper method that will generate the necessary routing around login at once.

Helper methods such as current_user, user_signed_in? Can also be used.

You can create a view file for your device with rails g devise: views.

Prefix A variable that contains the routing path.

unless statement

Describes the processing to be performed when the conditional expression is false, which is the opposite of the if statement.

unless conditional expression
Processing to be executed when fake
end

If the conditional branch is one line, it can be described in one line.
puts "Please login" unless user_signed_in?
###If you are not logged in (ie fake)"Please login"Is displayed

redirect_to method

In Rails, basically, when the processing of the action is finished, it will transition to the view with the same name as the action, but you can use the redirect_to method to execute another action or transition to the view.

redirect_to { action: :index }

Takes ʻaction:as the key and the symbolic type of the action name as the value. For example,: index`. {} Can be omitted.

before_action method

You can specify the method you want to execute before the action in the controller executes. It can be specified by the method name of the symbol type. There are options ʻexcept and ʻonly, and you can limit each action.

Add column to table

rails g migration Add Column name To Table name Column name:Mold

configure_permitted_parameters method

When implementing the login function with devise, the way of receiving parameters is different from usual. By default, the strong parameter is set to receive only your email address and password. Use the configure_permitted_parameters method to add it.

application_controller.rb


before_action :configure_permitted_parameters, if: :devise_controller?

def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:Action name you want to add, keys: [:Key to add])
end

application_controller.rb


class ApplicationController < ActionController::Base
#  protect_from_forgery with: :exception
 before_action :configure_permitted_parameters, if: :devise_controller?

 def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname])
 end
end

Recommended Posts

Basics such as devise pagination
[Rails] Introduction of devise Basics