weixin_33695450 2015-10-30 11:10 采纳率: 0%
浏览 25

Rails Ajax渲染部分

I want to implement simple feature.

When i click on checkbox

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

Data in view should be renewed

My controller action

  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

route

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

and important.js.erb

should render my partial on view after click

How to do it?

my 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
            }
        })

    }
});

}); How to render partial on success

  • 写回答

2条回答 默认 最新

  • YaoRaoLov 2015-10-30 11:26
    关注

    You can use the following:

    #app/views/notes/important_notes.js.erb
    $(".element").html("<%=j render 'partial' %>");
    

    This is assuming your other code is correct.


    Looking at your updates, I would use the following:

    #view
    <%= check_box_tag :important_only, nil, false, id:'important-notes-only', data: { note: @deal.id } %>
    
    #app/assets/javascripts/application.js
    $(document).on("click", "#important-notes-only", function(e){
       if ($(this).is(':checked')) {
          $.get("important/" + $(this).attr("data-note"));
       }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流