我正在使用_form.html.erb中的simple_form,我在edit.html.erb中渲染,在编辑表单时,它会编辑字段,但不会显示表单已更新的任何消息。我很惊讶为什么会这样?任何帮助?
<%= simple_form_for(@info, remote: true, :html => {class: 'form-horizontal'}) do |f| %>
<%= f.input :name %>
<%= f.input :phone %>
<%= f.input :email %>
<%= f.button :submit, class: 'btn btn-success' %>
<% end %>
答案 0 :(得分:1)
从我的项目中提取:
<强> _flash_notices.html.erb 强>
<div class="noticesWrapper">
<% flash.each do |name, msg| %>
<div class="alert alert-<%= name == :notice ? "success" : "error" %>">
<a class="close" data-dismiss="alert"><i class="icon-remove"></i></a>
<%= msg %>
</div>
<% end %>
</div>
!请参阅noticesWrapper元素,因为您需要在其中呈现通知。
<强> application_controller.rb 强>
after_filter :add_flash_to_header
def add_flash_to_header
# only run this in case it's an Ajax request.
return unless request.xhr?
# add different flashes to header
response.headers['X-Flash-Error'] = flash[:error] unless flash[:error].blank?
response.headers['X-Flash-Warning'] = flash[:warning] unless flash[:warning].blank?
response.headers['X-Flash-Notice'] = flash[:notice] unless flash[:notice].blank?
response.headers['X-Flash-Message'] = flash[:message] unless flash[:message].blank?
# make sure flash does not appear on the next page
flash.discard
end
<强> update.js.erb 强>
$('.noticesWrapper').html("<%= j(render partial: 'layouts/flash_notices') %>");
<强> your_controller.rb 强>
def update
@project = Project.find(params[:id])
if @project.update_attributes(params[:project])
flash.now[:notice] = "Project updated"
else
flash.now[:error] = "Project not updated"
end
end
end