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
回答1件
あなたの回答
tips
プレビュー