rails tutorial chapter 10 summary (for self-learning)

What is seeds.rb

A file that generates initial data. Simply write the code that will generate the data in this file and your app will have the data. You can easily create test data.

How to use

1. Create the data you want to prepare in the file.


Normal case

User.create(name: "tanaka") User.create(name: "sato")

For arrays

users = User.create([{name: "tanaka"}, {name: "sato"}])

For large amounts of test data

100.times do |n| User.create (title: "test {n} th", content: "test! # {n} th") end

2. Run the seed file

$ rails db:seed bonus Since there is a gem (Faker) that generates data that seems to actually exist, I will post it for the time being.

  1. Add the following to Gemfile

gem 'faker' Don't forget to do this as well.

$ bundle install 2. Write seeds.rb as follows.

db/seed.rb 100.times do |n| name = Faker::Name.name email = Faker::Internet.email content = "test"

User.create( name: name, email: email, content: content ) end 3. Run the seed file.

$ rails db:seed

Type Role Faker :: Name.name Name Faker :: Internet.email Email Address Faker :: Address.city City name Faker :: Food.ingredient Food name

What is pagination?

Rails has a wealth of pagination methods. This time, let's use the simplest and most robust will_paginate method. To use this, you need to include both will_paginate gem and bootstrap-will_paginate gem in your Gemfile and configure will_paginate using Bootstrap's pagination style. First, let's add each gem to the Gemfile

① Add will_paginate to Gemfile ②bundle install

render @**

When render not only reads a partial but also gives a variable such as @user, rails automatically calls a collection of User objects, lists them, and then assigns data to the _user partial and outputs it.

before method (used by controller)

By using the before_action method, you can execute the before_action method before performing a specific action in the controller. (Default is all actions,: only can specify a specific action) Usage example: Update, delete user information, etc.

How to write

before_action :logged_in_user, only: [:index, :edit, :update, :destroy]

Strong Parameters
 You can use Strong Parameters to specify the required and allowed parameters. In addition, passing the entire params hash as above will result in an error, so Rails is now protected from mass assignment vulnerabilities by default.

 In this case, I want the params hash to require the: user attribute, allow each of the name, email address, password, and password confirmation attributes, and not the others. To do this, write:

#### **`params.require(:user).permit(:name, :email, :password, :password_confirmation)`**

The return value of this code is a hash of params that contains only allowed attributes (an error will occur if there is no: user attribute).

To make these parameters easier to use, it is customary to use an external method called user_params. This method returns a properly initialized hash and is used as an alternative to params [: user].

data:{confirm: } Rails provides a mechanism to easily display a confirmation dialog when deleting data. By writing data: {confirm:'message'} in the argument of the link_to method shown below, you can display the confirmation dialog built into the browser when you click a link or button.

Recommended Posts

rails tutorial chapter 10 summary (for self-learning)
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
rails tutorial Chapter 7
rails tutorial Chapter 5
rails tutorial Chapter 10
rails tutorial Chapter 9
rails tutorial Chapter 8
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails Tutorial 6th Edition Learning Summary Chapter 4
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 5
Rails Tutorial 6th Edition Learning Summary Chapter 2
Rails Tutorial 6th Edition Learning Summary Chapter 3
Rails Tutorial 6th Edition Learning Summary Chapter 8
Rails Tutorial Chapter 5 Notes
Rails Tutorial Chapter 10 Notes
Rails Tutorial Chapter 3 Notes
Rails Tutorial Chapter 3 Learning
Rails Tutorial Memorandum (Chapter 3, 3.1)
Rails Tutorial Chapter 4 Notes
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Rails Tutorial Chapter 8 Notes
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
Rails Tutorial (4th Edition) Summary
[Rails Tutorial Chapter 4] Rails-flavored Ruby
rails tutorial
rails tutorial
rails tutorial
rails tutorial
Chewing Rails Tutorial [Chapter 2 Toy Application]
rails tutorial
rails tutorial
rails tutorial
Rails Tutorial (4th Edition) Memo Chapter 6
[Rails Struggle/Rails Tutorial] Summary of Heroku commands
Testing for Error Messages: Rails Tutorial Notes-Chapter 7
Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5
[Rails] Method summary for conversion / verification / search
Rails tutorial memorandum 1
Rails tutorial memorandum 2
Rails 6.0 Routing Summary
Start Rails Tutorial
rails db: 〇〇 Summary
[Beginner] Rails Tutorial
A summary of only Rails tutorial setup related
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 6
Rails Tutorial Chapter 1 From Zero to Deployment [Try]
Chewing Rails Tutorial [Chapter 3 Creating Almost Static Pages]
[Rails tutorial] A memorandum of "Chapter 11 Account Activation"
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
Resolve Gem :: FilePermissionError when running gem install rails (Rails Tutorial Chapter 1)
[Rails] Migration command summary
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Chapter 4 Rails Flavored Ruby
Rails Tutorial cheat sheet
[Rails Tutorial] "NoMethodError (undefined method` activation_digest ='for # <User: 0x00000003156938>
[Rails] Learning with Rails tutorial