LoginSignup
2
2

More than 5 years have passed since last update.

[Rails]form_forで一括保存したい時に気をつけたいこと

Posted at

やりたいこと

form_forを使ってchat_groupのnameと、chat_groupに所属するuserを一括で保存したい。
*2つ目のf.text_fieldに入力すると、インクリメンタルサーチでユーザーが表示される仕様

モデル

chat_group.rb
has_many :group_users
has_many :users, through: :group_users
has_many :messages
accepts_nested_attributes_for :group_users
user.rb
has_many :messages
has_many :group_users
has_many :chat_groups, through: :group_users
group_user.rb
belongs_to :chat_group
belongs_to :user

*chat_groupとuserは多対多の関係、中間テーブルにgroup_user

ビュー(失敗)

app/views/chat_groups/new.html.haml
= form_for(@chat_group) do |f|
  .chat-group-form__field.clearfix
    .chat-group-form__field--left
      = f.label :name, "グループ名", class: "chat-group-form__label"
    .chat-group-form__field--right
      = f.text_field :name, class: "chat-group-form__input", placeholder: "グループ名を入力してください"
  .chat-group-form__field.clearfix
    .chat-group-form__field--left
      .chat-group-form__label チャットメンバーを追加
    .chat-group-form__field--right
      = f.text_field :name, type: "text", placeholder: "追加したいユーザー名を入力してください", id: "keyword", class: "chat-group-form__input"

//userテーブルにnameカラムがあるので上記を試してみた。
//しかしf.text_field :nameが被ってしまい、上手くいかない(2つ目に入力されたものがグループ名になってしまう)

失敗その2

app/views/chat_groups/new.html.haml
= form_for(@chat_group) do |f|
  .chat-group-form__field.clearfix
    .chat-group-form__field--left
      = f.label :name, "グループ名", class: "chat-group-form__label"
    .chat-group-form__field--right
      = f.text_field :users, class: "chat-group-form__input", placeholder: "グループ名を入力してください"
  .chat-group-form__field.clearfix
    .chat-group-form__field--left
      .chat-group-form__label チャットメンバーを追加
    .chat-group-form__field--right
      = f.text_field :group_users, type: "text", placeholder: "追加したいユーザー名を入力してください", id: "keyword", class: "chat-group-form__input"

//:nameを:group_usersに変更。するとplaceholderにこんなメッセージが。。。
//#<User::ActiveRecord_Associations_CollectionProxy:0x007fcde18421a8>

解決策:value: ""を書き加える

app/views/chat_groups/new.html.haml
= form_for(@chat_group) do |f|
  .chat-group-form__field.clearfix
    .chat-group-form__field--left
      = f.label :name, "グループ名", class: "chat-group-form__label"
    .chat-group-form__field--right
      = f.text_field :users, class: "chat-group-form__input", placeholder: "グループ名を入力してください"
  .chat-group-form__field.clearfix
    .chat-group-form__field--left
      .chat-group-form__label チャットメンバーを追加
    .chat-group-form__field--right
      = f.text_field :group_users, value: "", type: "text", placeholder: "追加したいユーザー名を入力してください", id: "keyword", class: "chat-group-form__input"

振り返り

モデルに記載したaccepts_nested_attributes_for :group_usersを完全に忘れていた。
f.~と一緒に使われるvalue, nameなどの意味を考えていきたい。
フォームに直接入力して保存するタイプであればfields_forが使えそう。

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