javascript - Rails ajax 渲染部分

标签 javascript jquery ruby-on-rails ajax redmine

我想实现简单的功能。

当我点击复选框时

<%=check_box_tag :important_only,nil,false,id:'important-notes-only'%>

应更新 View 中的数据

我的 Controller 操作

  def important_notes
    deal =  Deal.find(params[:id])
    @notes = Note.where('is_important = ? and source_id = ?', true, deal.id)
    p @notes
    respond_to do |format|
      format.html { }
      format.js
    end
  end

路线

      get 'important/:id' => 'notes#important_notes', :as => 'important'

important.js.erb

should render my partial on view after click

怎么做?

我的ajax

$(document).ready(function(){
$('#important-notes-only').click(function () {
    if ($('#important-notes-only').is(':checked')) {
        console.log('clicked')
        $.ajax({
            type: "GET",
            url: "<%= important_notes_path(@deal.id) %>",

            success: render partial
            }
        })

    }
});

}); 如何在成功时渲染部分内容

最佳答案

def important_notes
    deal =  Deal.find(params[:id])
    @notes = Note.where('is_important = ? and source_id = ?', true, deal.id)
    p @notes
    respond_to do |format|
      format.html { }
      format.js{ render 'my_partial', :locals => { :if_any => @if_any } }
    end
end

正在查看

<%= check_box '/important/:id' , remote: true do %>

在 my_partial.js

$('.element').html("<%= escape_javascript render(:partial => 'my_partial', :locals => { :if_any => @if_any}) %>");

关于javascript - Rails ajax 渲染部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33434605/

相关文章:

javascript - 在 Google map API 中向圈子添加文本不起作用

javascript - 尝试访问本地 json 文件时出现 CORS 错误

jquery - Rails 3 使用链接呈现不同的部分

mysql - bundler : failed to load command: spring (/bin/spring)

ruby-on-rails - Rails - 不同的时区来限制每日帖子

javascript - 如何使用 serializeArray() 验证其默认值的表单字段。?

javascript - jquery 查找包含选择器的异常

jquery - Bootstrap 4 - 需要折叠来调整砌体布局

javascript - Angularjs 获取上次或当前更改的模型名称

javascript - 我应该取消绑定(bind)事件监听器以防止内存泄漏吗?