I'm working with an existing Refinery CMS app for a client that has many controllers in many different places. If you are n00b to Refinery CMS, you can nest entire rails apps INSIDE the vender folder and they act like plugins. Its complex how it works and even worse a lot of the models/controllers are embedded in the refinery gem so a controller might exist but theres not file for it.
I wanted to extend a controller by following this example:
http://refinerycms.com/edge-guides/extending-controllers-and-models-with-decorators
which I did but my code was not firing. I did actually fix this so my problem is solved but in the future it would be useful to know what controller called this view I have. The view is tucked away in the gem HOWEVER a partial that it references was already overridden so I could throw something like:
<%= raise self.class.to_yaml %>
The problem with this I get the following error:
can't dump anonymous class: #<Class:0x000000061f5850>
Which isn't very helpful.
My question is this: How can I output the class name of the controller that calls any given view/partial ?
Thanks!
You can use params[:controller]
And params[:action] for current action
Related
I was hired some days ago to update a Rails 4 app. In general the rspecs and the code look good however in the top of some controller a found this line:
delegate :edit_app_path, :new_app_payment_path, to: :view_context
Searching in the net, I found that the line is a way to load methods from the helpers inside a controller through a new instance of ActionView::Base class. I mean, is a way to do it instead of the classic:
include MyHelper
in the controller. My question is: is this really a good practice? is faster? AFAIK, view_context will load a new class with all the helpers and all the context of the view instead of one helper if I use the classic "include MyHelper". By the way :edit_app_path and :new_app_payment_path methods are in the same helper.
Should I remove the line?
Using view_context allows the controller to be blissfully ignorant of where the path is defined. If the helper file structure is refactored in future, the controller will continue humming along without requiring change.
Performance wise, I doubt the impact will be significant since all the code will have been loaded. Rails (and the Ruby standard library) creates new objects all the time.
When I try to render a partial from a helper, it fails with this (condensed) error message:
Missing partial /_cube_icon with [...]. Searched in:
Note that the list of searched directories is empty!
In contrast, when using render in a view, it knows where to look:
Searched in: * "/Users/Lars/GitHub/algdb/app/views"
In the helper code, I use ActionController::Base.helpers.render(). Should I use some other render function? How do I tell it where to look for partials? Could I have set up the project wrong somehow?
This is Rails 4.2.4 ยท Ruby 2.3.1
OK, I figured it out:
I was calling render from code in the helper directory, but not from a function in a standard SomethingHelper module.
When following that convention, things started working.
Partial files normally reside within the app/views directory and are not located in the helpers directory in Rails as you can see from the error message you are receiving. To solve your problem, I would move the _cube_icon file into the app/views directory and organize your code there. I recommend reading this section of the Rails documentation for views Layouts and Rendering
Additionally, it sounds like you may be new to rails so I would take a look at the conventions that rails offers. Rails, as you may or may not know, is an opinionated framework which means certain things need to go in certain locations in order for it to work. Here is another resource on just the view part of Rail's MVC framework Action View Overview. Hope this helps.
---Updated-----
If you really want to render a partial from a helper file, there are a few ways to do so but the best one to use is outlined below:
In app/helpers
module MyHelper
def custom_render
concat(render(:partial => 'cube_icon'))
end
end
Here are some links from stackoverflow to help you out.
Rendering a partial from helper #1
Rendering from a partial from herlper #2
Using concat rails
I recently came across a tricky situation related to rails view helpers.
The situation is like follows-
I am having a controller as Feature1::Feature1.1::Feature1.1.1Controller.
The Feature1.1 also includes other controllers like Feature1.1.2Controller, Feature1.1.3Controller...
So ofcourse related view helpers in folder app/helpers/feature1/feature1.1/...
Now the real problem I am facing is that a few helpers for feature1.1 includes the same method name method1 with related definition.
I was wondering how rails identifies all these helpers as I am noticing that the method1 i.e. being called in a view for the controller feature1.1.1 is using the definition of the method1 i.e. written for the controller feature1.1.2.
So does rails consider all helper modules defined in one folder as one?
In a view feature1/feature1.1/feature1.1.1/index I am making a method call for method1.
I am using rails3
It depends a little bit on your Rails version. With eralier Rails versions, Rails did only include application_helper.rb and <controler_name>_helper.rb.
Additional helper modules can be included via helper :helper_name1, :helper_name2, ... within your controller.
With later Rails verions (4.2.? and up, maybe previous versions too), Rails includes all helpers within your helper folder at once. You can set config.action_controller.include_all_helpers = false within application.rb and you will fall back to the old behaviour.
This makes the helper only available within your views. If you want to use a helper within your controller you still have to include your helper with include XXXHelper.
I did some research and would like to share some additional info.
As per #slowjack2k mentioned, view helpers are included by rails as a default behavior.
But my question was about the situation of same method names across multiple helpers.
I found this article to be useful in this scenario. Though it explains the behavior for Rails 4 but I found it behaves in the same fashion for Rails 3.2.2.
I will summarize the article -
If there will be any conflict in the same names of methods in different helper modules, rails will use method from latter file (alphabetically)
What I've run into is this:
AlchemyCMS is a Rails Engine for allowing Rails applications to have a Content Management System. It also has a preview page where it can load up an iframe of the example page with the layout. The layout here is the Spree layout. I've modified Alchemy to be able to load up the spree application layout and not its default.
In doing so, it is not loading up the helper methods. I am currently receiving:
undefined local variable or method `title' for #<#<Class:0x007f8dcc359498>:0x007f8de17dd6a8>
Where title is the first helper method in the application.
I've tried 5000 different techniques to try to load in Spree's helper methods into AlchemyCMS and I just can't do it.
Does anyone know how?
Ben,
You could do so by either including Spree's helpers within your application controller or within the base Alchemy controllers.
There is an extension for alchemy and spree together, which does a similar thing here:
https://github.com/magiclabs/alchemy_spree/blob/master/app/controllers/spree/base_controller_decorator.rb
You will just want to go in the opposite direction so instead of decorating a Spree controller to add Alchemy in you would decorate Alchemy controllers to include whichever of Spree's controller helpers you need to use:
https://github.com/spree/spree/blob/master/core/app/controllers/spree/base_controller.rb
In this case you need to include the common controller helpers:
https://github.com/spree/spree/blob/master/core/lib/spree/core/controller_helpers/common.rb
EDIT:
Alchemy::BaseController.class_eval do
include Spree::Core::ControllerHelpers
include Spree::Core::ControllerHelpers::Store
helper Spree::Core::Engine.helpers
end
Let's say I created a mountable engine called 'Soho' that has a controller for 'Users'. I can go to /users/1 to see my user with ID 1.
Inside 'Soho', I have a application.html.erb for layout.
Now let's assume I want to "blend" my engine 'Soho' in an application called 'Soho_test', and I mount my engine at "/". So, in my host application 'Soho_test', I can also go at /users/1 to see my user with ID 1. This is working.
My question is : how can I do in my host application 'Soho_test' to apply the 'Soho_test' application.html.erb to the /users/1 (user profile page) instead of the one in the 'Soho' mountable engine?
Thanks!
I found how to achieve it, so I will post my answer on my own question, in case anyone else wonders. It is actually quite easy. I should have thought about this in the first place...
All you have to do is to create a folder in your /views/layouts/ with the name of your engine. So according to my question, it would be /views/layouts/soho/. In that folder, put the application.html.erb that you want to have.
You can do the same with partials and other views. Just create a folder /views/soho/.../ and put your files there. I havn't found a rake task to copy the engine views in my host application, so I wrote one.
After reading your question over a few times, I think all you are trying to do is override a layout for a given controller.
If that is the case just specify the layout to use within your controller see the section 2.2.13.1 Specifying Layouts on a per-Controller Basis within the Rails Guide for Layouts
Here's an example:
class UsersController < ApplicationController
layout "users"
#...
end