Should I use .erb or .rhtml files for a Rails app in which all Controller logic exists in Views?

人盡茶涼 提交于 2019-12-12 15:20:29

问题


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?


回答1:


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.




回答2:


"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.




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


Adhering to MVC is the way to proceed to build an application. If you are uncertain why Controller is needed then do the research. I have faced maintaining code where the scrips are embedded in the presentation layer. It is farcical to begin any engineering effort without a thorough understanding of the correct, time tested methodology. It is like trying to build a house using no foundation or blueprint.




回答7:


Nothing, really. It’s just a change of philosophy between Rails 1 and Rails 2. Before Rails 2, you had file.rhtml, file.rxml and file.rjs. In Rails, that changed to file.content_type.template_engine. So with file.html.erb, the content type is html and the template engine is ERb. rxml is now xml.builder and rjs should now (mostly) be js.rjs

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



来源:https://stackoverflow.com/questions/821010/should-i-use-erb-or-rhtml-files-for-a-rails-app-in-which-all-controller-logic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!