BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Model View Controller (MVC) 2020

RubyOnRails_logo




Bookmark and Share





bogotobogo.com site search:





MVC

MVC_Rails.png

When interacting with our application, a browser sends a request, which is received by a web server and passed on to the Rails routing engine. The router receives the request and redirects to the appropriate controller class method based on the routing URL pattern.

The controller then takes over. In some cases, the controller will immediately render a view for the browser. More commonly for dynamic sites, the controller interacts with a model.

After invoking the model, the controller then renders the final view (HTML, CSS, and images) and returns the complete web page to the user's browser.

Rails has an application directory called app/ with three subdirectories: models, views, and controllers. This is the model-view-controller (MVC) architectural pattern, which enforces a separation between business logic from the input and presentation logic associated with a graphical user interface (GUI).

In the case of web applications, the business logic typically consists of data models for things like users, articles, and products, and the GUI is just a web page in a web browser.






Action Controller

When we first create an app, for example, DemoControllerApp, there is not much in our app/controllers directory. We only have application_controller.rb which has the following in it:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
end

ApplicationController inherits from ActionController::Base, which defines a number of helpful methods.

Suppose we want to create a users controller and corresponding user views, we run rails g controller users in our command line.

$ rails g controller users

This will create a users_controller.rb in our controllers directory, and will allow us to set up additional routes for our user model. This will also create users in our views directory.

What's in the users_controller.rb?

class UsersController < ApplicationController
end

A controller is a Ruby class which inherits from ApplicationController and has methods just like any other class. When our application receives a request, the routing will determine which controller and action to run, then Rails creates an instance of that controller and runs the method with the same name as the action.

Let's define a new method. Just the empty definition:

class UsersController < ApplicationController
  def new
  end
end

As an example, if a user goes to /users/new in our application to edit a profile, Rails will create an instance of UsersController and run the new method. Note that the empty method from the example above would work just fine because Rails will by default render the edit.html.erb view unless the action says otherwise.

The new method could make available to the view a @user instance variable by creating a new User:

class UsersController < ApplicationController
  def new
    @user = User.new
  end
end

Only public methods are callable as actions. It is a best practice to lower the visibility of methods which are not intended to be actions, like auxiliary methods or filters.- Action Controller Overview.





Routes

Basic Rails routing is relying on controller and its actions. In other words, the Rails router recognizes URLs and dispatches them to a controller's action.

But sometimes we may need different approach. For example, to make a link to another page with the same controller. Especially, the page does not have any VERB actions.

It may look ugly and they are not RESTful controllers. So, I do not like it, but so far that's the only way I found.

In this example, I have a controller called googleplus and I want to have lots of pages under views/googleplus folder. How can I construct links to those pages?

Here it is:

app/controllers/googleplus_controller.rb:

class GoogleplusController < ApplicationController
  skip_authorization_check
  skip_before_action :authenticate_user!

  def index
  end

  def secondpage
  end
end

config/routes.rb:

get '/googleplus' => 'googleplus#home', as: 'googleplus'
get '/googleplus/secondpage' 

Suppose, we have create 2 files in our views:

app/views/googleplus/index.html.erb
app/views/googleplus/secondpage.html.erb

Then, we can create links with:

<%= link_to 'Page 1', googleplus_path %>
<%= link_to 'Page 2', googleplus_secondpage_path %>




Bogotobogo's contents

To see more items, click left or right arrow.

I hope this site is informative and helpful.

Bogotobogo Image / Video Processing

Computer Vision & Machine Learning

with OpenCV, MATLAB, FFmpeg, and scikit-learn.

I hope this site is informative and helpful.

Bogotobogo's Video Streaming Technology

with FFmpeg, HLS, MPEG-DASH, H.265 (HEVC)

I hope this site is informative and helpful.







Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Ruby on Rails



Ruby On Rails Home

Ruby - Input/Output, Objects, Load

Ruby - Condition (if), Operators (comparison/logical) & case statement

Ruby - loop, while, until, for, each, (..)

Ruby - Functions

Ruby - Exceptions (raise/rescue)

Ruby - Strings (single quote vs double quote, multiline string - EOM, concatenation, substring, include, index, strip, justification, chop, chomp, split)

Ruby - Class and Instance Variables

Ruby - Class and Instance Variables II

Ruby - Modules

Ruby - Iterator : each

Ruby - Symbols (:)

Ruby - Hashes (aka associative arrays, maps, or dictionaries)

Ruby - Arrays

Ruby - Enumerables

Ruby - Filess

Ruby - code blocks and yield

Rails - Embedded Ruby (ERb) and Rails html

Rails - Partial template

Rails - HTML Helpers (link_to, imag_tag, and form_for)

Layouts and Rendering I - yield, content_for, content_for?

Layouts and Rendering II - asset tag helpers, stylesheet_link_tag, javascript_include_tag

Rails Project

Rails - Hello World

Rails - MVC and ActionController

Rails - Parameters (hash, array, JSON, routing, and strong parameter)

Filters and controller actions - before_action, skip_before_action

The simplest app - Rails default page on a Shared Host

Redmine Install on a Shared Host

Git and BitBucket

Deploying Rails 4 to Heroku

Scaffold: A quickest way of building a blog with posts and comments

Databases and migration

Active Record

Microblog 1

Microblog 2

Microblog 3 (Users resource)

Microblog 4 (Microposts resource I)

Microblog 5 (Microposts resource II)

Simple_app I - rails html pages

Simple_app II - TDD (Home/Help page)

Simple_app III - TDD (About page)

Simple_app IV - TDD (Dynamic Pages)

Simple_app V - TDD (Dynamic Pages - Embedded Ruby)

Simple_app VI - TDD (Dynamic Pages - Embedded Ruby, Layouts)

App : Facebook and Twitter Authentication using Omniauth oauth2

Authentication and sending confirmation email using Devise

Adding custom fields to Devise User model and Customization

Devise Customization 2. views/users

Rails Heroku Deploy - Authentication and sending confirmation email using Devise

Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger I

Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger II

OOPS! Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger (Trouble shooting)











Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master