Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

I'm writing my own web app with Ruby on Rails. Should I use JavaScript or Ruby code?

I'm still a beginner writing my own app using Ruby on rails. My idea is to display similar things to the chosen subject. I made pages of products. Now I want to display similar products to it. Should I use JavaScript or Ruby code? What are the pros and cons?

3 Answers

Brandon Barrette
Brandon Barrette
20,485 Points

Remember that JavaScript is run on the client side (in the browser) and ruby is run on the server.

If you just want to display similar products on the page when a product is already loaded, then you should use ruby and render those similar products when the page loads.

If instead you want a clickable button that pops open a modal to tell the user what similar products exist, then you would use JavaScript and an AJAX call to the server to retrieve those products.

Hope that helps.

Thanks, Brandon! That is very helpful. I want to display similar products when a product is already loaded, so I will use ruby then.

Brandon Barrette
Brandon Barrette
20,485 Points

I challenge you to try to do both =)

You know that you can use javascript in your rails app using <%=javascript_include_tag()%> and referring to app/assets/javascript .