我正在使用Ruby on Rails编写Web应用程序。 在控制器中:
def create
@category = Category.new(category_params)
respond_to do |format|
if @category.save
format.html { redirect_to @category, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: @category }
format.js { render js: 'window.top.location.href = "/categories";' }
else
format.html { render :new }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
在 new.html.erb 文件中:
<h1>New category</h1>
<%= render 'form' %>
<%= link_to 'Back', categories_path %>
在_form.html.erb文件中:
<%= form_for(@category) do |f| %>
<% if @category.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@category.errors.count, "error") %> prohibited this category from being saved:</h2>
<ul>
<% @category.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :content %><br>
<%= f.text_field :content, placeholder: "content" %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description, placeholder: "description" %>
</div>
<div class="actions">
<a href="#" onclick="window.top.location.reload(true)"><%= f.submit %>
</a>
</div>
<% end %>
我希望提交按钮能够正常工作,并且在页面刷新之前保存新创建的类别。在代码中,页面在保存之前已经被刷新。 请帮忙。谢谢。
onclick="window.top.location.reload(true)"
。 - madalinivascu