rails pass instance variable to layout or local variable - ruby-on-rails

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

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.

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' %>

in views with layouts, why do we use "provide" and "yield" instead of passing instant variable?

Am working on Rails Tutorials and I reached the layout part.
It is instructed to use "provide" in the views, and use "yield" in the layout file.
why don't we just use instant variable instead
in code:
view about:
<% #title = 'about' %>
application layout
<title>Website Name | <%= #title %></title>
The book use instead a more complicated syntax, and am sure for a good reason
view about:
<% provide(:title, 'Help') %>
application layout
<title>Website Name | <%= yield(:title) %></title>
Tried both, and both worked fine. But I don't understand why not use the simpler instant variables way?
Instead of provide you can use content_for and pass a block of code. Then yield will be able to execute this block of code - it doesn’t just print a variable.
This is useful because in more complex views you will use another concept know as "helpers". Keep going with the good work and I'm sure things will become clear in the future.
you need to use provide or content_for when you are not able to determine the value of a variable beforehand.
example:
render layout
yield title <-- not yet defined
yield main template
provide title <-- defined here

Keeping track of which view a partial is called from

I call the same partial from multiple views in Rails, the show page and the edit page. How can I keep track of which view the partial is called from? More specifically, I would like to adapt the partial slightly depending on which page it is rendered from. I have tried to request the uri using url_for(:only_path => true) and if-else statements to determine if the partial is rendered on the show or edit page, but this is a bit cumbersome. Is there a better approach?
you can try to use current_page? helper, i.e. if current_page?(root_path)
We put this in our application.html.erb template which shows the current controller, view and more debug information:
<%= debug(params) if Rails.env.development? %>
I think we got this from the Rails site:
http://guides.rubyonrails.org/debugging_rails_applications.html

Conditional content in html.erb

i have a footer as the last element in my application.html.erb page, however for one page in the whole site, i do not want the footer to appear.
Whats the best way to handle this? every solution i come up with is wet (not dry)
Why don't you create a specific layout for this single page? It should be more maintainable than any extra logic.
DRY is not a goal to reach, it's like a conditional warning and like all warnings, you can ignore them if it makes sense.
If you really insist, do this:
<% unless defined? #no_footer %>
your html here
<% end %>
So the footer will disappear only if you set the instance variable in your controller:
#no_footer = true
Another way could be to check params action/controller and put the logic in a helper method.

Resources