Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails Good Parts, Bad Parts

Rails Good Parts, Bad Parts

「MedBeer -Rails開発での技術的負債との付き合い方」での発表資料です

Shinichi Maeshima

September 12, 2018
Tweet

More Decks by Shinichi Maeshima

Other Decks in Technology

Transcript

  1. 自己紹介 » 前島真一 aka @willnet or @netwillnet » ginza.rb から来ました

    » メドピアさんで技術顧問しています » https://github.com/willnet » https://twitter.com/netwillnet » https://blog.willnet.in
  2. 素直に書くと <% Tag.all.each do |tag| %> <%= form.label tag.name do

    %> <%= check_box_tag 'beer[tag][id][]', tag.id, beer.tags.find { |t| tag.id == t.id } %> <%= tag.name %> <% end %> <% end %>
  3. 素直に書くと def update @beer = Beer.find(params[:id]) @beer.attributes = beer_params original_taggings

    = @beer.taggings taggings_ids = params[:beer][:tag][:id] delete_taggings = original_taggings.reject { |bt| taggings_ids.include?(bt.tag_id.to_s) } delete_taggings.each(&:destroy) add_tags = taggings_ids.reject { |id| original_taggings.map(&:tag_id).include?(id.to_i) } add_tags.each do |tag_id| @beer.taggings.build(tag_id: tag_id) end if @beer.save redirect_to @beer, notice: 'Beer was successfully updated.' else render :edit end end private def beer_params params.require(:beer).permit(:name) end
  4. 便利メソッドたちを使うと def update @beer = Beer.find(params[:id]) if @beer.update(beer_params) redirect_to @beer,

    notice: 'Beer was successfully updated.' else render :edit end end private def beer_params params.require(:beer).permit(:name, tag_ids: []) end