你好我想用Ruby on Rails(5)写一个小博客,通过表格提交类别和医生。
但是当我提交医生时,它经常出现两次,我不明白为什么。
这是我项目的源代码。
doctors_controller.rb
档案:
def create
@category = Category.find(params[:category_id])
@doctor = @category.doctors.create(doctor_params)
redirect_to category_path(@category)
end
def destroy
@category = Category.find(params[:category_id])
@doctor = @category.doctors.find(params[:id])
@doctor.destroy
redirect_to category_path(@category)
end
private
def doctor_params
params.require(:doctor).permit(:name, :address, :phno)
end
show.html.erb
档案:
<p id="notice"><%= notice %></p>
<center><h1>
<%= @category.name %>
</h1>
</center>
<p1><%= render @category.doctors %></p1>
<p2><%= render 'doctors/form' %></p2>
<%= link_to 'Edit', edit_category_path(@category) %> |
<%= link_to 'Back', categories_path %>
_doctor.html.erb
档案:
<table style="width:100%">
<tr>
<th>Name</th>
<th>Address</th>
<th>phone number</th>
</tr>
<% @category.doctors.each do |doctor| %>
<tr>
<th><%= doctor.name %></th>
<th><%=doctor.address%></th>
<th><%=doctor.phno%></th>
<th><%= link_to 'Delete doctor', [doctor.category, doctor],method: :delete,data: { confirm: 'Are you sure?' } %>
</th>
</tr
<% end %>
</table>
答案 0 :(得分:0)
@ Prabal-Kar,正如伊戈尔所建议的那样,你确定自己是在点击&#39;按钮只有一次?为了防止用户这样做,您可以在点击后disable the button,并确保您的医生型号上有unique constraints。