views:

775

answers:

5

I'm just starting to learn Rails. I created an empty Rails app. And instead of writing a Controller, I want to put all of my Controller logic in my Views instead of in separate Controller classes.

To do this, should I use .erb files or .rhtml files and what's the difference?

+19  A: 

First of all, they are virtually the same thing but you should use the new standard naming format of .html.erb

Second of all, stop what you are doing and reconsider everything!!!!!

The whole point of MVC is to separate logic from display and vice versa. Most of your logic should be in your models and the controller should just facilitate grabbing that logic and passing it to your views.

You should not do anything in your views other than display the data.

cpjolicoeur
So you're saying in terms of how they are interpreted by the compiler, erb and rhtml will treat their Ruby code the same way.
Junas
It's a small app. I don't want to waste time writing Controllers if I don't need to. It seems ridiculous to insist that you should ALWAYS use Controllers.
Junas
yes, but the new "standard" way to do it in Rails is to use the formatfilename.html.erb.Also, you are not wasting your time with a controller. You will still need the controller file for the views to be accessed so put the code there as is the standard and save yourself lots of headaches down the road.Why do it wrong from the beginning?
cpjolicoeur
to be honest and sincere, its ridiculous for you not to use controllers
cpjolicoeur
If you are doing a straight html site then don't use controllers just put it in an html file (or even shtml file) and don't use rails. If you are doing anything else with processing on the server then from the get go use controllers, models, and view.
Solmead
@Solmead - I want to use Rails.
Junas
This sounds like how a lot of temporary or small or "it's just going to do this one easy thing" applications start, and then ten years later they're a monolithic beast that everyone is afraid to touch for fear of it breaking because it was poorly designed in the beginning.If something's worth doing, it's worth doing right.
Sarah Vessels
If you want to use Rails, use it the Rails way : any code except presentation should be in models or controllers. If you don't do that, you're not using Rails.
tal
+2  A: 

In the new rails 3.0 .rhtml files will be unsupported. .html.erb is the new standard.

I understand that you have a small app and standards don't really apply to you, but that is the whole point of MVC. The logic should go into the controller/model and the view is strictly for presentation.

puttputt
If you are so dead against controllers and your app is so small, maybe Rails is not what you need. Have you thought of other frameworks like Sinatra?
cpjolicoeur
I've thought about microframeworks but decided I want to learn how to use the "standard" Ruby framework. The creators of Rails never said you shouldn't use Rails for smaller apps. Nor have they ever directed anyone to use Sinatra instead of Rails. I don't believe in blindly adhering to MVC under all circumstances. I believe I should be able to use Rails in a way that seems most appropriate to my needs. Thanks for the advice.
Junas
well, if you want to learn how to use the "standard" Ruby framework of Rails, then you should use the MVC concepts as that is what Rails is based on.
cpjolicoeur
@Junas: You're right that you shouldn't blindly adhere to anything. But hey: Wax on, wax off. Learn it the "right way," and then learn when to break the rules. Attempt it in the other order and you risk never learning the right way, picking up bad habits, and probably missing most of the points along the way.
Ian Terrell
+1  A: 

Yes, you are correct in that the creators of rails never stated that you should not use rails for smaller apps, but they have stated over and over the importance of the controller.

I created an empty Rails app. And instead of writing a Controller, I want to put all of my Controller logic in my Views instead of in separate Controller classes.

Just out of curiosity, what type of logic are you considering putting into your views? If it is presentation logic then that is one thing but if it is business rules, loading data from a database, xml file, web service/rest based then you are violating the core principles of rails. Ever heard of ASP (Classic Active Server Pages)? Frameworks have evolved beyond that to overcome the shortcomings and pitfalls like ASP to allow you not to mix presentation and code. If you jam it all together, how will you unit test your code? Another key principle of rails that is why it is built into the framework itself unlike other web frameworks.

I want to learn how to use the "standard" Ruby framework

In your responses you keep mentioning you want to learn the standard Ruby framework? If this is the case why don't you use irb then? Rails is not part of the standard Ruby framework. In fact you will probably learn a lot more about Ruby using irb then you will Rails. Once you have familiarized yourself with Ruby then take on rails.

I agree with the others and if you are going to take the time to learn a framework, then learn it right and as the creator intended, otherwise you are missing the point and you will not see why rails is such a good web framework to begin with. What you are hoping to accomplish can be done in a number of web technologies: ASP, ASP.Net, PHP, JSP, Perl, but you choose to learn Ruby and rails therefore do not do it the same as you could in any of the other web technologies.

+3  A: 

The simple answer to your question is no. No you shouldn't put controller logic in the views. If you don't need controllers then you probably don't need rails. I know that's not the answer you want, but frankly you are wrong, pure and simple. If you want to learn the Rails framework, then what you have been told here is correct and to do it your way would simply then mean either unlearning what you just did or it would mean becoming a bad developer.

That's the way it is, the rest is now up to you.

railsninja
+6  A: 

"A client has asked me to build and install a custom shelving system. I'm at the point where I need to nail it, but I'm not sure what to use to pound the nails in.

Should I use an old shoe or a glass bottle?

In your case I'd go for the glass bottle.

garrow