ruby初心者です。
fields_forの親子関係が逆の時はどうすればいいですか。
coordinationの外部キーにouterがあります。
coordinationは
belongs_to :outer, optional: true
outerは
has_many :coordinations
accepts_nested_attributes_for :coordinations
となっていますがcoordinationに保存しますが、親子関係はそれの逆となっております。
解決したいこと
coordination.controllerの@coordination.outer.buildの部分が
undefined method `build' for nil:NilClass
を起こします。
.newにしても同様にエラーが起きます。
.buildを外したら、エラー無く操作できますが外部キーにcheck_
boxで選択したouterの画像を保存できません。
おそらく正則化などが問題としてあるのかなと考えていますが、どなたかご教授お願いします。
#coordination.controller
class CoordinationsController < ApplicationController before_action :coordination_params ,only:[:create] def index @coordination = Coordination.new end def new @coordinations = Coordination.all @outers = Outer.all @inners = Inner.all @bottoms = Bottom.all @shoes = Shoe.all @hats = Hat.all @accessories = Accessory.all @coordination = Coordination.new @coordination.outer.build @outer = Outer.new @inner = Inner.new @bottom = Bottom.new @shoe = Shoe.new @hat = Hat.new @accessory = Accessory.new end def create @coordination = Coordination.create!(coordination_params) end def show @coordinaitions = Coordination.all end def edit @coordinations = Coordination.find(params[id]) @outers = Outer.find(params[id]) @inners = Inner.find(params[id]) @bottoms = Bottom.find(params[id]) @shoes = Shoe.find(params[id]) @hats = Hat.find(params[id]) @accessories = Accessory.find(params[id]) end def update @coordinations = Coordination.find(params[id]) @inners = Inner.find(params[id]) @bottoms = Bottom.find(params[id]) @shoes = Shoe.find(params[id]) @hats = Hat.find(params[id]) @accessories = Accessory.find(params[id]) end private def coordination_params params.require(:coordination).permit(:season, :coordination, :inner_id, :bottom_id, :shoes_id, :hat_id, :accessory_id, outer_attributes: [:outer]).merge(user_id: current_user.id) end end
#outer.controller
class OutersController < ApplicationController def index @outers = Outer.all end def new @outer = Outer.new end def create Outer.create(outer_params) end def edit @outers = Outer.find(params[id]) end def update @outers = Outer.find(params[id]) end def destroy outer = Outer.find(params[:id]) outer.destroy end private def outer_params params.require(:outer).permit(:outer, coordination_attributes: [:outer_id]) end end
#coordination.model
class Coordination < ApplicationRecord belongs_to :outer, optional: true belongs_to :inner, optional: true belongs_to :bottom, optional: true belongs_to :shoe, optional: true belongs_to :hat, optional: true belongs_to :accessory, optional: true belongs_to :user, optional: true has_many :accessory_coordinations mount_uploader :outer_id, ImageUploader end
#outer.model
class Outer < ApplicationRecord belongs_to :user, optional: true has_many :coordinations mount_uploader :outer, ImageUploader accepts_nested_attributes_for :coordinations end
#coordination/new.haml
.lists .cloth 服一覧 = form_for @outer do |f| = f.label :"アウター", class: "outer-btn" = f.file_field :outer, class: "file-btn" = f.submit "追加", class: "add-btn" %br .bbbbb V - @outers.each do |abc| = f.fields_for :coordination do |q| = image_tag (abc.outer), class: 'apapap' -# = q.label :outer, class: "apapap" = q.check_box :outer_id, class: "apapap" = q.submit "送信", class: "apapap"
#coordination.migrate
class CreateCoordinations < ActiveRecord::Migration[5.0] def change create_table :coordinations do |t| t.string :season t.string :coordination t.timestamps end end end
#outer.migrate
class CreateOuters < ActiveRecord::Migration[5.0] def change create_table :outers do |t| t.string :outer t.string :image t.timestamps end end end
#coordination.addreference.migrate
class CreateChageCoordinations < ActiveRecord::Migration[5.0] def change create_table :chage_coordinations do |t| add_reference :coordinations, :outer, foreign_key: true add_reference :coordinations, :inner, foreign_key: true add_reference :coordinations, :bottom, foreign_key: true add_reference :coordinations, :shoes, foreign_key: true add_reference :coordinations, :hat, foreign_key: true add_reference :coordinations, :accessory, foreign_key: true t.timestamps t.timestamps end end end
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。