Using js.rjs to render inline content

懵懂的女人 提交于 2020-01-06 19:34:12

问题


I am trying to improve my knowledge of Rails. I was trying to use js.rjs in a simple code. Here's the view:

<div style="background: #000080; font-weight: bold; margin-left: 30px"><p>This should     Remain Same</p></div>
    <div id="noid">


    <%=link_to 'Click',:action=>:say_when , :remote => true %>  

      and I will look it up.
    </div>

And here's the controller:

class DemoController < ApplicationController

    def say_when

      respond_to do |format|
        format.js
      end    

    end

end

And I have views/Say_when.js.rjs as follows:

page.replace_html('noid', render(:text=>"OK"))

But when the "click" link is clicked, nothing happens. What am I doing wrong?


回答1:


Got it. I changed the file extension to js.erb

then in the js.erb file, I added the following lines:

var d=new Date();
var curr_hour=d.getHours();
var curr_min=d.getMinutes();
$('div#noid').html("<h1>The Current time is</h1>"+curr_hour+":"+curr_min)


来源:https://stackoverflow.com/questions/5306704/using-js-rjs-to-render-inline-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!