How to change layout to only one method in spree - ruby-on-rails

I am new to the spree world ( using 3.0.0 ), and i want to override the layout used for a custom product method that i added as following:
in "app/controllers/spree/api/products_controller.rb" i did:
Spree::API::ProductsController.class_eval do
def custom_view
end
end
And after modifiying the routes accordingly i was able to display to view as wished...
Then i added the layout before filter to remove the default spree layout
Spree::API::ProductsController.class_eval do
layout nil
layout spree_application, :except => :custom_view
def custom_view
end
end
But it didn't work, and the spree layout kept displaying.
How to remove the spree layout only for this custom method ?
Thanks !

Use Deface Overrides with a :remove tag for the specific targeted element.
Hope your problem gets solved by doing this Way

Related

Accessing model data from numerous nested layouts in Rails

Let's say I have a nested layout in Rails, as described in Rails Guides, where my application.html.erb file has:
...
<%= content_for?(:content) ? yield(:content) : yield %>
...
somewhere in it.
In both the application layout, and in the sub-layout, I need to access data from models.
I've found a solution from this question. I could put something like this in my ApplicationController:
class ApplicationController < ActionController::Base
before_filter :get_main_layout_stuff, :get_sub_layout_stuff
private
def get_main_layout_stuff
#cart = find_cart
end
def get_sub_layout_stuff
#categories = find_categories
end
end
and then in any controllers not using the sub layout, I can just say:
skip_before_filter :get_sub_layout_stuff
This works fine. However, if I start to have more layouts, say, a dozen, with many layers of nesting, and where maybe a layout needs specific information based on the contents of the URL, then it becomes unwieldy. I have to either list a million skip_before_filters in every controller, or I have to remember exactly which set of functions to add as a before_filter in each controller. Neither solution is very DRY, when I'm already specifying which layout I'd like in each controller.
So my question is: how can I get the right information to each layout in the hierarchy of layouts without having a crazy number of before_filters? Is there way to automatically load the required data based on the layout requested and then recursively go back to load the required data for each of parent layouts? Or maybe is there a way to have a "controller" for each layout that gets called whenever the layout is required? Or am I thinking about this problem in completely the wrong way?

Change layout for Devise controllers

I am using gem devise. Devise extends application controller and adds user managment to rails application.
When I look inside the gem I can see following line
class Devise::SessionsController < ApplicationController
I am trying to change this since I want Devise controller to inherit from my custom controller named AdminController. Reason for this is I have whole web application finished and I do not want admin part of the page to use my application layout, css, js ...
How can I dynamically change base class of controller? Or dynamically tell controller to use admin.html.erb layout instead of application.html.erb layout.
When I say "dynamicly" I mean monkey patch it, thank you.
This solved my problem, if namespace of controller is Devise use admin layout.
class ApplicationController < ActionController::Base
protect_from_forgery
layout :determine_layout
def determine_layout
module_name = self.class.to_s.split("::").first
return (module_name.eql?("Devise") ? "admin" : "application")
end
end
Devise is a rails engine. I think that the best way to make a admin section of you site is to make a rails engine. Or better still use rails_admin or activeadmin. They are both rails engines There is a railscast about rails engines
I don't know the inner works of you app, but if you add
layout "admin"
to your AdminController and add a custom admin layout to the view/layouts folder with
<%= stylesheet_link_tag 'admin' %>
<%= javascript_include_tag "admin"%>
the AdminController views will use the admin stylesheet and javascript
If you just need to change the layout, I think you should be able to do it by re-opening the controller class. At the bottom of your initializers/devise.rb (underneath the config section at the top level, you could write:
Devise::SessionsController.layout :admin
I've not tried this, but in theory it should work since layout is just a class method on ActionController.base.

Using a layout other than application.html.erb in ruby on rails application?

How could I create and use a new template instead of application.html.erb in a RoR application? What are the setting that I need to change to have another template?
Actually, all controllers will search for same-named layouts by default inside /views/layouts folder with fallback to application.html.erb. For example, UsersController will search for users.html.erb. Anyway, to use other layout add next to your controller:
class UsersController < ApplicationController
layout 'custom'
end

Setting layout for rails forem gem

I am using the Rails forem gem and I would like to use a different layout for some of the forum actions. How would I go about doing that?
If it's for some particular actions inside a controller then I would use a decorator for that. Simply put this file in app/decorators/forem/forums_controller_decorator.rb:
Forem::ForumsController.class_eval do
layout "my_special_layout", :only => :show
end
If you want it for all the actions in a specific controller then you should use an intializer:
Rails.application.config.to_prepare do
Forem::ForumsController.layout "my_special_layout"
end

Ruby on Rails - How to manage layouts in admin interface?

I have a very simple site setup using awesome_nested_set and a single table called Pages.
I would like the ability to select different layouts in the admin when creating and updating Pages. What I envisioned is a drop down on the Pages form that allowed me to select a layout/template.
The only thing I know about layouts is you are required to add them to /views/layouts/ and specify the layout at the top of the controller. I need a way to manage layouts on a per Page basis inside the app itself.
Is that even possible? If so, can you explain on a high level how that might be done so I can have a starting point?
edit
Something like this:
You can easily change the layout at render by supplying the :layout key like so:
def some_action
#... stuff
render "some_action", :layout => "custom_layout"
end
You can also set layout to a symbol in the controller definition, and the controller will run the associated method to decide what layout to choose
class UsersController < ApplicationController
layout :decide_layout
private
def decide_layout
some_boolean ? "layout1" : "layout2"
end
end
You can also replace the symbol with a proc if you don't want the method located away from the usage. Finally, you can also call #layout in an action itself.
Assuming you have files in views/layouts called something like one_column.html.erb, two_column.html.erb, etc., and an attribute called layout on you page model, you could just do:
def show
#page = Page.find(params[:id])
render :action => "show", :layout => #page.layout
end
Is that what you're looking for?

Resources