HAML and Rails3 controller-specific layout not rendering - ruby-on-rails-3

I have a Users controller and a layout called 'users.html.haml'. The problem I'm having is that Rails does not seem to find the users layout automatically. I have to tell every action in the Users controller to render the layout. If I don't tell the action which layout to use, it renders no layout at all.
Currently, the layout renders only if the controller action has this line:
render :layout=>'users.html.haml'
Any ideas? Thanks in advance.
UPDATE:
I'm an idiot. I overwrote the "initialize" method in application controller, and it was causing all layouts to be loaded improperly unless I specifically told the action which layout to use. Didn't have anything to do with haml after all. Thanks for all your answers.

You havev the users.html.haml in your
app/views/layouts
directory right? And not your app/views/users directory?

Try using this:
render :layout=>'users'
NOTE: I have removed the .haml.html part, because that isn't necessary
Other this you can do is to have the layout in top of the controller, like
layout 'users'
Edit: and like #Noli said, your layouts should be in app/views/layouts path

Related

asp.net core pass model to view from controller changed view layout

I'm building website using asp.net core 3.1 following tutorials, I'm working on bootstrap template, now I encountered strange problem that when I pass a strongly typed model to the view the layout of bootstrap not working, but if request the view without sending model it work...
Searching in google doesn't give a result.
Please enter the script and css with the view rendersection you are using and add the bootsrap links there
#Rendersection("Scripts",false) and #Rendersection("Style" ,false) include in layout after go to view add up #section Style ( bootsrap css link ) and down view add section script
I figured out what was the problem, It's was in svg-icons calls, it was starts with "~/svg-icons/" so I replaced it with "../svg-icons/", so the problem solved.

Render layout without a controller

I'm writing a Rails 3.2 app with backbone, and since I only need rails to render one page, I have no need for a controller to back the index page.
Is there a way to render the layout (application.html.erb) without a controller? I imagine it would be a configuration in the routes.rb file?
My first thought was to move it to index.html in the /public directory, but I need to take advantage of erb for javascript includes and CSRF helpers, etc.
I get that you don't need the controller to do anything, but Rails is "opinionated" software; it expects a controller and a view, because that is the way it was designed, and trying to work around that is going to give you a lot of trouble.
Just
create an empty controller class in /app/controllers/main_controller.rb
create an empty view file /app/views/main/index.html.erb
set up a route like :root => 'main#index'
Easy peasy.

How do I properly setup a custom page layout - RefineryCMS 2.0.6

I have followed the instructions found on the Guides for building a custom layout:
NOTE: The portfolio is an engine of my own, not the refinerycms-portfolio.
config.layout_template_whitelist = ["application","portfolio"]
config.use_layout_templates = true
Created a portfolio.html.erb under app/views/layouts and copied everything from the application.html.erb except for the <header> section: I do not want the menu and logo shown in this layout, but all the rest
I can now see the layouts in the back end.
If I choose my portfolio page and press preview, the layout renders without the menu
However, if I go directly to /portfolios, the 'application' layout renders and not the 'portfolio'
Any ideas please?
Thank you...
Well, it seems that the namespacing introduced in the latest RefineryCMS versions prevents the layout to be picked up automatically, thus you need to manually instruct the Controller to pick up the layout in question. To this case I had to add:
render :layout => 'layouts/portfolio'
in my portfolios#index action.
Hope this helps...

ruby on rails 3, render multiple views in one view

I have a problem and I dont know how to solve it.
I have a user that log in a web site, and I identify them by session[:user_id] and user has a status page that is defined in user_controller and in status view.
So, I would like to make one page for admin, to see all the statuses from all users on one page, using already written controller and view.
Is that possible?
I could rewrite some of the code, so that I could call with params, like ?user_id=70 and then set session[:user_id]=params[:user_id], but it would be painful if I will have to rewrite whole statuses, beside i would have to maintain same code on 2 different places.
Thank you.
Dorijan
If you need more functionality in your controller, you can add more actions to it. You can also do that in a resourcefull way.
On the other hand it usually is best practice to keep controllers thin.
See: ActionController
In order to make your views reusable, you should use partials.
You could make a _user_status partial.html.erb and render a single partial for a user render all of them for an admin.
Checkout: Layouts and Rendering in Rails

Rails 3, menu in layout

i have some controllers, i want to put categories names into layout, like menu, what i could see in all pages. Is there a easy sollution to do this?
Use : render :layout => 'yourcustomlayout'
in your specific action.
Use general layout for whole application. You can specify one layout for an application and even override it wherever required.
See here for details and near exact example for your case.