Hide Page Elements From View - ruby-on-rails-3

I have a blog set up with the usual articles and tags on the index page. The Tags section also shows up on the sign-in and sign-up pages which doesn't look great.
The tags are set up in their own column under _side.html.erb - is there a way to hide them from all other pages apart from the index page?
<h3>Blog Tags</h3>
<div id= "tags" >
<% cache('all_tags') do %>
<% for tag in Tag.find(:all, :order => 'name') %>
<ul style="list-style-type: none">
<li>
<%= link_to "#{tag.name}", tag_path(tag) %>
</li>
</ul>
<% end %>
</div>

If you
render :partial => 'side'
from within your application.html.erb, then you could use another layout for the index page which then renders the partial while the standard layout would not.

Related

Adding menus in Rails 3

A bit of issue here. Im trying to add a menu to all pages. Reason for this is the ease of editing a single file which updates all web pages.
In my layouts/application.html.erb I have this, between body tags:
<% content_for :menu do %>
<ul>
<li> page 1 </li>
<li> page 2 </li>
</ul>
<% end %>
<%= yield %>
And in my welcome/index I have:
<div id="menu">
<%= yield :menu%>
</div>
<h1>Welcome to my index page!</h1>
Not sure if all that is needed so when I go to my root, I only see what is in the welcome/index file and not the links. Am I missing something?
Its should be other way around, that means you could call the :yield in application layout and have the content_for in your index file.
Actually the idea of the content_for tags are to allow slightly different variations for different pages but, still calling the same name from layout. read more about content_for from here
And I think , in your case what you need is a partial in the layout, or even you can have your menu in the layout itself. since the layout is visible for every page your menu will be available for each page, and if you need modifications in later, still you have to change only one page
1 - as a partial
#app/views/layouts/_menu.html.erb
<ul>
<li> page 1 </li>
<li> page 2 </li>
</ul>
#app/views/layouts/application.html.erb
<%= render partial: 'menu' %>
<%= yield %>
2 - having all in the same file
#app/views/layouts/application.html.erb
<ul>
<li> page 1 </li>
<li> page 2 </li>
</ul>
<%= yield %>
you should have the <%= yield :menu%> in your application.html file.
Also you can make a folder (lets name it "shared") where you can have your menus, messages etc.
Then you can try something like this
#welcome/index.html.erb
<div class="content">
Your Index Content here.
</div>
#layouts/application.html.erb
<%= yield :menu %>
<%= yield %>
#shared/menu.html.erb
<% content_for :menu do %>
<div id="menu">
<ul>
<li> page 1 </li>
<li> page 2 </li>
</ul>
</div>
<% end %>

RoR: how can I paginate all of my users microposts all in one place?

I want the home page to show everyones microposts, but I keep getting errors. I feel like this might be because the microposts have a belong_to has_many relationship with users. But anyways, This is the code for the home page..
<section>
<%= render 'shared/user_info' %>
</section>
<section>
<div id= "purchases">
<%= render 'shared/micropost_form_purchase' %>
</div>
<div id="sales">
<%= render 'shared/micropost_form_sale' %>
</div>
</section>
<ol class="microposts">
<%= render #microposts %>
</ol>
<%= will_paginate #microposts %>
and it gives me this error: 'nil' is not an ActiveModel-compatible object that returns a valid partial path. at the bottom.
I added
def home
#microposts = Micropost.all
end
to the microposts controller.
can anyone help me out? please?
The call to render is looking for a partial view that it can't find. If you're keeping all your partials in views/shared/, then do you have a views/shared/micropost.html.erb for it to render?

Is it possible to pass html elements into a partial?

I have a partial that contains a header, a subheader and potentially one or more buttons. It is used on many different pages. Often the pages don't need buttons, so I just pass in the header and an optional subheader to the partial. However sometimes the pages need one or more buttons and the only way I've managed to allow for an arbitrary number of buttons to be passed in is using content_for. My partial looks like this:
<% if defined? page_title %>
<header class="pageHeader">
<div class="page-details">
<h3><%= page_title %></h3>
<% if defined? page_subtitle %>
<p><%= page_subtitle %></p>
<% end %>
</div>
<ul class="crud-menu nav-pills">
<%= content_for :page_header_buttons %>
</ul>
</header>
<% end %>
This use of content_for nasty. Is there any way I can pass the list items / buttons into this partial? How else could I deal with this situation?
You could transform this partial into a layout:
<% if defined? page_title %>
<header class="pageHeader">
<div class="page-details">
<h3><%= page_title %></h3>
<% if defined? page_subtitle %>
<p><%= page_subtitle %></p>
<% end %>
</div>
<ul class="crud-menu nav-pills">
<%= yield %>
</ul>
</header>
<% end %>
And you would render it like that:
<%= render layout: "your_partial_above", locals: { page_title: "page title } do %>
<%= render "partial_with_your_buttons" %>
<% end %>

Refactoring - Chapter 11, Exercise 3 Rails Tutorial 2nd Edition

Anyone else working through Chapter 11 Exercises for Michael Hartl's Rails Tutorial 2nd Edition?
Chapter 11, Exercise 3 asks:
Refactor Listing 11.31 by adding partials for the code common to the following/followers pages, the Home page, and the user show page.
I'm not seeing anything worth refactoring in the homepage, user show page, or the show_follow page
If anyone came up with something worthwhile for this exercise, would love to know.
Thanks!
You can refactor the first block of code from Listing 11.31:
<section>
<%= gravatar_for #user %>
<h1><%= #user.name %></h1>
<span><%= link_to "view my profile", #user %></span>
<span><b>Microposts:</b> <%= #user.microposts.count %></span>
</section>
because it is essentially the same as the views\shared_user_info.html.erb partial used on the home page (Listing 10.32). Therefore, you can replace the block of code above with this:
<%= render 'shared/user_info' %>
Note that you will also need to add <% #user ||= current_user %> to the top of the views\shared_user_info.html.erb partial (which is the same as what was necessary to add to the stats partial in Listing 11.20).
Additionally, there is some duplication (though not exact duplication) between the feed_item + feed partials with the user + micropost partials, where depending on which page is being displayed (follow_show, home, or profile) there are one or more elements being listed (name, gravatar, admin delete link, micropost content, micropost time stamp, and micropost delete link). Those could probably be refactored too to eliminate the feed_item+feed partials and replace them with a combination of the user + micropost partials depending on the page.
I just worked through this exercise and found a solution that works.
First I changed around app/views/shared/_user_info.html.erb to use the #user variable if it is set, and the current_user variable otherwise.
app/views/shared/_user_info.html.erb:
<% if #user %>
<%= link_to gravatar_for(#user, size: 52), #user %>
<h1>
<%= #user.name %>
</h1>
<span>
<%= link_to "view my profile", #user %>
</span>
<span>
<%= pluralize(#user.microposts.count, "micropost") %>
</span>
<% else %>
<%= link_to gravatar_for(current_user, size: 52), current_user %>
<h1>
<%= current_user.name %>
</h1>
<span>
<%= link_to "view my profile", current_user %>
</span>
<span>
<%= pluralize(current_user.microposts.count, "micropost") %>
</span>
<% end %>
Then I replaced the corresponding information in app/views/users/show_follow.hmtl.erb with the partial _user_info.html.erb
app/views/users/show_follow.hmtl.erb:
<div class="row">
<aside class="span4">
<section>
<%= render 'shared/user_info' %>
</section>
<section>
<%= render 'shared/stats' %>
<% if #users.any? %>
<div class="user_avatars">
<% #users.each do |user| %>
<%= link_to gravatar_for(user, size: 30), user %>
<% end %>
</div>
<% end %>
</section>
</aside>
<div class="span8">
<h3><%= #title %></h3>
<% if #users.any? %>
<ul class="users">
<%= render #users %>
</ul>
<%= will_paginate %>
<% end %>
</div>
</div>
I hope this answer helps anyone going through M. Hartl's tutorial.

Render partial with content input in rails 3

I am trying to DRY up some of my HTML in my application currently I have a block of HTML that will get re-used multiple times
<div class="block">
<div class="block_head">
<div class="bheadl"></div>
<div class="bheadr"></div>
<h2>Configuration Needed</h2>
</div>
<div class="block_content">
<div class="message warning">
<p>You have not create an admin user yet</p>
</div>
</div>
<div class="bendl"></div>
<div class="bendr"></div>
</div>
What I would like to do is to create a partial or something along those lines and be able to pass in the content to the block header and content
Does anyone know of a way to do this in rails 3
The way i do it is to have a views/shared folder. Then, i create partials inside and i call them like :
<%= render "shared/flash_error", :error => flash[:error] %>
where shared/flash_error is :
<% if error %>
<%= error %>
<% end %>
If you want to have your partials in the partial folder, use this syntax :
<%= render :partial => "partials/your_partial", :locals => { :error => flash[:error] } %>