4
4

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 5 years have passed since last update.

hidden_fieldにenumの値を持たせる

Posted at

はじめに

submitボタンのみを利用してdbの情報を更新したい時に、hidden_fieldをしようとすると、valueの持たせ方やenumの場合のvalueの持たせ方がすぐにみつからなかったので調べました。

hidden_fieldの基本的な書き方

hidden_field(オブジェクト名, プロパティ名 [, オプション])

enumを利用する場合の書き方

<%= f.hidden_field :approval, {value: 'enumで定義したキー名'} %>

model
enum approval: { pending: 0, approved: 1, rejected: 2 }
view
<%= form_for ([@user, interview]) do |form| %>
  <%= f.hidden_field :approval, {value: 'approved'} %>
  <%= f.submit %>
<% end %>

参照

Set a Rails form hidden field from enum model - Stack Overflow

4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?