0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【hidden_field】rails hidden_field を使って情報を送ろう!!!!

Posted at

##【ゴール】
hidden_fieldを使って情報を送る

画面収録 2020-06-21 20.06.48.mov.gif

##【メリット】
■ paramaterの理解度向上
■ hidden_fieldの使い方理解
■ MVCの理解度向上

##【開発環境】
■ Mac OS catalina
■ Ruby on Rails (5.2.4.2)
■ Virtual Box:6.1
■ Vagrant: 2.2.7

##【実装】

####アプリケーション作成
※「scaffold」で一気に作成

mac.terminal
$ rails new hidden_lesson
$ rails g scaffold Item name:string text:text price:integer amount:integer
$ rails db:migrate 

##アプリケーション調整

※ルートを追記
※homeにitemsのindexページが来るように
※postメソッドで確認画面へのルートを作成

config/routes.rb
 root 'items#index'
 post 'items/confirm' => 'items#confirm'

※views/itemsにconfirm.html.erbを作成
※items/_form.html.erbを編集
hidden_fieldでUserには見えないようにページで情報を保持します!!!

items/confirm.html.erb
<h3>Confirm page</h3>
<%= form_with model:@item do |form| %>

<div class="field">
  <%= form.label :name %>
  <%= @item.name %>
  <%= form.hidden_field :name %>
</div>

<div class="field">
  <%= form.label :text %>
  <%= @item.text %>
  <%= form.hidden_field :text %>
</div>

<div class="field">
  <%= form.label :price %>
  <%= @item.price %>
  <%= form.hidden_field :price %>
</div>

<div class="field">
  <%= form.label :amount %>
  <%= @item.amount %>
  <%= form.hidden_field :amount %>
</div>

<div class="actions">
  <%= form.submit %>
</div>
<% end %>

items/_form.html.erb
<%= form_with(model: item, local: true) do |form| %> #これを↓に変更

<%= form_with(model: item, local: true, url: items_confirm_path) do |form| %>

以上です。商品詳細画面遷移前に確認画面が挟まれているはずです。

##【合わせて読みたい】
■form_withに関して
https://qiita.com/tanaka-yu3/items/50f54f5d4f4b8dfe19f3

■local: trueに関して
https://qiita.com/hayulu/items/5bf26656d7433d406ede

■確認画面作成に関して
https://qiita.com/tomoharutt/items/7959d28764912c64562f

■formの引数が2つの場合
https://qiita.com/tanaka-yu3/items/94d2b9fccc9577756127

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?