0
votes

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 ?

3

3 Answers

2
votes

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.

0
votes

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.

0
votes

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.