Rails 7 : layouts - ruby-on-rails

I read on https://learn.co/lessons/rails-layouts-and-templates
Rails uses a simple convention to find the correct layout for your request. If you have a controller called ProductsController, Rails will see whether there is a layout for that controller at layouts/products.html.erb. If it can't find a layout specific to your controller, it'll use the default layout at app/views/layouts/application.html.erb.
I have not specific layouts for my controller but i add <h1>hello</h1> before or after <%= yield %>
i dont see hello.
What is the problem?

Related

Creating targets based on routes

I have to solve an issue.
I'm trying to manage an advertising skin on my website using this gem:
Google_DFP
Now I'm using the standard invocation call on the homepage:
<%= dfp_tag :skin %>
I want to add the skin to the whole website, so in the application layout, using the targeting options to select the pages where the skin appear
<%= dfp_tag :leaderboard, { :page => 'Home' } %>
I need to set a target for some pages, for example the content type and the the section of my contents. It the right way to use a variable in the controller?
Your idea sounds perfectly legitimate. You can set an instance variable in each controller action, or perhaps in a before_filter to cover multiple controller actions at once. After the views are compiled from the controller actions they are then yielded into the layout and any instance variables defined in the controller will be referenced when compiling the layout. So, yeah, supply any action-specific behavior via instance variables.

rails pass instance variable to layout or local variable

I have a navigation bar (in fact two) and I use a before action in some controllers to fill it's dynamic data (the second bar may not exist in some), I've seen a lot of complains about not passing a lot of instance variables to views, and all of them suggested passing locals in render. I've been wondering using a instance variable to generate these stuff in the main layout is a good idea or not, and if it's not, how should I do this, render seems to overwrite the default view and I use the data in the main layout only
I not sure that I understand well your question. But for some of my menus I use something like that in my layouts:
<%= yield(:menu_top) %>
and I use
content_for :menu_top
to generate content in this area.
For exemple:
<%= content_for :menu_top do %>
<li>my specific content or var</li>
<% end %>
Here is the rails guide for content_for: link

RoR, where do I put generally used templates?

I am new to Ruby on Rails. I know that for each controller you have a specific views folder that holds all of it's views. I also know there is the layout folder for the layouts.
But what if I have a bit of a template that keeps popping up in many templates across the system but it's not a footer or header or otherwise layout related.
I want to refer to it using the <%= render.... %> command but where should I put this template?
Is there a generally agreed upon location?
Can I just create a directory under views and store it there?
Rails will automatically look in 'views/application' and in the folder that contains the current parent view.
That said, you can place partials anywhere you like, and refer to them like so:
<%= render 'foo/bar' %>
As #apneadiving suggests, 'shared' is a good name for the folder.
<%= render 'shared/bar' %>

Rails 3.2.1 - Find out template file name being rendered from layout

From the view file I know I can call access the #virtual_path instance variable to get the name of the template file being rendered. When I access this variable from the layout, as expected, I get the layout file name. Is there a way to access the name of the main view template file being rendered from the layout?
Is there a difference between layout and templates?
Maybe one of these thinks is useful for you:
#app_name
#app_path
<%= self._template.identifier %>

How do you handle widgets in rails?

StackOverflow, for example, has a user's reputation displayed up top. Clearly, this is grabbed from the database, and it's displayed on every page. Of course, this isn't in every controller action on every page, because that would be incredibly redundant.
How do you handle this kind of situation in rails? The only way I can think of it is to use before_filters to pass the models into the page, but that just seems like abuse of that feature. There seems to be the cells gem that does what I want, but I'd imagine this is a common problem and there must be a simple solution for it in rails without having to resort to plugins or gems.
What you are looking for is the layout. In rails this is where you define headers, footers, and sidebars that frame your site. Look for app/views/layouts/application.html.erb in your generated rails code. Towards the bottom you will see:
<body>
<%= yield %>
</body>
The yield is where rest of the app gets invoked. Everything before and after the yield will appear on every page. So, using your example, you might query the database and set the instance variable #reputation in the application controller:
#reputation = User.find( current_user ).reputation
then display it in the layout like this:
<body>
<%= #reputation %>
<%= yield %>
</body>
This is covered thoroughly in the book "Agile Web Development With Rails". If you are going to develop in Rails I recommend getting the latest edition.
I would just make a partial with the widget in it and render it in the layout(s) where you want it to appear. Let it do whatever it needs to do, eg connect to the db, run some js to connect to an external site, etc.
If you're concerned about optimisation then deal with it when it becomes a problem.
I guess, you can put the code you need into a view helper. And then render some partial, like it was said before, in the layouts where you want it to appear, calling helper's function.
Look here:
Rails view helpers in helper file

Resources