开发者

What's a good plugin or guide I can use to create javascript widgets from a Rails App?

开发者 https://www.devze.com 2023-01-01 04:48 出处:网络
I\'ve been looking around for a while now and the best guide I\'ve seen so far is Dr Nic\'s DIY widgets how to (here).

I've been looking around for a while now and the best guide I've seen so far is Dr Nic's DIY widgets how to (here).

I haven't been able to make something like this work:

Assuming this is my widget code:

<script src="http://mysite/nomnoms.js"> </script>

And my nomnoms controller looks like (ass开发者_运维技巧ume that the list partial exists and simply lists down a link to the show page of each nomnom in the @nomnoms variable):

class NomnomsController < ApplicationController

def index
  @nomnoms = Nomnom.find(:all)
  @content = render_to_string(:partial => 'list')
end

end

And in the index.js of my nomnoms_controller I have:

page << "document.write('<div>'"
page << "document.write('#{@content.to_json}')"
page << "</div>"

The above setup doesn't render anything :(. But when I change the second line of index.js to:

page << "document.write('nomnoms should be here')

...the widget renders the text. Any help or even a point in the right direction would be greatly appreciated. Thanks in advance.


Finally figured it out. I was able to make the divs render after doing two things:

First, I instantiated the string in a variable inside index.js like so:

text = @content.to_json.chop.slice(1..-1)

And used that as the replacement in the actual javascript.

page << %Q{document.getElementById("nomnom-list").innerHTML="#{text}"}

Second, as you can see in the first code snippet, I removed both leading and trailing unescaped double quotes from the string.


  1. Does index.js render the divs?
  2. If you enter http://mysite/nomnoms.js in your browser, does it render the script?

George

0

精彩评论

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