简体   繁体   English

没有页面重新加载的警报Rails AJAX

[英]Alerts without page reload Rails AJAX

I have a model Snippit and I want users to be able to delete a snippit, from a list, and then show an alert saying it was deleted, all using ajax. 我有一个Snippit模型,我希望用户能够从列表中删除一个Snippit,然后显示一条警告,说它已被删除,全部使用ajax。 I have figured out how to do the actual deleting, but not the alert. 我已经弄清楚了如何进行实际的删除,但是没有警报。

Here's the code: 这是代码:

snippits_controller.rb snippits_controller.rb

  def destroy
    @snippit = Snippit.find(params[:id])
    @snippit.destroy
    respond_to do |format|
      format.html { redirect_to snippits_url}
      format.json { head :no_content }
      format.js {render :alert => "Sippit destroyed. "}
    end
  end

destroy.js.erb destroy.js.erb

$('#snippit_<%= @snippit.id %>').remove();

index.html.erb index.html.erb

<% @snippits.each do |snippit| %>
      <span class="panel panel-default" id="snippit_<%= snippit.id %>">
      <%= link_to edit_snippit_path(snippit), :class => "text" do %>
      <div class="panel-body">
        <h3 class="text"><%=snippit.title%></h3>
          <p class="trash"><%= link_to snippit, method: :delete, remote: true do %>
            <i class="fa fa-2x fa-trash-o"></i>
          <% end %></p>
          </div>
          <% end %>
          </span>
    <% end %>

Any and all help is greatly appreciated :) 任何帮助都将不胜感激:)

If you want a JS alert response, then you'd want something like the following instead 如果您想获得JS警报响应,则需要以下类似的内容

format.js {render js: "alert('Sippit destroyed.');"}

The format.js render above means you're rendering a JS response. 上面的format.js呈现意味着您正在呈现JS响应。 Your alert render :alert => "Sippit destroyed. " only works for HTML response because the flash[:alert] is rendered in the HTML page, but since you are rendering a JS response, then you'd either do the JS alert implementation above OR you partially update the HTML page to update the flash message by something like the following 您的警报render :alert => "Sippit destroyed. "仅适用于HTML响应,因为flash[:alert]是在HTML页面中呈现的,但是由于呈现的是JS响应,因此您可以执行JS警报实现上方,或者您通过如下方式部分更新HTML页面以更新Flash消息

destroy.js.erb destroy.js.erb

$('#snippit_<%= @snippit.id %>').remove();
$('#flash_container').html('<%= j render partial: "flash_container" %>');

UPDATE (Added working controller code for method 2: using destroy.js.erb above) 更新(为方法2添加了工作控制器代码:使用上面的destroy.js.erb)

def destroy
  @snippit = Snippit.find(params[:id])
  @snippit.destroy
  respond_to do |format|
    format.html { redirect_to snippits_url}
    format.json { head :no_content }
    format.js { flash.now[:alert] = @snippit.destroyed? ? 'Sippit destroyed.' : @snippit.errors.full_messages }
  end
end

I added a failure-handler code above for format.js . 我在上面为format.js添加了故障处理程序代码。 It will set the flash alert message into either 'Sippit destroyed' if @snippit was successfully destroyed, OR into 'Some failure to destroy error' if @snippit was not destroyed. 这将设置闪光警报消息成是“Sippit摧毁”如果@snippit被成功摧毁,或进入“有些未能摧毁错误”,如果@snippit都没有被破坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM