1
1

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.

多対多の中間テーブルを<table>で一覧表示して編集するまで

Last updated at Posted at 2019-04-23

何に使うの?って内容ですが、自分用の備忘録です。
学習1か月ほどなので参考にはならないと思います。
メモ書きレベル

#イメージ
2019-04-24_013336.jpg

#参考
railsでラジオボタンを作る方法(モデル連携なし版)
https://ch.nicovideo.jp/tokyo-webs/blomaga/ar222386

Railsのform_tagで生成されるHTML
https://maeharin.hatenablog.com/entry/20120811/p1

Ruby :: ハッシュを配列に変換する
http://tm.root-n.com/programming:ruby:etc:hash2array

Ruby - 配列やハッシュの繰り返し each
http://azuuun-memorandum.hatenablog.com/entry/2016/04/12/213839

Ruby on Rails データ更新
https://qiita.com/shell/items/c9869809cba91e9c35a1

【Rails 5】(新) form_with と (旧) form_tag, form_for の違い
https://qiita.com/hmmrjn/items/24f3b8eade206ace17e2

#コード

controller

def show
  @workspace = Workspace.find(params[:id])
  @photos = @workspace.photos.all
  @users = User.all
end

 view

    # userの人数分テーブルを横に伸ばす     
          <% @users.each do |user| %>
            <th><%= user.name %></th>
          <% end %>
         </tr>

       #photoの枚数分レコードを表示   
        <% @photos.each do |photo| %>
          <tr>
            <% @users.each do |user| %>
              <td>
                <div class="btn-group btn-group-xs btn-group-vertical" data-toggle="buttons" role="group">

                  #helperにアクション記述
                  <% get_relation(photo, user) %>   

 helper

  # photo一枚毎のリレーションを取得する
  def get_relation(photo, user) 
    relationship = Relationship.where(photo_id: photo.id).where(user_id: user.id).first
    @target1 = ""
    @target2 = ""

    if relationship 
      if relationship.target == 1 then
        @target1 = " active"
      end
      if relationship.target == 2 then
        @target2 = " active"
      end
    
    else
    end
  end

 view

                  # helperで生成した文字列でデフォルト値を設定
                  #トグルボタンは同じname属性でグルーピングされるのでidを文字列結合してあとで取得できるように
                  <label class="btn btn-default<%= @target1 %>">
                    " 1 "<%= radio_button 'target', photo.id.to_s + '_' + user.id.to_s , 1, {} %>
                  </label>
                  <label class="btn btn-default<%= @target2 %>">
                    " 2 "<%= radio_button 'target', photo.id.to_s + '_' + user.id.to_s , 2, {} %>
                  </label>
                  
                </div>
              </td>
            <% end %>
          </tr>
        <% end %>
      
      </table>
      <%= submit_tag ' 送信する ' %>
    <% end %>

controller

def create
  @relationship_keys = params[:target]
  @relationship_keys.each do |key,value|

    key = key.split("_")
    photo_id = key[0]
    user_id = key[1]
  
    relationship = Relationship.where(photo_id: photo_id ).where(user_id: user_id).first

    if relationship
      relationship.target = value
    else
      relationship = Relationship.new(user_id: user_id, photo_id: photo_id, target: value)
    end
  
    if relationship.save
    else
    end
  end
  
  flash[:success] = 'stasu更新完了'
  redirect_to workspace_path(params[:workspace_id])

end      
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?