【问题标题】:Save Using Ajax And Include Notice - Rails保存使用 Ajax 并包含注意事项 - Rails
【发布时间】:2012-12-15 15:46:28
【问题描述】:

我在 _form.html.erb 中添加了 :remote => true ,现在想知道如何获取它,以便当用户保存页面上的内容时,它会保存并发出通知。

在我的documents_controller.rb中,我有;

def create
  @document = current_user.documents.build(params[:document])

  if @document.save
    redirect_to @document.edit, notice: 'Saved'
  else
    render action: "new"
  end
end

def update
  @document = current_user.documents.find_by_url_id(params[:id])

  if @document.update_attributes(params[:document])
    redirect_to @document, notice: 'Saved'
  else
    render action: "edit"
  end
end

【问题讨论】:

    标签: javascript ruby-on-rails ruby ajax


    【解决方案1】:

    您需要编写适当的 JavaScript 以使用以下 jQuery 事件:

    ajax:beforeSend
    ajax:complete
    ajax:success
    ajax:error
    

    例如

    $(document).ready(
        $(".ajax_rails_link").bind("ajax:success", function(evt, data, status, xhr){
            console.log(data);
    
            // Display your notice logic goes here
        })
    });
    

    更多信息可以在这里找到:http://guides.rubyonrails.org/ajax_on_rails.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      • 2018-11-15
      相关资源
      最近更新 更多