[Order function] rails Order function implementation Confirmation screen will also be created ~

【goal】

Create a search window where you can search for model information

【merit】

■ Improvement of UX ■ Improved understanding of MVC

【Development environment】

■ Mac OS catalina ■ Ruby on Rails (5.2.4.2) ■ Virtual Box:6.1 ■ Vagrant: 2.2.7

【Implementation】

Application creation

mac.terminal


$ rails new search
$ rails g model Item name:string text:text price:integer
$ rails g controller items
$ rails g model Order quantity:integer amount:integer item:references
$ rails g conrtroller orders show confirm 
$ rails db:migrate

Edit model

models/items.rb


has_many :orders

Edit route

config/routes.rb


root 'items#index'
  resources :items do 
    get 'order' => 'orders#show'
    post 'order/confirm' => 'orders#confirm'
    post 'order' => 'orders#create'
  end

Add view

items/index.html.erb


<h2>Item List</h2>

<% @items.each do |item| %>
  <%= item.name %>
  <%= item.text %>
  <%= item.price %>
<% end %>

<h2>Item creation</h2>

<%= form_with modle: @item do |f| %>
  <%= f.text_field :name %>
  <%= f.text_area :text %>
  <%= f.text_field :price%>
  <%= f.submit %>
<% end %>

order/show.html.erb


<h2>Order screen</h2>

<p>Item information</p>
  <%= @item.name %>
  <%= @item.price %>

<%= form_with ([model: @item, @order],local: true url: item_order_confirm_path) do |f| %>
  <%= f.text_field :quantity %> <br>
  <%= f.text_field :amount %> <br>
  <%= f.submit "Send" %> <br>
<% end %>

order/confirm.html.erb


<h2>Order confirmation screen</h2>
  <%= @order.amount %>
  <%= @order.quantity %>
  <%= @item.name %>
  <%= @item.price %>
<%= form_with model:[@item, @order], url:(item_order_path) do |f| %>
  <%= f.hidden_field :amount %>
  <%= f.hidden_field :quantity %>
  <%= f.submit %>
<% end %>

Edit controller

orders_controller.rb


  def show
    @item = Item.find(params[:item_id])
    @order = @item.order.new
  end

  def confirm
    @item = Item.find(params[:item_id])
    @order = @item.order.new(order_params)
  end

  def create
    @item = Item.find(params[:item_id])
    @order = @item.order.new(order_params)
    @order.save
    redirect_to items_path
  end

  private
  def order_params
    params.require(:order)
    .permit(:amount,
            :quantity,
            :item_id)
  end

that's all.

[I want to read it together]

■ About form_with https://qiita.com/tanaka-yu3/items/50f54f5d4f4b8dfe19f3

■ Regarding confirmation screen display https://qiita.com/tomoharutt/items/7959d28764912c64562f

Recommended Posts

[Order function] rails Order function implementation Confirmation screen will also be created ~
Rails search function implementation
Rails fuzzy search function implementation
[Rails 6] Implementation of search function
[Rails] Implementation of category function
Rails sorting function implementation (displayed in order of number of like)
Login function implementation with rails
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails 6] Pagination function implementation (kaminari)
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
[rails] Login screen implementation in devise
[Rails] Implementation of image preview function
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Rails] gem ancestry category function implementation
[Ruby on Rails] Comment function implementation
[Rails] "Input"-> "Confirmation screen"-> "Save"-> "View"
[Rails 6] Like function (synchronous → asynchronous) implementation
[Rails] Comment function implementation procedure memo
[Rails] I will explain the implementation procedure of the follow function using form_with.
[Ruby on Rails] Follow function implementation: Bidirectional
Rails [For beginners] Implementation of comment function
Rails Basic CRUD function implementation procedure scaffold
[Rails 6] Implementation of SNS (Twitter) sharing function
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Vue.js] Implementation of menu function Vue.js introduction rails6