在您尝试提交新指南并在表单上看到一条错误消息后(由于指南正确未能通过验证)...@specialties 列表未正确重新加载(即它只是说是/否而不是在您提交错误之前可以看到的正确列表)。我无法弄清楚这里的哪一部分是错的......
视图_form.html.erb
<%= simple_form_for(@guideline, html: {class: "form-horizontal"}) do |f| %>
<% if @guideline.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@guideline.errors.count, "error") %> prohibited this guideline from being saved:</h2>
<ul>
<% @guideline.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :title, label: 'Title (e.g. Asthma for under 12 months)' %>
<%= f.input :specialty, as: :select, collection: @specialties %>
<%= f.input :hospital %>
<%= f.input :content, as: :string, label: 'Link' %>
<div class="form-actions">
<%= f.button :submit, :class => "btn btn-info btn-large" %>
</div>
<% end %>
指南控制器.rb
def new
@guideline = Guideline.new
@specialties = Guideline.order(:specialty).uniq.pluck(:specialty)
respond_to do |format|
format.html # new.html.erb
format.json { render json: @guideline }
end
end
def create
@guideline = current_user.guidelines.new(params[:guideline])
respond_to do |format|
if @guideline.save
format.html { redirect_to @guideline, notice: 'Guideline was successfully created.' }
format.json { render json: @guideline, status: :created, location: @guideline }
else
@specialties = Guideline.order(:specialty).uniq.pluck(:specialty)
format.html { render action: "new" }
format.json { render json: @guideline.errors, status: :unprocessable_entity }
end
end
结尾