Should I use .erb or .rhtml files for a Rails app

2019-07-07 03:25发布

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?

7条回答
女痞
2楼-- · 2019-07-07 03:55

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

查看更多
Explosion°爆炸
3楼-- · 2019-07-07 04:01

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.

查看更多
女痞
4楼-- · 2019-07-07 04:04

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.

查看更多
Bombasti
5楼-- · 2019-07-07 04:13

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.

查看更多
闹够了就滚
6楼-- · 2019-07-07 04:14

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.

查看更多
Root(大扎)
7楼-- · 2019-07-07 04:21

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.

查看更多
登录 后发表回答