前提
Tender
とTenderLocation
モデルがあり、双方はTender belongs_to TenderLocation
とTenderLocation has_many Tenders
で関連づけています。感覚的にhas_many
とbelongs_to
が逆と思われると思いますが、これで正しいです。各々のmodelとtableは以下の通りです。(本質問に関係ない情報は消去してます)。
間接的なこととして、Seller
モデルも存在し、Seller has_many Tenders / Tender Locations
となっています。
ruby
1###Tender Model 2# 3# Table 4# 5# id :bigint(8) not null, primary key 6# seller_id :bigint(8) not null 7# tender_location_id :bigint(8) not null 8# starts_at :datetime not null 9# ends_at :datetime not null 10# status :string default("draft"), not null 11# created_at :datetime not null 12# updated_at :datetime not null 13# 14 15class Tender < ApplicationRecord 16 belongs_to :seller 17 belongs_to :tender_location 18 19 STATUS_VALUES = %w(draft ready) 20 21 ##色々省略## 22 23end 24 25 26###TenderLocation Model 27# == Schema Information 28# 29# Table name: tender_locations 30# 31# id :bigint(8) not null, primary key 32# seller_id :bigint(8) not null 33# address :string not null 34# address_country_part :string default("Japan"), not null 35# address_prefecture_part :string not null 36# address_city_part :string not null 37# address_rest_part :string not null 38# created_at :datetime not null 39# updated_at :datetime not null 40# 41 42class TenderLocation < ApplicationRecord 43 has_many :tenders 44 45end 46
今起きている問題
Tender
モデルを新規作成する際に、Tender Location
をユーザーに選択(新規作成てはなく、既存の TenderLocationから選んでもらう)。
で、以下のようにcontrollerを記載しています。
ruby
1def new 2 @tender = Tender.new 3 time0 = Time.current.beginning_of_day 4 @tender.starts_at = time0.advance(days: 1, hours: 9) 5 @tender.ends_at = time0.advance(days: 3, hours: 12) 6 @tender_location_candidates = current_seller.tender_locations 7 end 8 9 def create 10 @tender = Tender.new(tender_params) 11 binding.pry 12 @tender.seller = current_seller 13 tender_location = TenderLocation.find_by(id: tender_location_id) 14 @tender.tender_location = tender_location 15 if @tender.save 16 flash.notice = "入札会を設定しました。掲載商品を追加してください。" 17 redirect_to sellers_tender_path(@tender) 18 else 19 render "new" 20 end 21 end 22 23 private def tender_params 24 params[:tender].permit( 25 :seller_id, :tender_location_id, 26 :starts_at_date_part, :starts_at_time_part, 27 :ends_at_date_part, :ends_at_time_part, 28 :status 29 ) 30 end 31
フォームはこちら
erb
1 2<%= form_for @tender, url: :sellers_tenders do |f| %> 3 4<%= f.label :starts_at_date_part, "入札「開始」日"%> 5 6 間省略 7 8<%= f.select :status, Tender.status_options, {}, class: 'form-control' %> 9<%= f.label :tender_location_id, "開催場所" %> 10<%= select_tag :tender_location_id, options_from_collection_for_select(@tender_location_candidates.all, :id, :address), class: 'form-control' %> 11 12<%= f.submit "入札を開始する", class: 'btn btn-success' %> 13
その結果のコンソール/ログが以下の通りです。おかしいと思うのが、
0. デバックにてtender_params
を入れると、tender_location_idが入っていないこと
0. コンソールのログを見ると、"tender_location_id"=>"5"とあるものの、"tender"=>{---省略---}の外にきていること
ruby
1###コンソールのログ 2Parameters: {"utf8"=>"✓", "authenticity_token"=>"-省略-", 3"tender"=>{"starts_at_date_part"=>"2019-01-26", "starts_at_time_part"=>"09:00", 4 "ends_at_date_part"=>"2019-01-26", "ends_at_time_part"=>"12:00", 5"status"=>"draft"}, "tender_location_id"=>"5", "commit"=>"入札を開始する"} 6 7###binding.pryにて 8pry(#<Sellers::TendersController>)> tender_params 9=> <ActionController::Parameters {"starts_at_date_part"=>"2019-01-26", "starts_at_time_part"=>"09:00", "ends_at_date_part"=>"2019-01-26", "ends_at_time_part"=>"12:00", "status"=>"draft"} permitted: true>
解決策わからず、ご教示いただけますと助かります。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/23 11:22