下記のようなテーブルの紐付けをしており、一つの登録フォームから入力した値をそれぞのテーブルで保存できるようにしましたが、子テーブルの保存ができません。ご教授いただけますでしょうか。
birdテーブル
- id
- name
- breed
- sex
- ...
bird_conditionテーブル
- id
- ave_weight
- body_shape
- ...
(bigint "bird_id")
環境
Rails 6.0.0
ruby
1#app/models/bird.rb 2class Bird < ApplicationRecord 3 has_many :bird_conditions, dependent: :destroy 4 accepts_nested_attributes_for :bird_conditions, allow_destroy: true :reject_bird_condition 5end
ruby
1#app/models/bird_condition.rb 2 belongs_to :bird, optional: true 3end
rubu
1#app/controllers/birds_controller.rb 2class BirdsController < ApplicationController 3 before_action :set_bird,only: [:edit, :update, :destroy] 4 before_action :authenticate_user! 5 before_action :correct_user,only: [:edit, :destroy] 6 7 def index 8 @birds = current_user.birds.order(created_at: :desc) 9 end 10 11 def new 12 @bird = Bird.new 13 @bird.bird_conditions.build 14 end 15 16 def edit 17 end 18 19 def update 20 @bird.update!(bird_params) 21 redirect_to birds_path,notice: "更新しました" 22 end 23 24 def create 25 @bird = current_user.birds.new(bird_params) 26 if @bird.save 27 logger.debug "bird: #{@bird.attributes.inspect}" 28 redirect_to birds_path, notice: "保存しました" 29 else 30 render :new 31 end 32 end 33 34 def destroy 35 @bird.destroy 36 redirect_to birds_path,notice: "削除しました" 37 end 38 39 private 40 41 def set_bird 42 @bird = current_user.birds.find(params[:id]) 43 end 44 45 def correct_user 46 @bird = current_user.birds.find_by(id: params[:id]) 47 redirect_to birds_path if @bird.nil? 48 end 49 50 def bird_params 51 params 52 .require(:bird) 53 .permit( 54 :name, 55 :breed_id, 56 :sex, 57 :birthday, 58 :personality, 59 :appeareance, 60 :srarus_flg, 61 :lost_day, 62 :lost_place, 63 :day_of_death, 64 :image, 65 bird_conditions_attributes:[ 66 :id, 67 :ave_weight, 68 :body_shape, 69 :hospital, 70 :medical_history, 71 :medication, 72 :insurance, 73 :estrous_behavior, 74 :estrous_personality, 75 :bird_id 76 ] 77 ) 78 end 79end
html.erb
1#app/views/birds/_form.html.erb 2<div class="container"> 3 <%= form_with model: bird, local: true do |f| %> 4 <table class="table-bordered"> 5 <div class="form-group"> 6 <tr> 7 <th><%= f.label :name %></th> 8 <td><%= f.text_field :name %></td> 9 </tr> 10 <tr> 11 <th><%= f.label :breed %></th> 12 <td><%= f.collection_select :breed_id, Breed.all, :id, :name, include_brank: '選択して下さい' %></td> 13 </tr> 14〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜省略〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜 15 <%= f.fields_for :bird_condition do |condition| %> 16 <%= render partial: 'form_condition', locals:{condition: condition}%> 17 <% end %> 18 </div><!-- .form-group --> 19 </table><br> 20 <%= f.submit nil, class: 'btn btn-primary'%> 21 <% end %> 22</div><!-- .container --> 23
html.erb
1#app/views/birds/_form_condition.html.erb 2 <div class="form-group"> 3 <tr> 4 <th><%= condition.label :ave_weight %></th> 5 <td><%= condition.number_field :ave_weight %></td> 6 </tr> 7 <tr> 8 <th><%= condition.label :body_shape %></th> 9 <td> 10 <%= condition.radio_button :body_shape, :medium %> 11 <%= condition.label :body_shape, :普通%> 12 <%= condition.radio_button :body_shape, :slim%> 13 <%= condition.label :body_shape, :痩せぎみ%> 14 <%= condition.radio_button :body_shape, :bony%> 15 <%= condition.label :body_shape, :痩せ%> 16 <%= condition.radio_button :body_shape, :chubby%> 17 <%= condition.label :body_shape, :太りぎみ%> 18 <%= condition.radio_button :body_shape, :fat%> 19 <%= condition.label :body_shape, :肥満%> 20 </td> 21 </tr> 22 <tr> 23 <th><%= condition.label :hospital %></th> 24 <td><%= condition.text_field :hospital %></td> 25 </tr> 26 <tr> 27 <th><%= condition.label :medical_history %></th> 28 <td><%= condition.text_field :medical_history %></td> 29 </tr> 30 <tr> 31 <th><%= condition.label :medication %></th> 32 <td><%= condition.text_field :medication %></td> 33 </tr> 34 <tr> 35 <th><%= condition.label :insurance %></th> 36 <td><%= condition.text_field :insurance %></td> 37 </tr> 38 <tr> 39 <th><%= condition.label :estrous_behavior %></th> 40 <td><%= condition.text_field :estrous_behavior %></td> 41 </tr> 42 <tr> 43 <th><%= condition.label :estrous_personality %></th> 44 <td><%= condition.text_field :estrous_personality %></td> 45 </tr> 46 </div><!-- .form-group -->
#エラーメッセージ(ログ) Unpermitted parameter: :bird_condition
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。