#45
Jun 15, 2007

RJS Tips

This episode is packed with little RJS goodies. Learn the different ways to access an element, how to add "if" conditions and how to apply an effect to multiple elements.
Tags: ajax
Download (21.5 MB, 7:30)
alternative download for iPod & Apple TV (10.4 MB, 7:30)
# do_magic.rjs
page[:reviews].toggle

page[:review_name].value = "this is cool"

page << "if ($('review_name').value == 'foo') {"
page.alert('hi foo!')
page << "}"

page.select("#reviews strong").each do |element|
  element.visual_effect :highlight
end
<%= link_to_function "Show me the Magic!" do |page|
  #...
end %>

RSS Feed for Episode Comments 11 comments

1. chineseGuy Jun 15, 2007 at 01:12

Oh,very nice and cool!~


2. incoroneta Jun 15, 2007 at 01:17

let's see


3. Bruno Jun 15, 2007 at 01:25

Thanks for all your screencasts, Ryan. I always learn something even if English is not my primary language. Speak more slowly perhaps ? (joke).


4. maze Jun 15, 2007 at 01:45

link_to_funtion is nice! I hope you soon will produce some screencasts that deal with helper methods.


5. Top Ten Tutorials Jun 15, 2007 at 15:17

Thanks Ryan for another great Railscast.
Please keep them coming.


6. carmelyne Jun 16, 2007 at 20:43

Busy as a bee, Ryan? Love the snippets.


7. Ryan Bates Jun 16, 2007 at 22:35

@carmelyne, glad you like it. :)

I actually had the feature done a few days ago, I just didn't upload it until last night. It was still a chore retyping all the code.

I should probably keep the code in a subversion repository as well and add a "tag" for each episode. Hmmm.


8. foozilla Jun 17, 2007 at 10:27

Another great cast!
Thanks for reading the feedback.

dynamic update with rjs will be a great cast if you can. Thanks again Ryan.


9. Phillip Jul 10, 2007 at 01:57

Hey Ryan great ca$t, I was wondering if this was the bes appraoach to update a div with something like this:
<% @brands.each do |brand| %>
   <%= link_to_remote(image_tag(brand.filename),:url => {:action => 'show_description', :id => brand}, :class => "description") %>
<%- end %>

pretty much passing the :id of brand to a partial how would one go about that?


10. Ryan Bates Jul 10, 2007 at 07:39

@Phillip, that looks like it will work. If it doesn't you may want to post about this on railsforum.com.


11. Shoaib Jul 16, 2007 at 03:20

lovin' the casts thanks Ryan. Just a small comment about manipulating DOMs using element proxies. There is one advantage of calling the DOM methods directly over page is that many DOM objects can be affect in one call like:
<code>
page.toggle :image_div, :text_div
</code>
vs.
<code>
page[:image_div].toggle
page[:text_div].toggle
</code>


12. Jonathan Spooner Sep 04, 2007 at 21:42

Thanks! Your rock!


13. morb Oct 09, 2007 at 03:28

How does the controller look like?
How does it know to render the rjs?


14. Ryan Bates Oct 09, 2007 at 10:07

@morb, it isn't necessary to define the action in the controller. Rails will automatically look for a template with the name of the action (do_magic.rjs) and just render that. Nothing significant is in the controller.


15. dombesz Oct 26, 2009 at 09:36

Hi,

can somebody help me to test this:
page[:review_name].value = "this is cool"
 with assert_select_rjs, or any other method will be appreciated.

Thanks


16. Genyulya Feb 27, 2010 at 13:46

i think so be good


17. Lina Mar 09, 2010 at 12:49

how do its works?


18. Elyuna Mar 10, 2010 at 12:13

its so far so be good!


19. timberlandbootsuk Aug 02, 2010 at 02:06

we provide our buyers with an efficient and manageable procurement process covering every phase of the international supply chain and

streamlining trade channels. Also welcome wholesaling, feedback now!


20. timberlandbootsuk Aug 02, 2010 at 02:06

we provide our buyers with an efficient and manageable procurement process covering every phase of the international supply chain and

streamlining trade channels. Also welcome wholesaling, feedback now!


21. free directory list Aug 11, 2010 at 22:19

Its amazing interesting bangs!


22. p90 workout Aug 12, 2010 at 09:15

I want to say that I dont think Ive read anything so true in a long time. Youve got a lot of great ideas, a great deal of perspective. I think that you definitely have something important to say and Im gonna back it 110%, man. Good job keeping this subject alive and interesting!


23. Nike Sb Dunks Aug 14, 2010 at 02:18

I always learn something even if English is not my primary language. treamlining trade channels. Also welcome wholesaling, feedback now!


24. supra tk society Aug 18, 2010 at 18:50

good job,good article


25. Discount Timberland uk Aug 24, 2010 at 00:27

good ,tips,thank you


26. louis vuitton shoes Aug 26, 2010 at 21:02

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


27. snow boots Aug 31, 2010 at 01:33

I always learn something even if English is not my primary language.


28. GHD Hair Straighteners Sep 01, 2010 at 00:24

Good article


29. Cheap Supra Shoes Sep 01, 2010 at 00:26

I like this article very much.


30. GHD Australia Sep 01, 2010 at 00:29

Perfect content


31. levis belts Sep 01, 2010 at 21:10

I feel like I’m often looking for interesting things to read about a variety of subjects, but I manage to include your blog among my reads every day because you have interesting entries that I look forward to. Here’s hoping there’s a lot more great material coming!

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source