Formオブジェクトを使用した複数テーブルの編集。更新機能の実装
ここに質問の内容を詳しく書いてください。
Formオブジェクトを使用し、複数テーブルの作成・削除機能は実装できましたが、編集がどうしてもうまく実装できません。どのように考え組み立てていけばいいのか、ご教授願いたいです。
- テーブル設計
Table | Column | Type | NotNull |
---|---|---|---|
items | price | integer | ◯ |
// | number | integer | ◯ |
// | unit_price | integer | ◯ |
// | text | string | |
// | user | reference | ◯ |
tags | name | string | ◯ |
item_tag | item | reference | ◯ |
// | tag | reference | ◯ |
--------- | ------------ | ----------- | ---------- |
- ActiveStorageを使用して、itemsにimageを保存できるようにしています。
model
ruby
1class Item < ApplicationRecord 2 belongs_to :user 3 has_many :item_tag_relations 4 has_many :tags, through: :item_tag_relations, dependent: :destroy 5 has_one_attached :image 6 7 def self.search(search, id) 8 if search != "" 9 Item.joins(:tags).where(tags: {name: "#{search}"} , user_id: id) 10 else 11 Item.where(user_id: id) 12 end 13 end 14 15end 16 17class Tag < ApplicationRecord 18 has_many :item_tag_relations 19 has_many :items, through: :item_tag_relations 20 21 validates :name, uniqueness: true 22end 23 24class ItemTagRelation < ApplicationRecord 25 belongs_to :item 26 belongs_to :tag 27end 28
routes
ruby
1#該当部分のみ表示 2resources :items, only: [:new, :create, :show, :destroy, :edit, :update] do 3 collection do 4 get 'tagsearch' 5 end 6 end 7
#### items controller
ruby
1def edit 2 @item = Item.find(params[:id]) 3end 4 5def update 6 @item = ItemsTag.new(update_item_params) 7 if @item.valid? 8 @item.update 9 return redirect_to user_path(current_user.id) 10 else 11 render "edit" 12 end 13end 14 15private 16 17def update_item_params 18 params.require(:items_tag).permit(:image, :price, :number, :unit_price, :text, :name).merge(user_id: current_user.id) 19 end 20
Fromオブジェクト
ruby
1class ItemsTag 2 3 include ActiveModel::Model 4 attr_accessor :image, :price, :number, :unit_price, :text, :name, :user_id 5 6 with_options presence: true do 7 validates :image 8 validates :price 9 validates :number 10 validates :unit_price 11 validates :name 12 end 13 14 def save 15 item = Item.create(image: image, price: price, number: number, unit_price: unit_price, text: text, user_id: user_id) 16 tag = Tag.where(name: name).first_or_initialize 17 tag.save 18 19 ItemTagRelation.create(item_id: item.id, tag_id: tag.id) 20 end 21 22 def update 23 item = Item.update(image: image, price: price, number: number, unit_price: unit_price, text: text, user_id: user_id) 24 tag = Tag.where(name: name).first_or_initialize 25 tag.save 26 27 ItemTagRelation.update(item_id: item.id, tag_id: tag.id) 28 end 29 30end
■■edit/update機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
@item.imageがnilとなってしまう。
35: def update => 36: binding.pry 37: @item = ItemsTag.new(update_item_params) 38: if @item.valid? 39: @item.update 40: return redirect_to user_path(current_user.id) 41: else 42: render "edit" 43: end 44: end [1] pry(#<ItemsController>)> @item = ItemsTag.new(update_item_params) => #<ItemsTag:0x00007f9daf2a6c10 @name="ティッシュ", @number="4", @price="1200", @text="", @unit_price="300", @user_id=1> [2] pry(#<ItemsController>)> @item.image => nil
試したこと
当初controllerのeditアクションの定義を
ruby
1def edit 2 @item = ItemsTag.find(params[:id]) 3end
していたが、ItemsTagクラスにfindメソッドがないとのことから、現在の形に変更したものの
edit.html.erb遷移時にtagのnameの値が受け渡されず、imageもnilとなってしまった。
元の考え方から間違っている気がしますが、どのように改善すればいいのかわからなく、3日程悩んでいます。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
Ruby on Rails 6.0.0

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。