LoginSignup
3
5

More than 5 years have passed since last update.

メモ_form_for と accepts_nested_attributes_for使用時の注意(複数テーブル更新に当たってのparamsの返し方)

Last updated at Posted at 2016-12-21

以下メソッドで複数テーブル(groups,groups_users)を更新したかったが,
groups_usersが更新できない。
原因は、paramsの作り方がうまくいっていなかった。

form_forに渡すモデルインスタンスに合わせたform_forを記述しないといけない。

=================

accepts_nested_attributes_for

・モデルクラスに設定する。
・saveメソッドが走った時に複数テーブルを保存・更新することを可能にする。

groups_controller.rb
class Group < ApplicationRecord
  has_many :groups_users
  has_many :users, through: :groups_users
  has_many :messages

  accepts_nested_attributes_for :groups_users

end

form_for

・ビューに記述するヘルパーメソッド。

・formタグに変換されて表示される。

・引数として、事前に生成したモデルクラスのインスタンスを渡すことで、
 それらに容易に値を入れ、paramsとして返すことができる。

groups_new.html.haml
.chat-group-form
  %h1 新規チャットグループ
  %div#new_chat_group.new_chat_group
    = form_for @group do |f|
      .chat-group-form__field.clearfix
        .chat-group-form__field--left
          = f.label :グループ名, class: "chat-group-form__label", for: "chat_group_name"
        .chat-group-form__field--right
          = f.text_area :group_name, row: "1", placeholder: "グループ名を入力してください", id: "chat_group_name", class: "chat-group-form__input"
      .chat-group-form__field.clearfix
        .chat-group-form__field--left
          = f.label :チャットメンバーを追加, class: "chat-group-form__label", for: "chat_group_チャットメンバーを追加"
        .chat-group-form__field--right
          .chat-group-form__search.clearfix
            -# = f.text_area :name,row: "1", placeholder: "追加したいユーザー名を入力してください", id: "chat_group_name", class: "chat-group-form__input"
          #user-search-result
      .chat-group-form__field.clearfix
        .chat-group-form__field--left
          = f.label :チャットメンバー, class: "chat-group-form__label", for: "chat_group_チャットメンバー"
        .chat-group-form__field--right
          #chat-group-users
            #chat-group-user-22.chat-group-user.clearfix
              -# = f.text_area :user_id,type: "hidden",value: "22"
              %input{:name => "group[user_ids][]", :type => "hidden", :value => "1"}/
              %p.chat-group-user__name seo_kyohei
      .chat-group-form__field.clearfix
        .chat-group-form__field--left
        .chat-group-form__field--right
          = f.submit "Save", name: "commit", class: "chat-group-form__action-btn"

%p
  = debug params
  nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
  = debug @group

注意すべきはココ→ group[user_ids][]

インスタンスクラスに無い項目(accepts_nested_attributes_forの宛先)を、
paramsに含めるには、インスタンスクラス名を冠してやる必要がある。
結果、取れるparamsはこちら。

[1] pry(#<GroupsController>)> params
=> <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"tF+sdXczvVCg5d7csR1e1hGwKGzVuTfhJbcA+AcdgDmYOag4b/XG/+hc4ncmeJFSZueHBOirLYEMy5yr/ruPUA==", "group"=><ActionController::Parameters {"group_name"=>"uuuuuuuuuuuuuuu", "user_ids"=>["1"]} permitted: false>, "commit"=>"Save", "controller"=>"groups", "action"=>"create"} permitted: false>
[2] pry(#<GroupsController>)> 

ちなみに、[user_ids][]このようにしてgroupを冠していないと以下となる
(user_idsは複数もつため配列にしている。よって複数系名称。
 単数であれば、user_id = "1" と書く)

[1] pry(#<GroupsController>)> params
=> <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"vS1ULT/ynDkOCsyg4hIjZqSb+yZQ8zEDbWqcqynx3ICRS1BgJzTnlkaz8At1d+zi08xUTm3hK2NEFgD40FfT6Q==", "group"=><ActionController::Parameters {"group_name"=>"hhhhhhhhhhhhh"} permitted: false>, "user_ids"=>["1"], "com
[2] pry(#<GroupsController>)> 

つまりは 
<"group"=><ActionController::Parameters >

の 中に入る・入らないの違い。ここで、モデルインスタンスに関連する事項かどうかみているようだ。
含めることができていないと、saveメソッドに拾われない。

3
5
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
3
5