更新表单时,Simple_form不显示任何消息

时间:2013-05-24 08:52:43

标签: ruby-on-rails ruby-on-rails-3

我正在使用_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 %>

1 个答案:

答案 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