开发者

JS templates render but nothing happens in Rails 3.0.7

开发者 https://www.devze.com 2023-03-15 02:13 出处:网络
show.html.erb: ... <%= link_to \"Start Retrieval\", {:action => \"retrieve\"}, :remote => true %>

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 ca开发者_Python百科n 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?


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号