Rails: How to Render Conditional Based Upon 'pages/home.html.erb' - ruby-on-rails

In the tbody section of application.html.erb I wanted to render for the current_user two sidebars if he is on the home page and just one sidebar for every other page.
<body>
<% if current_user.present? %>
<% if 'pages/home.html.erb' %> # Not Working As a Conditional
<div class="container">
<div class="col-md-3">
<%= render 'layouts/valuations' %>
</div>
<div class="col-md-6">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-info") %>
<% end %>
<div id="modal-holder"></div>
<%= yield %>
</div>
<div class="col-md-3">
<%= render 'layouts/sidebar' %>
</div>
</div>
<% else %>
<div class="container-fluid">
<div class="container">
<div class="col-md-9">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-info") %>
<% end %>
<div id="modal-holder"></div>
<%= yield %>
</div>
<div class="col-md-3">
<%= render 'layouts/sidebar' %>
</div>
</div>
</div>
<% end %>
<% else %>
<div class="container">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-info") %>
<% end %>
</div>
<%= yield %>
<% end %>
</body>

Firstly, you're looking for current_page?:
<% if current_page?(controller: "pages", action: "home") %>
...
<% end %>
This is notoriously rickety though (what happens if you change your pages controller?).
What you'll be better doing is what beartech suggested, setting a variable which you can ping in your layout:
#app/controllers/pages_controller.rb
class PagesController < ApplicationController
def home
#home = true
end
end
#app/views/layouts/application.html.erb
<if #home %>
...
<% end %>
I would match this with the use of partials:
#app/views/layouts/application.html.erb
<%= render "shared/sidebar" if #home %>
<%= render "shared/flash" %>
This gives you the ability to split your code into more manageable chunks. We do it here:

In your :index action for your home page you can set a variable like:
def index
#home_page = true
...
And then you can just do something like:
<body>
<% if current_user.present? %>
<% if #home_page %>
<div class="container">
<div class="col-md-3">
<%= render 'layouts/valuations' %>
</div>

Related

Using 'content_for' inside a view

I used content_for inside a view. This is my code:
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<%= content_for :youtube_explain %>
</div>
</div>
</div>
<% content_for :youtube_explain do %>
<div>
物件 ⇒ <%= link_to "ント(編)", "https://youtu.be/ZYhR" %>
</div>
<% end %>
It does not seem to display the content in block youtube_explain.
You must define your block of markup before you call it, like:
<%= content_for? :youtube_explain %>
<% content_for :youtube_explain do %>
<div>物件 ⇒ <%= link_to "ント(編)", "https://youtu.be/ZYhR" %></div>
<% end %>
<!-- false -->
Otherwise:
<% content_for :youtube_explain do %>
<div>物件 ⇒ <%= link_to "ント(編)", "https://youtu.be/ZYhR" %></div>
<% end %>
<%= content_for? :youtube_explain %>
<!-- true -->

Render only when i set it to render

so my question is how can i enable this piece of code only when i want? I have multiple pages and some pages i need to remove that piece of code.
This is my current partial _navbar.html.erb and div.spotlight is the piece of code i need to disable in some pages
<div class="container">
<nav class="logonav">
<div class="row">
<div class="col50"><span class="logo"></span>
<h2>Musicus</h2>
<p>De ti para o mundo</p>
</div>
<% if user_signed_in? %>
<div class="col50">
<%= link_to 'Logout', destroy_user_session_path, class: 'ui-btn btn-normal', method: :delete %>
</div>
<% else %>
<div class="col50">
<%= link_to new_user_registration_path, class: 'ui-btn btn-accent' do %>
<span class="icon-add-user"></span> Criar Conta
<% end %>
<%= link_to new_user_session_path, class: 'ui-btn btn-normal' do %>
<span class="icon-login"></span> Login
<% end %>
</div>
<% end %>
</div>
</nav>
<div class="spotlight">
<%= yield :other_message %>
<%= render 'search' %>
<%= yield :primary_message %>
</div>
<%= yield :other_nav %>
</div>
You can use a simple if statement:
<% if show_spotlight %>
<div class="spotlight">
<%= yield :other_message %>
<%= render 'search' %>
<%= yield :primary_message %>
</div>
<% end %>

Create a link from a div - Ruby on Rails

Currently I have a text link within the div that links correctly <%= link_to 'Show', profile %>. I have read that the below is the proper way to turn a div into a link
<%= link_to root_path do %>
<div>Hey!</div>
<% end %>
Though when I apply that to my block it gives me a undefined local variable or method 'profile'
<%= link_to profile do %>
<div id="profiles" class="transitions-enabled">
<% #profiles.each do |profile| %>
<div class="box panel panel-default">
<div class="panel-body">
<%= image_tag profile.image.url(:medium) %>
<%= profile.artist %><br/>
<%= link_to 'Show', profile %>
</div>
</div>
<% end %>
</div>
<% end %>
My original block without the attempt at linking:
<div id="profiles" class="transitions-enabled">
<% #profiles.each do |profile| %>
<div class="box panel panel-default">
<div class="panel-body">
<%= image_tag profile.image.url(:medium) %>
<%= profile.artist %><br/>
<%= link_to 'Show', profile %>
</div>
</div>
<% end %>
</div>
My Controller:
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy, :id]
# GET /profiles
# GET /profiles.json
def index
#profiles = Profile.all
end
# GET /profiles/1
# GET /profiles/1.json
def show
end
# GET /profiles/new
def new
#profile = Profile.new
end
#...
Your first example is problematic in many ways - you have links inside links. I'm not exactly sure what you're looking to output from this - but basically you need the
<%= link_to root_path do %>
<div>Hey!</div>
<% end %>
behavior to replace where you have <%= link_to 'Show', profile %>
Something along the lines of:
<div id="profiles" class="transitions-enabled">
<% #profiles.each do |profile| %>
<div class="box panel panel-default">
<div class="panel-body">
<%= image_tag profile.image.url(:medium) %>
<%= profile.artist %><br/>
<%= link_to profile do %>
<div>
fancy pants stuff here
</div>
<% end %>
</div>
</div>
<% end %>
</div>
If you can give me some guidance on what you want in the div I can post you some code to achieve that, I'm just not clear on what you are looking for exactly.
Another example could be (if you wanted more of the pieces to fall inside that <div></div>:
<div id="profiles" class="transitions-enabled">
<% #profiles.each do |profile| %>
<div class="box panel panel-default">
<div class="panel-body">
<%= link_to profile do %>
<div>
<%= image_tag profile.image.url(:medium) %>
<%= profile.artist %><br/>
... more fancy pants stuff here ...
</div>
<% end %>
</div>
</div>
<% end %>
</div>

How to use the default layout for nested controller?

Pretty new to rails and I was building my routes to get a nested controller like this:
resources :companies do
member do
get 'operating_hour'
end
resources :services do
member do
get 'services'
end
end
end
Now, my layout for my applications is like this:
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
<% if main_side_view? %>
<%= render 'layouts/main_side' %>
<% elsif company_side_view %>
<%= render 'layouts/company_side' %>
<% else %>
<h1><%= yield(:title) %></h1>
<%= yield %>
<% end %>
<%= render 'layouts/footer' %>
</div>
All is working fine with my layout except all that is generated by the nested ('service') controller. When I go to a link of the nested controller my layout is not there!
Any help?
I think you do it in a wrong way. You should create several layouts :
First, layouts/application.html.erb
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
<h1><%= yield(:title) %></h1>
<%= yield %>
<%= render 'layouts/footer' %>
</div>
Second, layouts/companies.html.erb
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
Your code for companies
</div>
See the guide : http://guides.rubyonrails.org/layouts_and_rendering.html#using-nested-layouts
You can create a shared view for flash messages.

Rails 3 Tutorial Chapter 11 HTML not showing for Follow/Unfollow button

Here is my show.html.erb file.
<% provide(:title, #user.name) %>
<div class="row">
<aside class="span4">
<section>
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
</h1>
</section>
<section>
<%= render 'shared/stats' %>
</section>
</aside>
<div class="span8">
<%= render 'follow_form' if signed_in? %>
<% if #user.microposts.any? %>
<h3>Microposts (<%= #user.microposts.count %>)</h3>
<ol class="microposts">
<%= render #microposts %>
</ol>
<%= will_paginate #microposts %>
<% end %>
</div>
</div>
_follow.html.erb partial file:
<%= form_for(current_user.relationships.build(followed_id: #user.id),
remote: true) do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<%= f.submit "Follow", class: "btn btn-large btn-primary" %>
<% end %>
_unfollow.html.erb partial file:
<%= form_for(current_user.relationships.find_by_followed_id(#user),
html: { method: :delete },
remote: true) do |f| %>
<%= f.submit "Unfollow", class: "btn btn-large" %>
<% end %>
And my _follow_form.html.erb partial:
<% unless current_user?(#user) %>
<div id="follow_form">
<% if current_user.following?(#user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
Edit: I would also like to say that I'm using Bootstrap. When I inspect the user page, both classes span4 and span8 come up, but there is a complete blank where "follow_form" should be.
Edit: This question was answered years ago :)
i came across the same problem , you can rectify this by doing the following changes :
step 1)
In app/views/users/show.html.erb
change code from this :
<div class = "span8">
<%= render 'follow_form' if signed_in? %>
<% if #user.microposts.any? %>
to this:
<div class = "span8">
<%= render 'shared/follow_form' if signed_in? %>
<% if #user.microposts.any? %>
step 2)
In partial app/views/shared/_follow_form.html.erb
change the code from this :
<% unless current_user?(#user) %>
<div id="follow_form">
<%if current_user.following?(#user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
to this :
<% unless current_user?(#user) %>
<div id="follow_form">
<%if current_user.following?(#user) %>
<%= render 'shared/unfollow' %>
<% else %>
<%= render 'shared/follow' %>
<% end %>
</div>
<% end %>

Resources