load partial with jquery and rails

后端 未结 5 1362
深忆病人
深忆病人 2021-01-04 23:54

Im curious about what the best way to load a rails partial with jquery. So far I have tried a solution not working:

$(\'#imagecontent\').load(\'/newsletters/         


        
5条回答
  •  醉梦人生
    2021-01-05 00:29

    With rails 3 you could do...

    controller

    @content = '/some/partial'
    

    view - either haml.erb(html.erb) or js.erb

    $('#myDiv').html("<%= escape_javascript render @content %>");
    

    You could try this if rails 2 (only prototype available unless noConflict added)...

    In the controller

    @content = render :partial=>"/users/cool_partial"
    

    Call your js

    myFunction(#{@content.to_json})
    

    Your function...

    myFunction = function(content){
    
        $("myDiv").html(content);
    
    }
    

    I do agree with neutrino that this could be better handled via an action.

    def get_partial
      render 'some/partial'
    end
    

    Hope this helps

提交回复
热议问题