Ruby Controller Views Actions and Model setup - ruby

I have been coding in php for a year now and i have recently decided to start learning Ruby because i heard its a better web language. I'm trying to convert what i have on the website so far into Ruby code, but I'm having trouble understanding the structure for controllers views and models. Heres what i have for php pages.
Homepage(controller)
- index page
- about page
- etc.
Signup(controller)
- index page for signup
Signin(controller)
index page for signin
Admin Page(controller)
Business Page(controller)
Do i make all these controllers and what do the actions and models represent? Is the action the form to signup.
Thanks in advance

I'll try my best to give a condensed answer. RoR follows the MVC pattern (Model, View, Controller) typically controllers handle url requests, models store data and views are used to display the data to the end user. To answer your question, you would not need to create seperate controllers for each individual action. Controllers contain methods for creating, reading, updating, deleting (CRUD) and much more. For example: You might have a UsersController, within this controller are four methods; create, new, update, and delete. This would give you a URL something like /users/new /users/1/edit /users/1/delete
An application like the one you give as an example is quite simple. to seperate an "admin" page is simple and would be something like
<%= if current_user.admin? %>
content only the admin can see
<% end %>
Creating users would involve storing their details in the database, creating a session and allowing users to create and destroy sessions when they login and logout.
The major difference between RoR and PHP (to me, anyway) is PHP makes the views messy by embedding logic code directly into the .html page. Ruby on Rails provides a clean way of seperating logic from html content making it easier to manage or develop between a group.
Pages such as the root page, and about page or contact page might all be methods in the HomeController and would be accesible via /home/index /home/about /home/contact
http://guides.rubyonrails.org/getting_started.html
The above link would be the best place to start learning about the fundamentals.

Related

Codeigniter - a dynamic sidebar

I only started with this a couple of days ago, so this may be a silly question...
I have an admin area with its own directory "admin" within the controller folder. So if the user is in the admin area, I want a sidebar to show. But obviously there will be a fair few "pages" (controllers) within the admin area. I have Clients, Services and Dashboard.
In the sidebar (on all pages) I want a list of clients and services so when clicked, it goes to a page and display info for that client/service.
I sort of have it working with add_service(), edit_service(), view_services() etc... but in each of these methods, it seems like I need to load services AND clients models, and pass the data back to each view... in all methods? So if I want to add a service, I click "add service" and it takes me to the add_service(). Do I need the below 4 lines in each method?
$this->load->model('client_model');
$this->load->model('services_model');
$data['clients'] = $this->client_model->get_clients();
$data['services'] = $this->services_model->get_services();
I have read about widgets, but not sure if that's what I need exactly.
Thanks
As you read, all you need is HMVC. This will create a "widget" with your controller, model and views, and you'll be able of calling "a whole": A part of the website wich manage its own part.
One solution is implement https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc into your code, you create a module called "client", and in your "admin" controller you call it. You'll get your client table there, and in your controller "client" you'll be able of call it too, and you'll have exactly the same behaviour. I've implemented wiredesignz HMVC quite times and I think is the solution for your trouble. Anyway, you could also check https://github.com/Bigwebmaster/codeigniter-modular-extensions-hmvc, which is a fork with improvements into the code.
About your sidebar, you'll have to call the widget you'll create with your clients, and you'll have it quickly with only one call, because the module will manage all the behaviour

Using the ion_auth login process and using a templated process to render the login process

I'm a new kid with CI and ion_auth and have both running correctly. I have also created a view/template of a view/template/header and view/template/footer on another page ('welcome') that is a part of a fairly complex html5 responsive template with all my assets in a directory called assets/html5templateV1/css etc.
The question is what is the best approach of using the views/auth/login.php file with the template so I have
A nice looking login page /failed login / request pwd page etc.
The login page uses a templated HTML layout approach (mine is probably incorrect)
Updates to ion_auth can be fairly easily installed
The next person along doesn't say "WT?" and start pulling hair out.
Thanks
There is no need to use ion_auth views or controllers. they are just for reference. we have to use their function as they have define in model for login/logout/register etc... just simply put his model,library and config file to the respective folder of codeigniter in your project and just use their function as they have shown in demo

Zend Framework :: General App Design Question

I just started learning Zend (& OO PHP for that matter), I have spent the last 4-5 weeks learning, tutorials, books etc. I feel good about it but will bog down in the models (thats ok, I'm learning). I am now beginning my first app (for work even); It has at least 5 major sections (including the login + will need ACL), and a couple will have up to 10-12 sub sections like admin: create user, edit user, etc.
I created a single layout, and have made most of the page views with working links, and have a few of the forms complete already.
My major concern now is should I refactor and make modules of the major sections before it gets out of hand, or am I worried about nothing. One thing I think I did wrong is that I have a 'AdminController' that does nothing but bring in the admin 'view' that is nothing more than links to each 'user' action in the 'UserController'. I'm thinking maybe I should have put the user actions in the AdminController. I'm thinking too, that I should make a 'admin' module, 'reports' module, 'auth' module, etc. Or is it normal to end up with 8 controllers and growing? I already have the inclination to make and maintain a developer's sitemap just for my own sanity, not to mention that I want to do the best job possible :)
In principle, I like the idea of a plugin-able module for each set of functionality - News, Users, Galleries, etc. "Plugging in" that module would provide functionality for the back-end admin and the front-end display. It is a self-contained place to put all the functionality - models, action helpers, view helpers, view scripts. etc - that you need for that content area. There might be two controllers per module - News_BackendController and News_FrontendController - dedicated to their specific areas.
But in practice, I find that ZF modules make that hard. I know that smarter guys than me - a low bar, to be sure - can make it all work, but I've never had luck with it.
So I usually end up with two modules - frontend and backend. For news functionality, for example, I'd have a news controller in the backend module for managing the content; another news controller in the frontend module for displaying it.
The sticky point for me in this setup is where to put model functionality that is common to both frontend and admin. One idea is to put it out a separate library and then create module-specific models that extend these for any module-specific functionality. Something like:
MyLibary_Model_News for the common news stuff.
Frontend_Model_News extends MyLibrary_Model_News for any frontend-only news functionality, if any.
Admin_Model_News extends MyLibrary_Model_News for any backend-only news fnctionality, if any.
Just some ideas. As always, YMMV.

codeigniter, admin system and auto load

Hello im working on a project and i have a footer and a sidebar that i want to load some information from the database, how can i do so it load the same on all pages.
i want to make a admin system to, how should i do that?, do i need to have a new codeigniter instillation or can i just create a new map in my controller,model and view maps ?.
how you guys doing it?.
The way I would go about it is create a MY_Controller and then put all of your generic logic inside of it and then all of your other controllers extend your MY_Controller. This saves you having to fetch content repeatedly and define and write the same code over and over again inside of your controllers. See Phil Sturgeon's article on base classes and keeping it DRY.
For templating your site including an admin panel, Phil Sturgeon has also created a simple template library that allows you to have themes on your site with different layouts you can switch between, etc.
As for creating an admin panel, Phil has also written a post on the subject as well and he goes into quite a bit of detail about the various ways you can develop an admin panel, which approach is best, etc. Some of the comments on the article are also very helpful too. Read his admin article here.
The templating method I usually use is as such... (it obviously is dependent on whether your sidebar, in your design, comes before/after the "content" in your markup)
<? $this->load->view('path/to/header') ?>
//content of page
<? $this->load->view('path/to/sidebar') ?>
<? $this->load->view('path/to/footer') ?>
Now if you are going to have variables you will need for every view, you can load them globally like so in the constructor of your controller.
$data->some_variable = $some_information;
$this->load->vars($data);
This will make $some_variable available to all views you load from that controller.
An admin system is simply another area of your site/application that is simply protected by an authentication system. You first need a way to verify the user's identity. I generally use Ion_Auth as my preferred auth library, and I have done a fairly extensive write-up on how to set up Ion_auth and your "protected" Controllers in a very clean fashion.

Static pages in MVC framework?

Where do you guys put your static pages, like "home", in an MVC framework? Do you have a "home" controller? A "pages" controller? Do you create actions for each static page?
I'm using CFWheels now, and I'm trying to figure out the best place to put them.
Edit: Apparently CFWheels doesn't require you to create actions for all your views. So you can just create an empty controller and call the views like actions, but not have to write out the blank functions.
CakePHP (and I guess, Ruby On Rails) has a "pages" controller. There is a routing function which redirects requests to /pages/foo to /pages/display/foo. Similarly, / is redirected to /pages/display/home. The display action looks up the views/pages folder for any file with a matching name and renders that.
At the end of the day, a static page is a view without a model, that was returned based on an action the user requested from your server by hitting particular route. :-)
Yes, technically you could expose the direct location of the view resource to the user and rely on the http daemon to go fetch it and return it. However, that means that the resource URL is now tied not to the semantic of the resource you want to expose, but to actual bits. This means that if you want another representation of that same resource, you have to expose it on a different URL.
So, when you create the structure of your web app, think first about the URLs and the resources you want to expose and then think how to implement each resource.
I put my static pages in the database using a simple CMS with a private admin page.
This way, the clients can make simple changes themselves.
In Wheels, you don't even need to create the controller file.
If you create your view here:
views/about/index.cfm
You don't need to create the controller file at all. Then you should be able to just call this with no problems:
http://www.example.com/about

Resources