最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Rails, jQuery, .js.erb files, JS not executed by the browser - Stack Overflow

programmeradmin24浏览0评论

I'm trying to use jQuery, and everything has been great, until now, when I'm trying to render a partial and append it to a div. Here is how I have it set up:

I have an action that responds to js:

def index
  @objects = Object.find(:all)

  respond_to do |format|
    format.js
  end
end

And a template called index.js.erb with some javascript in it:

alert("hello world");

Firebug returns a "text/javascript" response containing:

alert("hello world");

But the alert window does not appear, nor does any other JavaScript work. I checked out And am more or less following along. I tried several other things, like using .rjs instead of .erb and putting this in my template:

page.alert("hello world"); 

but I get the same exact result, and the browser never executes the JS.

Anyone know why the JavaScript isn't being executed?

I'm running Rails 2.3.4.

I'm trying to use jQuery, and everything has been great, until now, when I'm trying to render a partial and append it to a div. Here is how I have it set up:

I have an action that responds to js:

def index
  @objects = Object.find(:all)

  respond_to do |format|
    format.js
  end
end

And a template called index.js.erb with some javascript in it:

alert("hello world");

Firebug returns a "text/javascript" response containing:

alert("hello world");

But the alert window does not appear, nor does any other JavaScript work. I checked out http://railscasts./episodes/136-jquery And am more or less following along. I tried several other things, like using .rjs instead of .erb and putting this in my template:

page.alert("hello world"); 

but I get the same exact result, and the browser never executes the JS.

Anyone know why the JavaScript isn't being executed?

I'm running Rails 2.3.4.

Share Improve this question edited Jan 7, 2010 at 10:02 Simone Carletti 176k50 gold badges367 silver badges369 bronze badges asked Jan 6, 2010 at 21:24 a10sa10s 9701 gold badge9 silver badges15 bronze badges 1
  • 1 You don't appear to be using anything jQuery specific, but just as a note, Rails ships with Prototype, not jQuery. Check out jRails if you want to switch. – Topher Fangio Commented Jan 6, 2010 at 21:29
Add a ment  | 

1 Answer 1

Reset to default 12

You have to call it from your view or it will never be executed.

an example controller:

def index
  @objects = Object.find(:all)

  respond_to do |format|
    format.js{
      render :text => "alert('hello')"
    }
  end
end

and an index.html.erb with:

<script type="text/javascript">
  $(function(){
    $.ajax({ url: '/controller', type: 'get', dataType:'script' });
  });
</script>

replace '/controller' with the actual url that executes that controller's index action, by default for PostsController it will be '/posts' and so on...

if you like rjs you delete the {} and everithing in it in the controller and create an index.js.erb or index.rjs with:

page.alert("hello world")

or:

page << "hello world"

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论