JS 模板渲染但在 Rails 3.0.7 中没有任何反应

发布于 2024-11-17 05:37:31 字数 1062 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻想少年梦 2024-11-24 05:37:31

您应该能够执行以下操作:

respond_to do |f|
  f.js {}
end

这将渲染与操作同名的 js.erb 文件 (retrieve.js.erb)。

你正在做的是将字符串“retrieve”渲染为纯javascript,相当于在js文件中只包含“retrieve”,如下所示:

<javascript>
retrieve
</javascript>

You should be able to just do:

respond_to do |f|
  f.js {}
end

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:

<javascript>
retrieve
</javascript>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文