ruby初心者を助けてください。fields_forの1対多を逆にした時の対処法
- 評価
- クリップ 2
- VIEW 359
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
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
check解決した方法
0
配列の中身がおかしくなっていることが原因でした
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
0
同じです
@coordination = Coordination.new
@coordination.outer.build
@outer = Outer.new
#を
@outer = Outer.new
@coordination = outer.coordinations.new
に
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.32%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる