问题
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