Controller action for partials - ruby-on-rails

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

Related

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.

How do I use layouts and view scripts outside of controllers?

I'm creating a new class, not a controller, nor a model, nor a mailer, that will act as a sort of controller.
It will have a render method that will need to render an HTML view script and layout associated with this class.
How can I utilize what Rails already provides in my class to render these views?
AbstractController, specifically AbstractController::Rendering is probably what I would look at first. There's likely something you can use there.

partial view rendering

I have a partial view in my Foo folder. I want to show it on my Home/index view. I am using partial render and it is trying to locate it in temp folder. How to write Renderpartial to render foo\partial view ?
regards,
Asif hameed
To get it to render just specify the path in RenderPartial like this:
<%Html.RenderPartial("~/Areas/FooArea/Views/Foo.ascx");%>
Obviously replace my example path with the path to your actual Foo partial view.
If you want to call an action that returns a partial view on another controller (foo) try using Html.RenderAction. It will allow you to pass in an action and a controller.
This post has a decent description of the differences between RenderPartial/RenderAction and when to use each one: http://www.arrangeactassert.com/when-to-use-html-renderpartial-and-html-renderaction-in-asp-net-mvc-razor-views/

rails render_to_string partial in model

How can i render partial to string and assign content to my model attribute?
I now that is not by MVC convetion, but it's easier to predefine some content then
assign same thing from controller to controller
THanks
you should create a helper for that.
A helper is here to provide your views and controllers with those kind of things. It allows your controller to stay thin. And your model to stay a model.
I once included a render_as_html into a model. And it was ugly.

can I have a controller associated with a partial?

In rails, page templates have their own controller which is called before a page is rendered (I think?).
Likewise, can I have a controller associated with a partial that is called before the partial is rendered?
I know you can pass local variable into a partial, but I want to run a good few lines of code to assign those local variables, which if I wasn't using a partial I would have put in the controller.
This code that you want to run should be in a helper that can be called from the controller, or from the partial itself.

Resources