简体   繁体   中英

call jquery function from rails controller based on condition

I would like to set the inner html of an element based on some condition. ie

    if cond
      js('$("#my-id").html(a)')
    else
      js('$("#my-id").html(b)')
    end

I found page.replace_html, but it's deprecated for rails 4.

Assuming you have remote request coming in to the controller (with remote: true), use:

respond_to do |format|
  format.js do 
    if cond
      render text: '$("#my-id").html(a)'
    else
      render text: '$("#my-id").html(b)'
    end
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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