0

我在 _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 %>
4

1 回答 1

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

更新.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
于 2013-05-24T10:42:54.890 回答