我链接了以下两个表。 我想将书面数据从表单发送到两个不同的表并保存。 但是我也不能保存子表。
我是一个初学者,尝试了两天。但我不知道。 你能帮我吗?
鸟桌
- id
- 名称
- 品种
- 性别...
bird_condition表
- id
- ave_weight
- body_shape
- ... (bigint“ bird_id”)
版本Rails 6.0.0
#app/models/bird.rb
class Bird < ApplicationRecord
has_many :bird_conditions,dependent: :destroy
accepts_nested_attributes_for :bird_conditions,allow_destroy: true :reject_bird_condition
end
#app/models/bird_condition.rb
belongs_to :bird,optional: true
end
#app/controllers/birds_controller.rb
class BirdsController < ApplicationController
before_action :set_bird,only: [:edit,:update,:destroy]
before_action :authenticate_user!
before_action :correct_user,:destroy]
def index
@birds = current_user.birds.order(created_at: :desc)
end
def new
@bird = Bird.new
@bird.bird_conditions.build
end
def edit
end
def update
@bird.update!(bird_params)
redirect_to birds_path,notice: "upload"
end
def create
@bird = current_user.birds.new(bird_params)
if @bird.save
logger.debug "bird: #{@bird.attributes.inspect}"
redirect_to birds_path,notice: "create"
else
render :new
end
end
def destroy
@bird.destroy
redirect_to birds_path,notice: "delete"
end
private
def set_bird
@bird = current_user.birds.find(params[:id])
end
def correct_user
@bird = current_user.birds.find_by(id: params[:id])
redirect_to birds_path if @bird.nil?
end
def bird_params
params
.require(:bird)
.permit(
:name,:breed_id,:sex,:birthday,:personality,:appeareance,:srarus_flg,:lost_day,:lost_place,:day_of_death,:image,bird_conditions_attributes:[
:id,:ave_weight,:body_shape,:hospital,:medical_history,:medication,:insurance,:estrous_behavior,:estrous_personality,:bird_id
]
)
end
end
#app/views/birds/_form.html.erb
<div class="container">
<%= form_with model: bird,local: true do |f| %>
<table class="table-bordered">
<div class="form-group">
<tr>
<th><%= f.label :name %></th>
<td><%= f.text_field :name %></td>
</tr>
<tr>
<th><%= f.label :breed %></th>
<td><%= f.collection_select :breed_id,Breed.all,:id,:name,include_brank: 'select' %></td>
</tr>
〜〜〜〜〜〜〜〜〜〜〜〜abbreviation〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
<%= f.fields_for :bird_condition do |condition| %>
<%= render partial: 'form_condition',locals:{condition: condition}%>
<% end %>
</div><!-- .form-group -->
</table><br>
<%= f.submit nil,class: 'btn btn-primary'%>
<% end %>
</div><!-- .container -->
#app/views/birds/_form_condition.html.erb
<div class="form-group">
<tr>
<th><%= condition.label :ave_weight %></th>
<td><%= condition.number_field :ave_weight %></td>
</tr>
<tr>
<th><%= condition.label :body_shape %></th>
<td>
<%= condition.radio_button :body_shape,:medium %>
<%= condition.label :body_shape,:普通%>
<%= condition.radio_button :body_shape,:slim%>
<%= condition.label :body_shape,:痩せぎみ%>
<%= condition.radio_button :body_shape,:bony%>
<%= condition.label :body_shape,:痩せ%>
<%= condition.radio_button :body_shape,:chubby%>
<%= condition.label :body_shape,:太りぎみ%>
<%= condition.radio_button :body_shape,:fat%>
<%= condition.label :body_shape,:肥満%>
</td>
</tr>
<tr>
<th><%= condition.label :hospital %></th>
<td><%= condition.text_field :hospital %></td>
</tr>
<tr>
<th><%= condition.label :medical_history %></th>
<td><%= condition.text_field :medical_history %></td>
</tr>
<tr>
<th><%= condition.label :medication %></th>
<td><%= condition.text_field :medication %></td>
</tr>
<tr>
<th><%= condition.label :insurance %></th>
<td><%= condition.text_field :insurance %></td>
</tr>
<tr>
<th><%= condition.label :estrous_behavior %></th>
<td><%= condition.text_field :estrous_behavior %></td>
</tr>
<tr>
<th><%= condition.label :estrous_personality %></th>
<td><%= condition.text_field :estrous_personality %></td>
</tr>
</div><!-- .form-group -->
#error(log)
Unpermitted parameter: :bird_condition