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

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

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

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

Ruby

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

Q&A

解決済

1回答

650閲覧

外部キーが保存できない!!!!!!!!!!!

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails 5

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

Ruby

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

0グッド

0クリップ

投稿2020/01/21 06:39

編集2020/01/21 08:36

ruby初心者です。
もう一週間ほど悩んでいて、病んできたので助けて欲しいです。
coordinationのnewビューに表示させているouterテーブルにある画像をcheck_boxで送信し、coordinationテーブルの外部キーouter_idに保存したいです。

ですがcoordinationの主キーは保存され、外部キーは保存されません。
なお、エラーは出てきません。
どなたか助けて欲しいです。

coordination/new.html.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 |outer| = form_for @coordination do |q| = image_tag outer.outer, class: 'apapap' if outer.present? = q.text_field :coordination, class: "apapap" = q.check_box :OuterId, as: :boolean, class: "apapap" = q.submit "送信", class: "apapap"

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 @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_id).merge(user_id: current_user.id) end end

outerのコントローラー

class OutersController < ApplicationController def index @outers = Outer.all end def new @outer = Outer.new @outer.coordination.build end def create Outer.create(outer_params) end def edit # binding.pry @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) end end

coordinationのmigrationファイル

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のmigrationファイル

class CreateOuters < ActiveRecord::Migration[5.0] def change create_table :outers do |t| t.string :outer t.string :image t.timestamps end end end

後付けした外部キーたち

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

coordinationのモデル

class Coordination < ApplicationRecord belongs_to :OuterId, 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 validates :coordination, presence: true end

outerのモデル

class Outer < ApplicationRecord belongs_to :user, optional: true has_many :coordinations mount_uploader :outer, ImageUploader accepts_nested_attributes_for :coordinations end

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

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

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

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

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

y_waiwai

2020/01/21 06:53

コードは画像ではなくてテキストで提示してください 質問を編集し、<code>ボタンを押し、出てくる’’’の枠の中にコードを貼り付けてください
退会済みユーザー

退会済みユーザー

2020/01/21 07:09

アドバイスありがとうございます。 コードに変えました。
winterboum

2020/01/21 08:47

check box で送信 は どのcontroller の どのaction へですか
退会済みユーザー

退会済みユーザー

2020/01/21 08:57

check_boxはcoordination.controllerのcreate.actionに飛んでいきます。
guest

回答1

0

ベストアンサー

そもそもの間違いを指摘しておきます。
<form>タグは入れ子にできません。
子レコードに相当するフォームはfields_forを使ってやれば良いです。

そのまま送信しても子レコードの作成等はできないので、
親のモデル定義にaccepts_nested_attributes_forを使ってやればうまいことできるはずです。

投稿2020/01/21 08:42

Mugheart

総合スコア2349

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

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

退会済みユーザー

退会済みユーザー

2020/01/21 08:58

試してみます。 ありがとうございます。
退会済みユーザー

退会済みユーザー

2020/01/22 09:03

https://teratail.com/questions/236758 昨日はありがとうございました。 fields_forを使おうとしましたが、うまくいけませんでした。 よろしければ教えていただきたいです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問