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
db/seed.rb
User.create(name: "tanaka") User.create(name: "sato")
users = User.create([{name: "tanaka"}, {name: "sato"}])
100.times do |n| User.create (title: "test {n} th", content: "test! # {n} th") end
$ 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.
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
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.
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