use a model in my rails header partial - ruby-on-rails

I have contest model which I want to use in my _header partial.
The header partial is located in an elements folder but there is no elements model.
I want my header to display all the contests in the menu.
How can I best handle this? Create a elements model or move the _header partial to my Contest folder and add it there to the controller?

Simply spoken a model is mostly holding some data which is persited in the database. To insert content in the header while rendering specific views you can stick with content_for. Basic idea: while rendering a view you can pass your object / model via locals from the controller into your view template which passes it further via content_for to render the header specifically.

Related

Controller action for partials

I was trying to create an action for one of the partials in my rails application, but wondered how to name it. Does it have the underscore at the beginning? Like:
def _my_partial
end
or no underscore? Not mentioned in the controllers guide
A partial is a piece of view which is embedded in the main view or within another partial. The purpose of a partial is typically for re-use or to making view code cleaner.
If you need to use an action to render a partial, then it's probably better to either:
make your partial into a view, or
create a view to embed your partial.
See Rails 4 - passing variable to partial

Using MVC Partial View's own model instead of its parent's

I need to populate a list box in a partial view, using ASP.NET MVC4.
Can Partial View have its own #model, as opposed to taking a model from its parent as described here?
I can populate my dropdown box using a separate AJAX call to another MVC controller (i.e. not parent page/url) as discussed here, but the resulting syntax is a bit more clumsy; furthermore, an additional endpoint is exposed to the outside world.
Yes - you can call partial view with any model you like. There is no requirement that data somehow comes from current model.
#Html.Partial("PartialView1", new MyOtherModel(42))

Changing some of static content of shared partial view depending on main view

I have several shared razor partial views in my application to show some details of products on different pages. Now i need to change some of links inside partial views depending on what main view rendered that partial.
For example:
If partial view is rendered inside "index.cshtml", one of the links in partial view should be:
site1 called from index.cshtml
and if it's inside "insert.cshtml" then link need to be
other site that's not 1 and it's called from insert.cshtml
something like:
#if (something.parentview = "index.cshtml")
{ site1 called from index.cshtml}
else {
other site that's not 1 and it's called from insert.cshtml
}
Is there a way to do this?
When you call the Shared PartialView you know where are you.
Is far more easy to pass a parameter to your partial at the time of rendering that rely in "detect" inside of which view is rendered. Imagine... you have 3 levels of nesting... things get more complicated. Imagine if you change routing or view names?
You can use the ViewBag to pass the parameter: in your view you can set something to check inside the Partial. Not controller code is needed.
A more robust approach is pass a ViewModel to your Partial, but if the only data you need to pass is just a parameter, the ViewBag is simpler.

Same model instance for two partial views (ajax)

Here's what happens:
Partial views A and B show the same model
View A changes the data via Ajax (using Ajax.BeginForm)
View A re-renders itself (controller gets the updated model from the database)
Now we have to re-render View B as well, because the data has been changed, right?
We get the updated model from the database again and re-render View B
How do I prevent re-querying the database? Maybe cache the model-instance in the Session?
What's the "right" way to do this?
<!--works great when the page is rendered
via postback but what about Ajax?--!>
<div>
#Html.Partial(#ViewA", MyModelInstance)
#Html.Partial(#ViewB", MyModelInstance)
</div>
Can you create a new partial View C that contains both A and B. Whenever the model changes you have to invoke the action that returns the View C.
UPDATE:
The other simple solution I see is when View A updates the model instead of re-rendering the view, get the updated model through AJAX and the update HTML portions through javascript. If you are using jquery you can use the template plugin to update the html quite easily.
By this way you can avoid making one more unnecessary request to update the other View B.

ASP.Net MVC reusable form as RenderAction or RenderPartial

I'm looking for a best practice for embedding a form on multiple pages as a partial view.
I have a contact form I'm looking to embed on multiple pages on a site. Usually, the form would be on a contact page and the contact model could be the model for the view and use data annotations for validation. However, the view is already strongly typed.
How can I create a reusable form in a partial view and embed it on the page? I'm using N2 on the site, so the pages have to already have a strongly-typed model, but I would be open to extending those objects.
Personally, I recommend using for Html.RenderAction() for cross-cutting concerns such as these.
The handler for your contact form is going to need to exist independently of the page your are currently viewing so you are left with 3 options:
Manually add it to the response of
the current action
Manually add it to the response of
the current controller by way of a
base controller that modifies the
ViewState or ViewModel
Call the RenderAction()
HtmlHelper inside of the current view
Of these 3 options, while the third is technically more costly than 1 and 2 (because it initiates a brand new request), it is also the most maintanaible solution. By calling RenderAction() you have the advantage of being able to completely isolate your contact form from the rest of the view and thus you won't have to worry about hacking it into the current controller responses.
Use RenderPartial if data model for partial view is already in main view's model, in other case use RenderAction (then the action of the partial view will create its view model itself).

Resources