I'm moving my site to Rails currently.
Beforehand I used jQuery load() method to insert a separate site html element. Now moving to Rails, I have to change it. From what I've learnt so far, the best way here is to use a Rails partial for this purpose.
Is this correct?
I would recommend taking a read through the Ruby on Rails Guides which are extremely helpful if you are just getting started in Rails. The specific section that might help is their documentation on using partials.
Yes, you are correct. Partials come in handy when you want to load some HTML elements conditionally, or want to reuse them in different places. A prime example for that is the footer or header of a website. Instead of writing the code in all the required files, simply render the partial wherever required. For further reading/implementation guide, you can refer the section on partials in the RoR guide
Related
I am using ruby on rails 5.0.
It is common practice to use partials to share code fragments for views (e.g., in order to avoid duplication). Partials are files that start with an underscore, e.g., "_fields.html.erb".
After a certain time the directories are full of partials, and it can become quite hard to keep an overview which view calls which partials and which partial calls another partial et cetera.
Question: Is there an "easy" way or a tool or a gem that can help to give an overview of the call tree or the call dependencies? Perhaps by rendering an html page, generating a png or something similar.
Thank your for your thoughts!
This is an older gem that does that. It lets you view a tree-ish list of your partials.
https://github.com/skwp/PartialMap
I am completely new on Ruby On Rails and I already watched a long tutorial to start developing a small web application. In such a tutorial I could see several helpers for textboxes, textareas, dates, times, checkboxes, radiobuttons, comboboxes, and so on.
Where can I find other helpers like accordions, WYSIWYG editors (like an HTML editor), and others that can be bound to data from model and used in views? Maybe a toolbox for example.
I will very much appreciate your feedback.
Best regards.
What you're mostly talking about are Form Helpers. There are a bunch of other Rails Guides so I'd recommend reading through them and getting a better idea of what Rails does and can provide.
If you're not finding what you need in that documentation, you may need to add a 3rd party gem to your app's Gemfile, and follow the gem's documentation for getting it working. The Ruby Toolbox is a good place to start searching if you want to see which gems are most common.
And, of course, in the end you might not be able to find something that someone else already wrote and that solves your problem, in which case you will need to write it yourself. For front-end stuff you'll want to get up to speed on how to use HTML, CSS, and Javascript.
Say the view is in erb/haml, but is static (doesn't have any dynamic parts within it), does Rails recompile that view every time someone hits it, or does it cache the html after someone hits it once?
A follow up question: if I have a view with some dynamic parts, does Rails recompile only the dynamic part of the view or does it recompile the whole page?
I'm running Rails 4.
Rails evaluates view files and partials on every request. That is why html fragment caching is so valuable.
See Caching with Rails in Rails Guides.
Typically you would use Rails' cache to cache a html fragment so it does not need to be re-rendered on each request. Here's a Haml example:
- cache "key-name-for-static-content" do
.some-html
some content
See DHH's How Key-Based Cache Expiration Works for key-based caching using models.
For advanced uses, I wrote cache_rocket to help cache static content around dynamic content in partials.
Just a bit of terminology note: now compile is commonly referring to the act of turning the view template into a ruby function. Rails does this for all views:
http://guides.rubyonrails.org/action_view_overview.html#template-caching
Those functions will then output the output you want.
So I would say views are not "re-compiled" but "re-evaluated".
My goal is to create a Rails based site that uses AJAX to fetch different sections. The site should never completely refresh. I'm using Rails 3.2.8.
There's a lot of conflicting articles online about how to actually implement this. It seems to me that simply fetching pages.json and using javascript would accomplish my goal, but is that the "rails" way?
Every page that is users will see is static. I'll be using Rails as an admin to CRUD them, but that's it, and that portion doesn't need to be AJAX.
Take a look at backbone.js. For an ajax heavy site, that's exactly what you need to help organize your code and keep your front end consistent with your database. Also, check out this excellent railscast on implementing backbone in a rails project.
I noticed that you said static site. Well, if the site is completely static, why bother with something like rails? I would suggest just coding it in html and javascript because rails is intended to be used for dynamic, database driven sites.
I was using backbone standalone for some time but currently I am trying to integrate it with Rails. Until now I used underscore templates and the question would be if it is possible to use Rails view helpers inside the template and if it is smart thing to do at all?
Update: Here is a simple example what I am talking about.
I have a list of messages and I have a MessageView for each message, I want to render the avatar thumbnail of the message author, link to his profile and description when the message was posted. Also I use markdown for the message content. With underscore templates I don't have access to the helpers to achieve this so I am forced to create methods on the model itself which feels really wrong...
You should take a look at the EJS Embedded JavaScript Framework, which provides rails-like standard view helpers like link_to, url_for, and other form tags.
Of course, you will have to translate your custom rails templates in js, but it's a start !
I ran into the same problem where I wanted to reuse my templates between Backbone and Rails. I ran into stache before: https://github.com/agoragames/stache
You can read more about the setup here: http://slainer68.wordpress.com/2011/09/20/partial-reuse-between-rails-js-the-easy-way/
Right out of the box, your underscore templates are pure javascript, so, in that sense, you can't really embed rails helpers into them. You can, however, make those templates ejb's (or whatever templating system you use) and have rails render them. With so little information, it's impossible to figure out what your app does, but it does feel weird to me to do that. I think, typically, your javascript templates are used for rendering html on the host side after some js functionality. Maybe a better description of what you are trying to accomplish?
Update ...
So you have some set of relationships between messages and authors in your rails models correct? You'd do a similar thing in your backbone models. So, you've got a User model, and a Message model. User has_many Messages, and Message has_one User. You can model that out in backbone as well... see my answer here:
Backbone set collection attribute (for the url)
You just need to describe the relationship on the backbone side.