Find controller name inside view in Padrino - 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]

Related

Order of including Helpers in ViewControllers

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

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

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!

Rails3: one controller many partials for form data submission

I am not sure if this is the best approach but I have a controller that originally I intended to control a show index that renders many partials on it (a header partial and then, has some if else magic to render different partials based on the step the user is in in filling out a form... a form has many sections across several pages). I think ultimately ajax is the way to go but I am not even to that point yet. I am not sure this is the right way to do it, so I guess that is what I am asking... is the many different partials to one controller the way ? or does each "page" of form data have to be broken out into its own controller? allowing the user to fill out form (check boxes, comment section) and click "next" passing the model of the data they are filling out along the way and saving that model in each next?
U may not need several controllers, but 1 controller with some actions may be a good start. =)
Then each action should load only the partial it needs. like u can give the action name to the partial, making easy to know which partial to render.
Or maybe u can try to use wicked.
There is a railscasts for it.
Well, you could use a method to decide which partial to render.
Use this example or do some meta programming.
class YourController < ApplicationController
def index
render :partial => partial_selector(param)
end
private
def partial_selector param
#logic to decide what partial do render
#returns the partial name
end
end

Ruby on Rails: load different partial with radio buttons

I want to load different partials with generated content into a form with ajax.
So how do i make this work?
For example, i have a form with the radio buttons "fruits" and "vegetablles", so when i select one of them, the corresponding partial should be loaded directly into the template dynamically with the right content.
Its a simple form created with scaffolding (edit, show, destroy)
should be prototype normally?
All you do is :
In your controller, you should have the following methods:
def fruits
#myfruit = Fruit.new # what you want!
respond_to do |format|
format.js
end
end
Then, you should have your fruit.js.erb corresponding to that action (suppose you have jQuery here) :
$("#fruits-holder").replaceWith('<%= escape_javascript(render #myfruit) %>');
You then need a partial view like _fruit.html.erb that contains the html you want
As Codeglot stated, if you want more precise answer, post more information about your problem. I cannot be more specific right now.

Resources