Cookies
Cookies

Reputation: 135

JS templates render but nothing happens in 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

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?

Upvotes: 2

Views: 498

Answers (1)

klochner
klochner

Reputation: 8125

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>

Upvotes: 1

Related Questions