[RUBY] Try using simple_form / edit even child models

Overview

simple_form is a gem that automatically creates high-performance forms. https://github.com/heartcombo/simple_form Not only does it create a form, it also automatically displays error messages and item names.

This time it ’s a simple rails project with two tables. I tried using simple_form

image.png

Implementation

First, implement only the simplest implementation user model

gemfile


gem 'simple_form'

user.rb


class User < ApplicationRecord
  validates :name, presence: true
  validates :email, presence: true
  validates :age, presence: true
end

routes.rb


  resources :users

users_controller.rb


class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to user_path(@user)
    else
      render 'users/new'
    end
  end

  private

  def user_params
    params.require(:user).permit(
      :name,
      :email,
      :age,
      :date
    )
  end

Execute the following migration file. `` `rails db: migrate```

db/migrate/20200906_create_users.rb


class CreateUsers < ActiveRecord::Migration[5.1]
  def change
    create_table :users do |t|
      t.string :name
      t.string :email
      t.integer :age
      t.datetime :date
      t.string :url

      t.timestamps
    end
  end
end

Write simple_form in the view file (new). It's almost a copy of the readme

ruby:app/views/users/new.html.erb


<%= simple_form_for user do |f| %>
  <%= f.input :name %>
  <%= f.input :email %>
  <%= f.input :age, collection: 18..60 %>
  <%= f.input :date %>
  <%= f.input :url %>
  <%= f.button :submit %>  
<% end %>

The form is now displayed. The result of accessing localhost: 3000 / users / new is as follows. image.png

Items for which presence validation is set are automatically displayed as can't be blank.

In addition, simple_form judges email and url from the column name, and each will perform the optimum validation. image.png

image.png

The design of the balloon was also generated by simple_form without permission.

Also, as you can see by looking at the date and age items, it will change the input format arbitrarily according to the type.

This is just an example Various input patterns such as color and country are available. (It is written in reademe) How to use simple_form is like this.

If you want to edit the child model together

You can edit the child model associated with user from the user model. It is used when you want to edit at the same time on a page where the user model is the main.

This time, we will prepare a child model called user_property. Not the create action of user_properties_controller Make it possible to edit with create of users_controller. (same for update

It also allows you to combine the submit buttons into one.

Below is the file that has changed from the previous time.

user_property.rb


class UserProperty < ApplicationRecord
  belongs_to :user

  validates :nickname, presence: true
  validates :hobby, presence: true
end

user.rb


class User < ApplicationRecord
  validates :name, presence: true
  validates :email, presence: true
  validates :age, presence: true

 has_one :user_property
  accepts_nested_attributes_for :user_property
end

users_controller.rb


class UsersController < ApplicationController
  def new
    @user = User.new
  @user.create_user_property(nickname: "takashi", hobby: "yamamoto")
  end

  private

  def user_params
    params.require(:user).permit(
      :name,
      :email,
      :age,
      :date,
       user_property_attributes: %i[
        nickname
        hobby
      ]
    )
  end

Execute the following migration file. `` `rails db: migrate```

db/migrate/20200906_create_user_property.rb


class CreateUserProperties < ActiveRecord::Migration[5.1]
  def change
    create_table :user_properties do |t|
      t.integer :user_id
      t.string :nickname
      t.string :hobby

      t.timestamps
    end
  end
end

Simple_form is written as follows. ---- user_proiperty ----- The following items have been added. Use simple_field_for.

ruby:app/views/users/new.html.erb


<%= simple_form_for user do |f| %>
  <%= f.input :name %>
  <%= f.input :email %>
  <%= f.input :age, collection: 18..60 %>
  <%= f.input :date %>
  <%= f.input :url %>

-------user_property----------  

  <%= f.simple_fields_for :user_property, user.user_property do |ff| %>
    <%= ff.input :nickname %>
    <%= ff.input :hobby %>
  <% end %>

  <%= f.button :submit %>  
<% end %>

The result is as follows Two items for user_property have been added. That's it. image.png

Like user, I get a validation error image.png

that's all

Recommended Posts

Try using simple_form / edit even child models
Try using libGDX
Try using Maven
Try using powermock-mockito2-2.0.2
Try using GraalVM
Try using jmockit 1.48
Try using sql-migrate
Try using SwiftLint
Try using Log4j 2.0
Try using Axon Framework
Try using JobScheduler's REST-API
Try using PowerMock's WhiteBox
Try using Talend Part 2
Try using Talend Part 1
Try using F # list
Try using each_with_index method
Try using Spring JDBC