2013-03-17 3 views
3

레일에 루비를 처음 사용했습니다. 나는 ajax가 link_to와 remote = true를 사용하는 레일즈에서 어떻게 작동 하는지를 이해하기위한 튜토리얼을 따라 가고있다. 나는 다음과 같은 코드를 가지고있다.ajax, link_to ruby ​​on remote 레일

// in view/home/sample.html.erb 

<head> <%= javascript_include_tag "prototype.js" %> </head> 
<body> 
<div id="time_div"> 
<%= link_to("click here", :update=>'time_div' ,:url=>{:action => :say_when} , :remote => true) %> 
</div> 
</body> 

// in /controllers/home_controller.rb 

def sample 
end 

def say_when 
    render_text "<p>The time is <b>" + DateTime.now.to_s + "</b></p>" 
end 

저는 assets/javascripts 폴더에 prototype.js를 넣었습니다. 그러나 나는 브라우저에서 시간을 얻지 못하고있다. plz someone help

답변

1

div 콘텐츠를 Ajax 요청의 결과로 업데이트하는 자바 스크립트가 누락되었습니다. 당신은 다음과 같이 시도 할 수 있습니다 :

당신은 컨트롤러에서 방법 say_when을 제거 할 수 있습니다 다음과 같은 내용

$("time_div").update('<%= escape_javascript("<p>The time is <b>#{DateTime.now.to_s}</b></p>")%>'); 

say_when.js.erb를 생성, 더 이상 필요없는 코드가없는, 레일은 say_when.js.erb 파일을 직접 호출합니다.

물건을 설명 할 수있는이 #205 Unobtrusive Javascript Railscast을보세요.

+0

감사합니다. 문제는 link_to 구문에도있었습니다. 나는 그것을 <% = link_to ("여기를 클릭하십시오", : action => : say_when, : remote => true) %>로 변경했습니다. 이제 작동! – nbbk

관련 문제