# rails w/ jquery

**URL:** <https://discuss.rubyonrails.org/t/rails-w-jquery/40027>\
**Category:** rubyonrails-talk\
**Created:** [March 4, 2009, 2:25am UTC](https://discuss.rubyonrails.org/t/rails-w-jquery/40027 "2009-03-04T02:25:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Greg\_Donald1](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/greg_donald1/32/1313_2.png) [@Greg\_Donald1](https://discuss.rubyonrails.org/u/Greg_Donald1)\
**Post date:** [March 4, 2009, 2:25am UTC](https://discuss.rubyonrails.org/t/rails-w-jquery/40027/1 "2009-03-04T02:25:58Z")

</div>

I have this in my application.html.erb

\<script src="/javascripts/prototype.js" type="text/javascript"\>\</script\> \<script src="/javascripts/effects.js" type="text/javascript"\>\</script\> \<script src="/javascripts/dragdrop.js" type="text/javascript"\>\</script\> \<script src="/javascripts/controls.js" type="text/javascript"\>\</script\> \<script src="/javascripts/jquery.js" type="text/javascript"\>\</script\> \<script src="/javascripts/jquery-ui.js" type="text/javascript"\>\</script\> \<script src="/javascripts/application.js" type="text/javascript"\>\</script\>

I have this in my application.js:

var $j = jQuery.noConflict();

This puts the jQuery instance into it's own namespace, leaving the $ object from prototype alone.

And then in an rjs template I can do things like:

page.replace\_html 'pane\_content', :partial =\> 'message/sent\_pane' page \<\< "$j($j.fn.nyroModal.settings.openSelector).nyroModal();"

The page object still processes Javascript, no matter what library you use.

---

<div class="post-metadata">

**Author:** ![Starr\_Horne](https://avatars.discourse-cdn.com/v4/letter/s/b19c9b/32.png) [@Starr\_Horne](https://discuss.rubyonrails.org/u/Starr_Horne)\
**Post date:** [March 4, 2009, 1:55pm UTC](https://discuss.rubyonrails.org/t/rails-w-jquery/40027/2 "2009-03-04T13:55:44Z")

</div>

I have successfully used the jRails plugin. You just drop it in and it changes the built in JS jelpers to use jquery. [EnnerChi.com is for sale | HugeDomains](http://ennerchi.com/projects/jrails)

---

<div class="post-metadata">

**Author:** ![Greg\_Donald1](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/greg_donald1/32/1313_2.png) [@Greg\_Donald1](https://discuss.rubyonrails.org/u/Greg_Donald1)\
**Post date:** [March 4, 2009, 2:08pm UTC](https://discuss.rubyonrails.org/t/rails-w-jquery/40027/3 "2009-03-04T14:08:45Z")

</div>

When I tried it, about 4 or 5 months ago, it did work, but it didn't play nice with prototype. Perhaps it's improved since.

---

<div class="post-metadata">

**Author:** ![Starr\_Horne](https://avatars.discourse-cdn.com/v4/letter/s/b19c9b/32.png) [@Starr\_Horne](https://discuss.rubyonrails.org/u/Starr_Horne)\
**Post date:** [March 4, 2009, 2:34pm UTC](https://discuss.rubyonrails.org/t/rails-w-jquery/40027/4 "2009-03-04T14:34:42Z")

</div>

Ahh. Well I never it with prototype, so I couldn't say.

SH

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 4, 2009, 4:49pm UTC](https://discuss.rubyonrails.org/t/rails-w-jquery/40027/5 "2009-03-04T16:49:22Z")

</div>

Starr Horne wrote:

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 9, 2010, 6:06pm UTC](https://discuss.rubyonrails.org/t/rails-w-jquery/40027/6 "2010-10-09T18:06:53Z")

</div>

Difei Zhao wrote:

> Starr Horne wrote:
> 
> > > \> allowed you to render partials in the .rjs file and execute the \> javascript which updates the page, how can I do that using jquery? \> Thanks in advance!
> > 
> > I have successfully used the jRails plugin. You just drop it in and it changes the built in JS jelpers to use jquery. [http://ennerchi.com/projects/jrails](http://ennerchi.com/projects/jrails)
> > 
> > -- Starr Horne My blog: [http://starrhorne.com](http://starrhorne.com) Check out my Helpdesk RailsKit: [http://railskits.com/helpdesk/](http://railskits.com/helpdesk/)
> 
> Hi, since I'd like write js code by hand with jquery, so "render :partial" in .js.erb file just fills my needs.

for newbies like me who don't want to use jrails in rails 3 and might need it spelled out, rather than:

render :update do |page| &nbsp;&nbsp;page.replace\_html 'preview', :partial =\> 'preview' end

do something like this in your controller:

render :action =\> :preview

and in your preview.js.erb file do something like:

jQuery('#preview').html('\<%= javascript\_escape(render(:partial =\> "preview")) %\>');

of course, you'd also need a partial named \_preview.erb (or \_preview.haml)

good luck!

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 9, 2010, 8:36pm UTC](https://discuss.rubyonrails.org/t/rails-w-jquery/40027/7 "2010-10-09T20:36:17Z")

</div>

> and in your preview.js.erb file do something like:
> 
> jQuery('#preview').html('\<%= javascript\_escape(render(:partial =\> "preview")) %\>');
> 
> of course, you'd also need a partial named \_preview.erb (or \_preview.haml)
> 
> good luck!

oops, should have said escape\_javascript
