ruby - Do Actions in RCAV = Model in MVC? (Rails) -
i'm trying better understand rails workflow, rcav in relation mvc. know mvc typical structure of rails app , rcav standard order of building various components of app, however, i'm little confused correlation between two.
for example, i'm assuming routes in rcav link models views , controller in mvc. correct?
i'm guessing controller , view in rcav same controller , view in mvc , represent order in build them. correct well?
what i'm stuck on action part of rcav - represent model component of mvc?
sorry if question doesn't make sense, trying better hang of standard rails workflow , order in various components of app typically built. wish little more distilled i.e. "first build model, views, controller" whole separate rcav thing confusing me little.
to title question, no.
to elaborate, it's kind of confusing map out rcav (the sequence) against mvc (the framework), i'll see if can clarify. it's worth noting concepts not complex seem, it's confusing explain.
rcav (route-controller-action-view)
routes defined in config/routes.rb. may think of them coupling between http request , method (a specific type of method called action). on 1 side defines path (ex. "/login") , 1 of http verbs (ex. "get"), , says whenever type of request made location, perform appropriate action. on other side, action given in form of name of controller (ex. "sessions"), , name of method (ex. "new").
controllers receive request according routes (so following above example, if user navigated www.example.com/login, wind in sessions controller, housed in app/controllers/sessions_controller.rb). controller may perform number of functions prior (and following) action, in form of callbacks. callbacks arguably more advanced concept need understand right now, important step (and reason explain rcav "route >> controller >> action >> view", not "route >> controller-action >> view").
actions public methods in controller. again, our above example, you'll hitting method named "new" in sessions_controller when user navigates www.example.com/login. actions may perform variety of functions, , there interaction model here (for instance, may have session model in app/models/sessions.rb method named "logout", , perhaps first thing when hit login page ensure user logged out calling session.logout. poor design, decent example). action must "return" in 1 way or another. assuming action meant consumed user directly through browser (as opposed as, say, json or xml service), either render view now, or redirect_to action.
views are, dare say, "end result" of action. if nothing specified, above example end rendering "app/views/sessions/new.html.erb" user (rails defaults rendering view path matches controller , action, referenced above name different). in file potentially make reference model associated sessions (but time inside of erb tags, compiled html).
mvc (model-view-controller)
models classes tied, generally, tables in database. contain validations fields, class methods, , instance methods. database keeps track of sessions. potentially grab list of sessions calling session.all anywhere in application. you'd use action in sessions controller save list instance variable (maybe @sessions), in turn accessed in appropriate view (<% @sessions.each |s| %> ... <% end %>).
views web pages. can bring along own styles , scripts, , exist in form of html files embedded ruby. end point of request made site (unless you're hitting service returns json or xml instead).
controllers contain actions user may access, prepare variables via methods models, , drop user view.
in summation
a user accesses route. route drops them specific action within controller. controller (in form of callbacks) , action (on own) prepare data model (via instance variables) consumed view. view rendered users browser.
this is simplification, in standard cases, how mvc framework (and rails, specifically) functions.
Comments
Post a Comment