I am building a CMS using Ruby on Rails to teach myself the framework. One thing I am trying to work out and can't find resources for is how to build it in a modular format (By this I don't mean Ruby modules, but like the style of Zend Frameword Modules)
so for example:
App : Admin : Controllers
: Views
: Models
: Pages : Controllers
: Views
: Models
: Localisation : Controllers
: Views
: Models
Now I know that there may be a different way to achieve this, but I guess my specific aims are for easy separation of different parts and easy drop in/out, so for example a localisation module could be dropped in to achieve specific extra functionality for localisation etc.
Edit:
I am not trying to build this in the same way as ZF, however I am assuming there is some way to organise sections of the site. I am trying to work out what RoRs answer to this kind of 'problem' is. I am just using ZF as an example.
My best bet would be to use Rails Engines. They are built to be drop-in modules including views, controllers and models. Basically a way to include a Rails-application into another.
Trying to build an application in one framework the way another framework does it defeats the purpose. It is a nice inclination to want to do that but you are doing yourself no favors by doing so. Rather you should apply your experience to learn how RoR really works, what it's conventions are and then adjust your thinking and approach to solving the problems at hand accordingly.
Related
I want to use Zend Framework 2 to build an medium size website. So, I need the one place to store some global abstract classes, controller plug-ins, view helpers. For this purpose, I decided to utilize the Application module.
Is it good or bad? If bad, where should I put all common/abstract classes to be available for whole application?
Thanks!
It is OK to do so. However a better approach would be to separate into it's own Module. Something like Stdlib or Core. This one then you could simply copy-paste into all of your other projects.
i want to develop a simple CMS.
i want to add the ability to add modules to the CMS.
what need to be the architecture for
such a CMS?
a solution and then a mvc project
for the site and another for the
admin? or one project with area for
the admin?
in mvc each of my modules will have a controller,model and views. if i will put all in one project, then i will mix of all module ,each module will be in 3 folders (controller,model and views).
how i need to arrange them so my code will be nice and clean?
Since you are only separating by type of user, you can surely (and easily) include in one project. all of your Admin controllers should have [Authorize(Roles="Admin")] on them to limit them only for the admin. Mixing them is fine, other applications mix user roles in an application on a regular basis, just limit the difference by security (and dont use url restrictions in your web.config - use the [Authorize] attribute on your controllers instead!!
If you expect the differences in both applications to be huge then you can separate into another project, but I'm imagining you can get very good reuse by including them in the same project.
How to architect this is a very very broad question. For a basic project some include everything all in one project. I prefer to break out all my models and data access code into a separate project and try to code to interfaces as much as possible for unit testing purposes. I think that is all a bit beyond the scope of the posting here though. To start - work with the mentioned attribute and I think you will develop. Start unit testing early and I think that will help steer you into the right direction. Also read up on dependency injectiom , unit, ninject, etc for ways to dynamically bind to your implementation classes as this makes your unit test go quite a bit smoother as well.
I've been wondering about best practices on creating a project in MVC, and let others create extension/modules/plugins and skins/themes etc, for it.
For example WordPress is great at this. Just upload a file or folder. Or even browse the plugins directly from your WP administration and install by a simple click.
Does anybody here have a few tips on how to create a project that allows that flexibility.
For the skins I might need to implement my own viewengine, right?
Any good resources and tips would be great.
Thanks!
Skinning isn't that hard, just swap stylesheets. For using a different set of templates, yes, you'd need just customize, not write your own, viewengine. This is pretty easy and all you'd have to do is tell MVC to look in different places for your views:
http://www.dotnetguy.co.uk/post/2010/01/31/ASPNET-MVC-e28093-Dynamically-Changing-The-Master-Page-(Theming).aspx
The MVC plugins widgets question has been asked before:
https://stackoverflow.com/questions/2230482/asp-net-mvc-2-widgets/2230833#2230833
I ended up using a VirtualPathProvider since I wanted the views outside of the application. Now, we made it possible for the views to be stored in the database.
I've been programming for many years and have just recently got the RoR "itch". I'm thinking about how I could utilize a graphic designer and wondered how best to collaborate with them:
1) Do scaffolding and then show them where to place the graphics in the views (seems ugly)
2) Have them do screens and make them keep a certain dir structure for all media links i.e. /public/images public/stylesheets, etc. Will this confuse the graphics designer LOL?
3) Better to just have them build the images (like a header or footer image, etc.) and place them in yourself. But this approach means you have to start thinking about design and layout which seems better left to the graphics specialist.
All of the above seem flawed to me and I was wondering if there are any "best practices" for this problem? Searched for articles, etc., but didn't find much. Suggestions? Thanks all.
i work in a company that works 100% with ruby on rails development. which approach we take to integrate views.
First thing that we develop are views. The design team develop a view, tell us which data will be filled in this view and our development team develop the necessary ERB code. So normally a development process are like:
We develop some views with the client
Our dev team do the spec for functions
The design give to us the application layout
We implement it and start to work in the first models and controllers spec'ed
New whishes and views will be asked by the client, that will be sent by our design team and it will be spec'ed by our dev team.
Normally give them their own rails server, teach them the basics of where they need to add controllers and views (no need for any model stuff) to work with the default route.
I integrate the HTML, they stick in the CSS.
Teaching them how to render partials helps them with consistency. Think that's about it.
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?
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.
"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.
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.
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.
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.
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.
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.