JS 模板渲染但在 Rails 3.0.7 中没有任何反应
show.html.erb:
...
<%= link_to "Start Retrieval", {:action => "retrieve"}, :remote => true %>
<div id="notices">
<%= render :partial => 'notice' %>
</div>
....
_notice.html.erb:
<div>Notice</div>
retrieve.js.erb:
#alert('test') //=> does not work
$('#notices').append("<% escape_javascript render :partial => 'notice' %>");
controller:
def retrieve
respond_to do |f|
#f.js {render :js => "alert(typeof jQuery != 'undefined')"} #=> true
f.js {render :js => "retrieve"} #=> nothing happens
end
end
输出:
Started GET "/model/1/retrieve" for 127.0.0.1 at 2011-06-24 21:09:44 +0100
Processing by ModelController#retrieve as JS
Parameters: {"id"=>"1"}
Completed 200 OK in 50ms (Views: 49.2ms | ActiveRecord: 0.0ms)
如您所见,检索动词不直接涉及模型,并且我没有将 @model var 传递给模板。我的印象是这只是速记,不应该影响事情。我这件事有错吗?
show.html.erb:
...
<%= link_to "Start Retrieval", {:action => "retrieve"}, :remote => true %>
<div id="notices">
<%= render :partial => 'notice' %>
</div>
....
_notice.html.erb:
<div>Notice</div>
retrieve.js.erb:
#alert('test') //=> does not work
$('#notices').append("<% escape_javascript render :partial => 'notice' %>");
controller:
def retrieve
respond_to do |f|
#f.js {render :js => "alert(typeof jQuery != 'undefined')"} #=> true
f.js {render :js => "retrieve"} #=> nothing happens
end
end
Output:
Started GET "/model/1/retrieve" for 127.0.0.1 at 2011-06-24 21:09:44 +0100
Processing by ModelController#retrieve as JS
Parameters: {"id"=>"1"}
Completed 200 OK in 50ms (Views: 49.2ms | ActiveRecord: 0.0ms)
As you can see the retrieve verb doesn't involve the model directly and I'm not passing a @model var to the templates. I'm under the impression this is just shorthand and should not affect things. Am I wrong about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够执行以下操作:
这将渲染与操作同名的 js.erb 文件 (retrieve.js.erb)。
你正在做的是将字符串“retrieve”渲染为纯javascript,相当于在js文件中只包含“retrieve”,如下所示:
You should be able to just do:
Which will render the js.erb file with the same name as the action (retrieve.js.erb).
What you're doing is rendering the string "retrieve" as pure javascript, equivalent to having just "retrieve" in the js file, like this: