Rails. Different application layouts for different parts of the site. - ruby-on-rails

I'm building a web site with a part that users can browse and administrative panel. How can use different application layouts for different parts of the site? Is it possible?

You can use layout method to render different layout for different controllers.
class MyController < ApplicationController
layout :admin_layout
private
def admin_layout
# Check if logged in, because current_user could be nil.
if logged_in? and current_user.is_able_to('siteadmin')
"admin"
else
"application"
end
end
end

You can use seperate views, for example if your run rails g controller home index you get a home_controller.rb and in the views folder you get a home folder with an index view. In your controller you can put your logic there and present it in the view.
Check this link seems to be good for beginners

Related

Rails - Edit data on front-end like in Linkedin's profiles pages

Is it possible, in a RoR web application, to allow users to edit elements which are in a Show page?
The target would be something like on Linkedin, when you edit your own profile page (moving your mouse over a field gives you the ability to edit it). How do they manage to do that? Is it on a Show page or an Edit page? What kind of front-end technology do we need?
I'm not a big fan of the traditional 'Edit.html' vs 'Show.html'.
Many thanks! :)
Yes you can use your show page as an edit page. You could set up your controller to something comparable to this:
your_controller.rb
class YourController < Application Controller
before_filter :show_user
def show
render :edit
end
def edit; end
private
def show_user
#user = current_user
end
end
Also don't forget your 'update' method within this controller and params that you are passing. Then you can create your edit.html view that acts as a show page, but allows edits to take place. As far as editing comparable to LinkedIn, you can use the 'best_in_place' gem for inline editing. Found here: https://github.com/bernat/best_in_place

rails overriding/scoping views for multi tenancy app

I'm wondering how I can scope my views. I want to have custom themes depending on the organization, I can use render directly on the show/index actions... and that works, but I would have to override a lot of actions on my application. I would prefer to do it on the controller level and tried doing it with prepend_view_path but it didn't except the variable as undefined.
class EditionsController < ApplicationController
helper_method :current_organization
prepend_view_path "app/views/#{current_organization.slug}/editions" #doesn't work
def show
#edition = Edition.find(params[:edition_id])
#page = #edition.pages.first
render template: "#{current_organization.slug}/editions/show" #works
end
Any ideas?
Also tried: (with same error)
append_view_path(File.join(Rails.root, "app/views/#{current_organization.slug}"))
custom themes depending on the organization
Surely it would make more sense to define custom layouts & CSS rather than entirely different view sets for each company?
--
I would personally do this:
#app/layouts/application.html.erb
<%= stylesheet_link_tag "application", controller_name ... %>
This will give me the ability to style the different pages as per the theme. Obviously a constriction on what you want, but I hope it shows how you could modularize your controllers etc
--
If you wanted to create different "themes" (IE having a completely different view structure per tenant), you'll want to use the prepend_view_path helper, as described here:
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
prepend_view_path("views/#{current_organization.slug}")
end
Try to remove editions in prepend_view_path
prepend_view_path "app/views/#{current_organization.slug}"
Make sure what the way was added. If it doesn't add before_filter

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.

Asp.net MasterPage equivalent in Ruby on Rails, Trying to define a site wide layout

Asp.net WebForms and MVC has a concept of Masterpages which make it easy to define a one time layout for all the page of your site. In Rails I'm struggling to find an equivalent usage pattern or feature.
From what I've read it's really easy to define a layout in every action with:
layout: 'viewname'
Now that seemed pretty ceremonial to include in every controller so I added:
layout: 'application'
in the base ApplicationController.
So far this is working ok unless I have a more specific layout in the view pages. Is this common technique for getting a consistent style in your Rails application?
Imagine a simplified blog where we have a controller called PostsController which has two actions: index and show
The index action is called when the user hits http://yourwebsite.com/posts - this action displays all of the available blog posts.
The show action is called when a user gets a specific blog article - i.e. http://yourwebsite.com/posts/article-about-something-interesting
Let's say that we want the index page to have a two column layout and we want the show page for each blog-article to have a three column layout. To achieve this, we would simply define two separate layouts (in app/views/layouts folder) - we'll call the two column layout "application" and we'll call the three-column layout "alternate".
In order to get the index page to use the two-column layout and the show page to use the three-column layout, we could just do the following in our controller:
class PostsController < ApplicationController
def index
#posts = Post.all
render :layout => "application"
end
def show
#post = Post.find(params[:id])
render :layout => "alternate"
end
end
If we want all actions to use the same layout, we can just do this:
class PostsController < ApplicationController
layout "application"
def index
#posts = Post.all
end
def show
#post = Post.find(params[:id])
end
end
Finally, if we do not specify which layout we want to use, then Rails will by default, render any layout which has the same name as the resource we are displaying. So in our example, where our resources are called "Posts", if we define a third layout called posts.html.erb (in app/views/layouts) then Rails will automatically use that layout when the user renders any of the actions in the PostsController - providing of course that we have not explicitly asked Rails to render another layout....
Hope it helps,
This PDF book excerpt from Rails for .Net Developers has a pretty good explanation of Rails layouts, along with a comparison to ASP.Net MasterPages. Since it seems to work pretty well, it's probably used fairly often, at least by developers familiar with the master page concept.

Rails Sub-controllers?

I'm pretty new to Rails and have an issue which I can't quite get my
head around as to the architecturally 'correct' way of doing it.
Problem relates to what I kinda call sub-controllers. The scenario is
this:
I have a series of pages, on which is a panel of some form containing
some information (think the user panel on gitHub top right).
So, in my app, I have controllers that generate the data for the pages
and render out the responses which is fine, but when it comes to this
panel, it seems to me that you would want some sort of controller action
dedicated to generating this panel and it's view.
Question is, how do you go about doing this? How do I render a 'sub
controller' from within a view?
I would put the logic in a helper or a module. (http://api.rubyonrails.org/classes/ActionController/Helpers/ClassMethods.html)
Then render partials where you want these things displayed. (http://api.rubyonrails.org/classes/ActionView/Partials.html)
Like Herman said, if it's logic that you need generated after the controller hands off to the view (ie, the Pages controller generates a page view, but you want a customized panel) then put it in a helper. Or, call a separate method in your Pages controller before handing off to the view. Or, if it's a lot of logic, create a Module and stick it in your /lib folder. So you could have a whole Panel module with methods that generate different parts of your Panel and which are called by your controller. But if you want to call these methods from within the view, then you should use a helper instead.
I dont think a module is what is required here, modules are required for shared behaviour across a small subset of your classes.
What I think is required here is the understanding of the inheritance of ApplicationController and also layouts
so, for example, my layout might look like:
<html>
<head><title>Foo</title></head>
<body>
<%= render :partial => (current_user ? "/shared/user_widget_bar" : "/shared/login_bar") %>
<%= yield %>
</body>
</html>
Any code that i want to use for it would go in my ApplicationController since it would be shared across the majority of my app:
before_filter :generate_user_widget
def generate_user_widget
if current_user
#avatar = ...
#unread_messages = ...
end
end
I understand that it might be cleaner for it to belong in a separate controller BUT honestly, unless the code is huge, it doesn't matter and can even still be put inside a module which is then included by ActionController. However it does need to be inside ApplicationController if you consider the scope of it.
If there are more related pages, say for example, you have a Rails app that manages multiple sites and you want shared behaviour across a particular site, try creating a parent controller which has no actions and only private methods, any controllers that need to have access to those methods can inherit off it. That way you can apply before filters to all controllers which inherit off it, saving you the pain of forgetting to add one in your non-parent controllers.
e.g:
class SiteA::SiteAParentController < ApplicationController
before_filter :generate_user_widget
...
end
class SiteA::ProductController < SiteA::SiteAParentController
def index
...
end
end
well, if you really need to call a controller action from the view, you can use components. They were part of the framework, now they only exist as plugins. One such plugin that seems to be well maintained is here: http://github.com/cainlevy/components/tree/master
from its docs:
== Usage
Note that these examples are very simplistic and would be better implemented using Rails partials.
=== Generator
Running script/generator users details will create a UsersComponent with a "details" view. You might then flesh out
the templates like this:
class UsersComponent < Components::Base
def details(user_or_id)
#user = user_or_id.is_a?(User) ? user_or_id : User.find(user_or_id)
render
end
end
=== From ActionController
class UsersController < ApplicationController
def show
return :text => component("users/detail", params[:id])
end
end
=== From ActionView
<%= component "users/detail", #user %>

Resources