Order of including Helpers in ViewControllers - ruby

In rails app, how are helpers included in viewcontrollers, and what's the order of includion(which helper is included first, second, so on)?
Also, are all helpers available to views? If so, does the inclusion order same as the view's corresponding controller?

Any controller can have a corresponding helper.
For example if you have a controller named Question (questions_controller.rb), it can have a helper named questions_helper.rb. The question helper is accesible only to the views corresponding to the question controller.
Besides you will have an Application helper (application_helper.rb) which is accessible to all the views in the application.
All helpers are not available to views by default. But you can include other helpers in a helper like this:
module MyHelper
include QuestionsHelper
end
You can also use a helper inside a controller like:
class MyController < ApplicationController
include MyHelper
def my_method
...
end
end

Related

In which Class are these 'blade functions' startSection() and stopSection()?

When you open the cached view inside storage/framework/views/, there are rendered blade views and I can't find these functions:
$__env->startSection('content');
and
$__env->stopSection();
It's probably made with call_user_func() so you can't get to it just by clicking Ctrl+Click, this needs to be answered by someone who really knows the guts of Laravel :)
You can find these methods in the traits used on the Illuminate\View\Factory class.
https://github.com/laravel/framework/blob/5.8/src/Illuminate/View/Factory.php#L17-L23
This specific method is actually on the Illuminate\Views\Concerns\ManagesLayouts trait.
https://github.com/laravel/framework/blob/5.8/src/Illuminate/View/Concerns/ManagesLayouts.php
Also, in the constructor of that class you will see that $__env is shared with the view.
https://github.com/laravel/framework/blob/5.8/src/Illuminate/View/Factory.php#L99

Find controller name inside view in Padrino

How can I access the controller name inside a partial or a view thats rendered by the controller.
I want to create a dynamic template that changes depending on the controller rendering it, whats the best way to do it? I would prefer to access the controller inside the view rather than setting a new variable.
There are two methods to detect the entry point in Padrino: request.controller and request.action.
controller :foo do
get :bar do
[request.controller, request.action].inspect
end
end
=> ["foo", :bar]

Show taxons instead of products at root url?

just loosing ideas about proper way to display taxons instead of products in body of spree_application layout.
The spree/home/index.html.erb template renders spree/shared/_products but I would like to render spree/taxons/show instead, for each taxonomy.
No wonder, spree/home/index.html.erb miss taxonomies context so render :template => 'spree/taxons/show' would know nothing about #taxon .
Any idea how simply display taxons instead of products at the homepage, using spree/taxons/show view preferably ?
You should override the spree/home/index.html.erb template in your application's views directory if you want to override the content of this page. As for preparing the content, I would recommend writing a decorator for the HomeController and overriding the index action, like this:
class HomeController < Spree::StoreController
def index
# code goes here
end
end

Globally accessible attribute in UIViewController instances

In my RubyMotion app I would like to have a attribute, called access_token, accessible in every UIViewController instance.
All my controllers are either a subclass of TableController or AppController.
I tried creating an attr_accessor for TableController and AppController, though the problem is that assigning a new value will not be set for TableController or AppController at the same time.
How could I achieve this?
Make TableController and AppController subclasses of a Controller class, and add the attribute in it.
I personally use class variables for things like this, since iOS apps are not multi-user. Taking advantage of the fact that inherited classes share class variables, ##access_token will have the same value for all of your UIViewController subclasses (or your own subclass if you prefer).
I have something similar to this:
# Reopen and extend
class UIViewController # Actually I prefer UIViewController.class_eval do
##access_token = nil # This will have the same value for all UIViewController children
def self.access_token=(value)
##access_token = value
end
def self.access_token
##access_token
end
end
In reality, I would build a class that holds properties including and in addition to the token.

Rendering a partial in the parent directory in Ramaze

How do I render a partial that's in a different directory (in my case, the parent) than the current view?
<%=render_partial :sidebar%> #looks in the current dir and works as expected
<%=render_partial "/view/sidebar"%> #doesn't work!
Thanks!
You have to specify the right controller that is responsible for the right view:
TheRightController.render_partial :sidebar
If you don't specify the controller class, render_* works for the current action (controller) only, except render_full that does real internal HTTP request.
So, the answer is: If you need shared templates, just create special controller, i.e. called Shared, without any action methods inside, just with many templates in an appropriate view folder and call Shared.render_partial.
Shared.render_partial works like internal request. It renders contents of the controller's action and even the action's method is executed. If you want to render just the view (without executing Shared's action method), use Shared.render_view instead.
Moreover, you can use the internal requesting to prepare some data in the Shared controller's method. For instance, if your sidebar consists of #articles, let's load them in the Shared's sidebar() method. You don't need to load #articles in any other controller that displays the sidebar! You only call "Shared.render_partial :sidebar" in there. This is how to build widget-like web with Ramaze :-)
I found following api in apidock.com, maybe useful for u
# Renders a collection of partials located in a view subfolder
# outside of our current controller. In this example we will be
# rendering app/views/shared/_note.r(html|xml) Inside the partial
# each element of #new_notes is available as the local var "note".
render :partial => "shared/note", :collection => #new_notes
#rebnoob may use (without view directory name, because Rails search on app/view directory)
<%= render "/sidebar" %>
instead of
<%=render_partial "/view/sidebar"%> #doesn't work!

Resources