How do I use layouts and view scripts outside of controllers? - ruby-on-rails

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.

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

Calling a Controller Action from a View... Or not?

I'll keep it short: Should I call Controller2.getByCriteria(some, criteria, here) from Controller1 then set an instance variable for the view to use, or should I call it from Controller1's view using something like = render Controller2.getByCriteria(some, criteria, here)?
Generally speaking, calling one controller's action from another is a design error. It either means you have common business logic, which implies the code should be in models (or maybe lib) or you have common view logic, which implies the code should be in helpers.
So in your case, I think using a helper seems apt:
module ApplicationHelper
# ...
def getByCriteria(some, criteria, here)
# handle criteria here
# Something like:
# render :partial => 'foo'
end
# ...
end
And then simply call it from the views.
If you want a full controller/view like component sharing across your application, you can use a gem called cells. It enables you to create re-usable controller and view components.
The proper way to do this is to declare the controller method (not controller action) as a helper.
See here.

Razor Layout Page pre-set values

I'm trying to convert an old masterpage (ASP.NET Webform) into Razor layout for new project. and I just wonder how can I set some values in the Razor layout by calling few other custom function. I know I can just write them in my layout page but it seems a bit messy. What is the best approach?
The best is to have the controller action pass whatever values a view might need in the view model. Another possibility is to use a custom HTML helper which would format the values. Or include a partial: Html.Partial, or render an action: Html.Action. Yet another possibility is to include a #section. So as you can see there are many ways, which one is best would depend on your exact scenario. I can though say which is the worst: write C# code in your views. The view shouldn't set anything. It should be as dumb as possible and simply render whatever information it is thrown to it by a controller.

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.

Guidelines for calling controller methods in helper modules?

Few questions:
Is it possible to call a controller method in a helper module (e.g., application helper)?
If so, how does the helper handle the rendering of views? Ignore it?
In what instances would you want to call a controller method from a helper? Is it bad practice?
Do you have any sample code where you're calling controller methods in helper?
You generally don't call controller-methods from helpers. That is: if you mean a method that collects data and then renders a view (any other method that needs to be called should probably not be in a controller).
It is definitely bad practice and breaks MVC.
It is, however, perfectly possible to make controller-methods available in the views, a great example is for instance the current_user method.
To make a controller method available in the views, as a helper method, just do
private
def current_user
# do something sensible here
#current_user ||= session[:user]
end
helper_method :current_user
Such a method is best defined in the private section, or it could be available as an action (if you a wildcard in your routing).
Declare your methods on the corresponding controller
private
def method_name1
...
end
def method_name2
...
end
In the head of the controller declare
helper_method :method_name1, :method_name2
You may want to declare those methods under private state
And that's it, now you can use your method on a helper
Calling a controller from a helper violates the MVC pattern. IMO if you need to call a controller from a Rails view helper (like application_helper) then there is something about the design that could be improved. The intention is that helpers "help" views, and therefore only talk to models.
I'm not going to defend MVC itself here (there are a ton of references on the web), but this SO thread about calling the controller from a view should get you started.
Calling controller from view? (note, this is an ASP.NET thread, so only the high level principles are relevant).

Resources