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
Related
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
This might seem like a strange question. But have been pondering over this for quite some time now.
Is it better to use RAW HTML in Rails views or to go with Rails view helpers??
If i understand correctly Rails views helpers are converted back to raw HTML. So would it affect the performance of the application by a great deal ??
And is there a tool that would allow me to convert HTML to ERB :)
I'm aware of the HTML to HAML converter.
Thank you,
Certainly better to use a rails helper, as this allows you to create dynamic elements
Use Rails helpers, they guarantee the latest HTML good practice are used and up to date.
Being quite new to rails and currently building a project, i'm begining to be in a situation where my view folder is growing a bit too much
I have for e.g :
/app/
../views/
..../comments/
....../_comment.html.erb
....../_comments_count.html.erb
....../_form.html.erb
....../create.js.erb
....../destroy.js.erb
....../edit.html.erb
....../edit.js.erb
....../index.html.erb
....../index.js.erb
....../new.html.erb
....../show.html.erb
....../update.js.erb
I would definitely prefer to have 2 files :
comments.html.erb
comments.js.erb
And inside of each (like in controller) have a part for each actions.
Currently it seems too much trouble to edit each files, even if they are skinny.
How do you manage your view files ? Is my comments view folder "normal" for a rails project ? Is there some templates engine like handlebar that can help address this problem ?
This is a bad idea. Rails splits up actions into different views so that everything is modular and more easily maintained. There is no way to simply combine everything into a monolithic file and call the parts you want on a per-action basis; that's the point of your controller.
If you're having a hard time editing different files, I wouldn't say that's a problem with Rails behavior but with your development environment. Many IDEs and editors have features or plugins that assist the process of dealing with many files. However, your case is pretty standard for a CRUD view.
In rails 3.1 and beyond, JS files go in the assets/javascripts folder for use in the assets pipeline...which if I read between the lines you'd probably like even less. But aside from that, this looks pretty normal.
Having too much in one file violates the Single Responsibility Principle. Each file should have only one reason to change.
I agree with #rpedroso. Rails sets up these things for a reason. You can choose to disagree with the reason and do it differently; presumably you have a deep understanding of the system and the tradeoffs involved. But doing things differently without knowing the reason is just careless. Rails is so popular because it lays down easy-to-follow conventions which are strongly recommended, because everybody knows what they are and can speak the same language with you. Disregard the conventions at your own peril.
I recommend Michael Hartl's Rails Tutorial as a wonderful intro to Rails.
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.
I have a project going on, and I am in charge of the front-end design, whereas my developers will work on the back-end with Ruby on Rails.
I do not know Ruby on Rails, and am designing front-end using XHTML, CSS, jQuery, 960.gs CSS Framework. My developer is supposed to take my design and connect the elements of back-end to it, with Ajax too.
What are the things that I should know while designing the template/view so that I won't kick my developers' asses with my design? How to help the connecting of elements painless? I understand I must avoid . Some Ruby on Rails developers also prefer Blueprint CSS Framework over 960.gs.
Any guidance? Thanks.
Generally the Rails templating system is quite flexible and will enable developers to create even complex designs. The CSS framework should not make such a difference. However if they are using Rails 2.x it is markedly easier to use Prototype instead of jQuery. However Rails 3.x is also agnostic to javascript library.
A relatively good overview is the official guide. You might also try out this tool for cutting up your views and layouts.
I can speak from the developer point of view.
The things that piss me off are too complicated css structures, keep it simple and abstract as you can.
The other things is naming classes and ids, in general try to find out what models the developer is using and name your classes and ids accordingly. E.g. for a blog with posts:
#posts .post for a post in the index view and #post for a post in the show view.
I never care what css framework the designer uses, as long as it works.
And lastely, if you design different html pages, be aware that we often have only one or two layouts, that's eg. /views/layouts/application.html.erb and we usually try to keep that number low as possible.
jQuery is find, ever been my preferred choice over rails' own prototype.