質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

1097閲覧

ruby初心者を助けてください。fields_forの1対多を逆にした時の対処法

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

1グッド

2クリップ

投稿2020/01/22 06:25

編集2020/01/22 09:47

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
DrqYuto👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

配列の中身がおかしくなっていることが原因でした

投稿2020/05/24 12:21

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

同じです

@coordination = Coordination.new @coordination.outer.build @outer = Outer.new #を @outer = Outer.new @coordination = outer.coordinations.new

投稿2020/01/22 08:08

winterboum

総合スコア23284

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/01/22 08:20

ご回答ありがとうございます。 同じなんですね。 勉強になりました。 ただご指摘通り変更しましたが、 @coordination = outer.coordinations.new nameエラーが起き@outerにしろと言われたので @coordination = @outer.coordinations.new にしたところ、まだ外部キーには保存されませんでした。
winterboum

2020/01/22 08:27

失礼チョンボ @coordination = @outer.coordinations.build です
退会済みユーザー

退会済みユーザー

2020/01/22 08:41

ご返信ありがとうございます。 @coordination = @outer.coordinations.build にしましたが、まだ外部キーには登録されません。 なんども申し訳ありませんが、お願いします。
winterboum

2020/01/22 08:46

ああ、formが2つありますね。だからです。一度には一つのformしか送られません。 coordination の方は outerのformの中に入れて、f.fields_for をつかってください。
退会済みユーザー

退会済みユーザー

2020/01/22 08:56

そうするとouterテーブルに保存されます。 = 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"
winterboum

2020/01/22 09:30

コメント欄だとインデントが分からないので、修正したbiewとして質問に追加してください
退会済みユーザー

退会済みユーザー

2020/01/22 09:48

viewを修正しました お願いいたします
winterboum

2020/01/22 09:57

= q.submit "送信", class: "apapap"  は削除してください で - @outers.each do |abc| は何のために? 新たに準備した @coordination のデータを入れるだけなのでは? 全ての Cordination を触る必要があるとは思えないのですが。 このnewの画面で何をしたいの?
退会済みユーザー

退会済みユーザー

2020/01/22 11:09

画像一覧をnew.hamlに表示させcheck_box選んで送信したものをcoordinationの外部キーouter_idに入れたいです each文は一枚一枚表示させ、それらにcheck_boxつけるためにやっています。 = q.submit "送信", class: "apapap"はチェックしたものをcoordinationの外部キーに入るためにやっています。
winterboum

2020/01/22 11:17

どのcoordinationに入れるのですか? 新たに作る?
winterboum

2020/01/22 11:20

outerの方のformは何のために?
winterboum

2020/01/22 11:24

判らん! coordination.model には belongs_to が7つあるのに create_table :coordinations には 関連付けの ***_id が一つもない。 「coordinationの外部キー」って何?
退会済みユーザー

退会済みユーザー

2020/01/22 11:42

coordinationの外部キーはcoordination.addreference.migrateで後付けしてます。 outerの方のformは何のために? はouterテーブルに画像を新規追加するためにあります。 ここで、outerテーブルに画像を入れていきます。 その後each文のところでouterテーブルの全ての画像出しcheck_boxで選択したもののみをcoordinationテーブルにある外部キーouter_idに追加します。 この際coordination/createアクションを使い入れていこうと思っています。 coordination.model には belongs_to が7つあります。 が、今回は無関係なのでここにコードは張っていません。 実際はinner.modelなどがあります。 紛らわしかったですね。 すみません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問