Displaying and updating multiple models in one view - ruby-on-rails

I have 3 different models, business_info, business_hours, business_vacations.
I have one view that displays all that information, so I made a new controller called business_all to gather all that info.
But when it comes to editing the individual parts within the page I'll be calling each of those 3 individual controllers to update, delete etc, then send back the updated data for that particular model.
Given that, should I skip having the business_all controller and instead, when the view is loaded have 3 ajax calls to each controller to get the relevant info?

one alternative is to make a non persisting model called business_all that has fields that combine the three models with its own validation and so on. and when persisting it actually saves to the actual models.
this way doing the forms and so on are easier as you can do form_for (BusinessAll.new ... ...) and so on.

Related

understanding MVC in simple words

i checked google, books etc but all defines it differently and made me highly confused. Please describe it simply that what parts does what and contains what or can ?
lets start with
CONTROLLER:
MVC always start with the CONTROLLER. its connects your VIEW and your MODEL. CONTROLLERS are responsible for your business logic like validation, computations, etc.
MODEL:
responsible for obtaining/storing data in your database.
VIEW:
displays data, your User interface.
The Model-View-Controller as it implies contains three components when
this model is implemented:
The Model - This should take care of all the operations which deal
with the data required (in other words the business logic) for the
application, which is implementing the MVC model from now onwards will
be referred to as by the MVC application. The operations can mean
reading, writing data into the database, getting information from
remote machines via network, time consuming operations etc. The model
should also inform the view about any changes to the data happening in
the background. The View - This component takes care of presenting the
data to the user. With respect to the context of this article, i.e.,
WinForms, the view class will be tied around with the Form which will
be shown to the user. The Controller - This is the center and
important component of the MVC pattern as it ties the Model and View
together. The Model which manipulates the data and the View which
presents the data to the user does not know the existence of each
other or they interact directly with each other. It is the controller
which acts as an intermediary and ties them together. For example, the
controller takes the input from the user such as a button click and
informs the model to take appropriate action, if there should be an
action that needs to be initiated to manipulate the project data.
Check here
The View is the parts of the application that handles the display of the data.
You can say it GUI of your web application. It donot have any logic say database conenction logic.For example , if you want to display list of students from the DB , one way is put the select query in the .asp.,.cshtml,.vbhtml file and show the reult there . But in MVC view will have code of displaying recodes and model will have the code fore retriving it. Most often the views are created from the model data.
The Model is the part of the application that handles the logic for the application data.
Often model objects retrieve data (and store data) from a database.
The Controller is the part of the application that handles user interaction.
Typically controllers read data from a view, control user input, and send input data to the model.
It connect the view to model , model to view and user request for some page to view.
When user enter the url like ".../Home/Index" then it goes to the Controller first . There can be many controllers , here it will go to "Home" controller class and in that class it will call the "Index" action or say "method". That method will return the "Index.cshtml/Index.vbhtml/Index.asp" view back to user as response.
MVC seperate tha main logic from the view and controller is the one who handel those.

Where should I create an action to populate a select?

I'm quite new to Rails development and I came up with this question today. I have a method that returns some JSON data.
It is used to populate a select with a list of cities according to what was selected on a previous select (list of states). So, it's a simple method that loads a list based on some ajax parameter passed through and it is used all along my site.
I'm using Rails 4 and I placed this method on my HomeController. So everytime I need to fetch the list of cities, I call the HomeController to load the data.
Is this the correct approach or should I place this method on a more generic controller (like ApplicationController)? Is there a better way?
I think the best thing is to keep this modular. So you can create a separate controller for this, like StatesController - and possibly even a separate model if that makes sense for your application (I'm not sure where you're getting your data). There is no cost to having extra controllers, and this way your code is clean and organized, with each piece of functionality existing in its logical place.

ASP.NET MVC3 model with list

I have a minor problem in MVC 3. I'm creating an application, where my model, a shipment, consists of the following:
a user id (string, required)
a reference id (string, optional)
a list of order ids (strings, cannot be empty)
The index view of the application is where the user creates the shipment (model). Once this is done, the user has no further interaction with it (no edit, detail or list views).
My problem is this. I'm trying to use one form for both adding order ids, and for creating the shipment itself, using two separate buttons for submitting ("Add" for adding order ids, "Send" for creating the shipment). It seems that when I'm using the Create-action of my controller, that pressing "Send" overwrites my list of order ids with an empty one. However, if I'm submitting to the Index-action, and redirecting to Create on a press of "Send", my model validation is gone (ModelState only contains "submit").
Right now I'm using sessions to pass data around my controller actions, which is probably not the best way to do it.
TLDR; I need a way to add items to a list in a model, one at a time, while persisting other form data, and still be able to validate it.
Any suggestions?
It's better to take a look on your Actions, because it's not quite clear what are you doing there, but still it looks like you are tring to use same data structure to pass different tipes of parameters (one contains list, another contains general data). I think it's better to submit different data structures and build required result data object in each action.

Is it considered bad practice to write db queries in a controller rather than a model

I've read that best practice dictates fats models, skinny controllers.
Models should contain business logic such as getting a list of customers based on parameters sent from a controller.
Controllers should contain just enough logic to invoke methods within a model to return to a view.
However I see many examples and tutorials where there is logic within the controller such as a query that accesses a db to get a list of products. I was under the impression that the logic should live in a method inside a model. The controller can then invoke this method, rather than containing the actual logic to query the database.
So if I have a ProductController I might have a Index action which returns an Index View, and I would have a ProductModel which would house my logic to display certain products based on a query string(which the ProductController would pass to said model). Right?
So if I have a ProductController I might have a Index action which returns an Index View, and I would have a ProductModel which would house my logic to display certain products based on a query string(which the ProductController would pass to said model). Right?
That is correct. As per the Model-view-controller architecture:
The model manages the behavior and data of the application domain,
responds to requests for information about its state (usually from the
view), and responds to instructions to change state (usually from the
controller). In event-driven systems, the model notifies observers
(usually views) when the information changes so that they can react.
The view renders the model into a form suitable for interaction,
typically a user interface element. Multiple views can exist for a
single model for different purposes. A viewport typically has a one to
one correspondence with a display surface and knows how to render to
it.
The controller receives user input and initiates a response by making
calls on model objects. A controller accepts input from the user and
instructs the model and viewport to perform actions based on that
input.
Keep the data-related queries and operations in the model; stuff as much as you can in there in accordance with DRY (Don't Repeat Yourself). Make the functions reusable as much as possible so they can be ported into various controllers and used throughout the application as necessary.
The view should contain little - if any - logic outside of view-specific work.
Your controller functions should invoke the model functions required to retrieve and manipulate data, and should be as "thin" as possible (as you pointed out). The smaller and less involved the controller, the easier it will be to add asynchronous features that "don't reboot the application" on the front-end, making for a better user experience. (If you are concerned about this, anyway!)
The Add Controller dialog box in VS 2010 has the option of adding a scaffold with CRUD functions into the controller. This should tell you quite a bit above how the ASP.NET MVC dev. team views this debate.

Synthesize multiple action methods in MVC

We have a dashboard-style application with lots of individual bits of the screen that load asynchronously. Each of these bits really makes sense as its own action on the page's controller. However, the data will load more quickly if we reduce the number of http requests to 1. I'm considering creating an action that just returns a combination of results from several smaller actions, such that the web client can GET the actions separately or together. However, this seems like a hand-rolled solution to a common problem, so I'm wondering if there's built-in support for agglomerating actions in this way that I don't know about. Anybody?
In your core view, you can use the Html.RenderAction() method to render completely separate actions from another action.
Why don't you just use ViewModel containing all data needed for your dashboard and then create several partial views, feed them with data from ViewModel?
I have also an dashboard in my project, I did it this way and I am happy with the solution. I have DashboardController and its Index action just takes ViewModel (which aggregates all necesary data) and sends it to View. In that View, I have several partial views (each of them needs its own model) and I pass them data from my ViewModel. Inside that partial view are then references to specific actions (like CRUD for specific models).
When you do it this way, you can easily create partial views for specific models (Task, Project, User .. in my case). And then just use them as a pieces in your final layout. My ViewModel contains collections of Tasks, Projects and Users which I then simply pass to matching partial views.

Resources