Setting layout for rails forem gem - ruby-on-rails

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

Related

How to change layout to only one method in spree

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

Link_to for static pages

I have made a forum-based website, I want to add a few links to my footer like "more about badges" or "how to ask questions". Well, they are static pages so I really don't want to go through controller to go to those pages. For example, I'd like to view views/static_pages/badges.html.erb using link_to or other possible tag.
How can I go directly to some page in my views?
For static pages you should check out high_voltage gem. Once it's installed, it'll make a folder pages inside your view directory and it'll generate a named route method of page_path. For example, if you create a static page pages/static then you can refer it to by page_path('static') in your views.
EDIT:
If you don't want to use any gem then I'll suggest checking out this question
We do it like this:
#config/routes.rb
resources :pages, only: [:show] #-> sends params[:id] as /page_name
#app/controllers/pages_controller.rb
Class PagesController < ApplicationController
def show
render "pages/#{params[:id]}"
end
end
This will allow you to call the following:
= link_to pages_path("badges") # -> domain.com:3000/pages/badges

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

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