view not rendered properly in rails - ruby-on-rails-3

I have a model - Products, ProductsController and a layout product. I declared the layout inside the controller. added the css/js/images to the app assets folder. I did everything the rails guide told me to do when I want to have a custom layout. But It still doesn't show the layout i declared but shows me the default page without any formatting and settings.
my files are as follows
Products Controller
class ProductsController < ApplicationController
layout "product"
# GET /products
# GET /products.json
def index
#products = Product.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #products }
end
end
end
and my layout file is
<!DOCTYPE html>
<html>
<head>
<title>Cool Amazon Products</title>
<%= stylesheet_link_tag "application" %>
<%= stylesheet_link_tag "bootstrap" %>
<%= stylesheet_link_tag "bootstrap-responsive" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">Cool Products from Amazon</a>
</div>
</div>
</div>
<%= yield %>
<%= javascript_include_tag "bootstrap" %>
</body>
</html>
the index.html.erb is the one that is generated by default!.
I am using twitter bootstrap for the UI. Please let me know if i'm missing something inorder to display the correct layout.

Ok, weird, that should work.
So, things to check:
Are you in development environment, or have you restarted your Rails app?
Are you using page or action caching, and you're not clearing them?
Could it be a caching issue on the web server?
That's about all that could be causing this.

Related

how to use content_for so that the content shows up in the layout

Am testing the content_for in my rails 3.2 app and following the rails guides but they are specific to the actual files and I cannot seem to get the yield to work:
application.html.erb file:
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<%= yield :navigation %> #shouldn't this load the content_for block named :navigation specified in the _main_nav.html.erb partial?
<%= yield %> #this load the index page content
</body>
</html>
I created a layout file _main_nav.html.erb (i know I can render with <%= render 'layouts/header' %> but I am trying to use the content_for instead) The _main_nav.html.erb is:
<% content_for :navigation do %>
<ul>
<li>Home</li>
</ul>
<% end %>
They way I read the RailsGuide http://guides.rubyonrails.org/layouts_and_rendering.html#using-the-content-for-method
this should work. But it does not. I do not get an error. Seems simple but I am stumped.
When I go to my index.html.erb file I would expect to see this result:
Home
I believe what you want to have is have a view that will contain your content_for block. So an example would be if you have the following:
index.html.erb
<% content_for :head do %>
<%= stylesheet_link_tag 'users' %>
#Above this will load the users stylesheet
<% end %>
<h2>Example</h2>
<ul>
<% #users.each do |users| %>
<li><%= user.name %></li>
<% end %>
</ul>
Then to output what inside the users stylesheet we can yield and pass in the symbol of the name of the content_for.
Application.html.erb
<!-DOCTYPE html>
<html>
<head>
<%= yield :head%>
<title>This is my title</title
</head>
<body>
<p>This is a test</p>
<%= yield %>
</html>
So to review whats happening here is that, in my example I am saying I have a users stylesheet that I would like to load into the <head></head> of my application.html.erb. To do this I set the content_for which is a Rails helper and give it the identifier sysmbol which is head which is then called in the application.html.erb where I do yeild :head. So what I am getting my application to do is when the my index.html.erb for that page is being rendered the application.html.erb will load my users stylesheet. Hope this clears things up for you.
Update explanation
To add to this another thing the purpose of combination of using content_for with yield is to allow you to inject data into the application layout from ANY view. So as another example. You could have the following:
<% content_for :title do %> My Title<% end %>
Here when the controller renders the view template and combines it with the application layout, the text My title will be replaced. The yield(:head) makes it easy to add more elements to the specific page if needed. Take a look at the following example:
app/views/layouts/application.html.erb
<% if content_for?(:navbar) %>
<%= yield(:navbar) %>
<% else %>
<%# default navbar %>
<section class="navbar"></section>
<% end %>
app/views/blah/index.html.erb
<% content_for(:navbar) do %>
<section class="navbar"></section>
<% end %>
And a further note not sure how your developing your application or what design framework your using but you could also take a look at Rails-Bootstrap-Navbar. May also be an alternative.
OK, I think I have a solution for this. Your code:
<% content_for :navigation do %>
<ul>
<li>Home</li>
</ul>
<% end %>
should be at the top of the file that is loading. Your _header.html.erb is a partial. If you move this code into views/tasks/new.html.erb then it works as expected.
However, for it to work as you want, then you need to adjust your application.html.erb file:
<p>this is where we should see the "Home" link appear that is defined in _header.html.erb:</p>
<section class="header">
<% render 'layouts/header' %>
<%= yield :navigation %>
</section>
Notice, that I have called the render erb tag without the = sign. This means I don't see the contents of the header partial, but it does load. If you include the = sign then it still works but also renders any other content you may have in the partial.
Note: The render tag has to be above/before the yield tag.

Nivo Slider, showing image one on top of the other (nothing of "sliding")

I'm trying to set up Nivo Slider in a rails app with twitter-bootstrap, the problem I'm facing is tha images appear on top of the other not sliding, any idea how to solve this?
here is my code:
load the nivo slider:
<div class="slider-wrapper theme-default">
<div id="slider" class="nivoSlider">
<% #page.images.each_with_index do |img, index| %>
<%= image_tag img.url, :title => '#htmlcaption' %>
</div>
</div>
<div id="htmlcaption" class="nivo-html-caption">
<%=raw #page.caption_for_image_index(index) %>
</div>
<% end %>
_javascript.html.erb
<%= javascript_include_tag 'application' %>
<%= javascript_include_tag 'jquery.nivo.slider.pack' %>
<%= javascript_include_tag 'jquery.nivo.slider' %>
<script type="text/javascript">
$(window).load(function() {
$('#slider').nivoSlider({
effect: 'fade',
pauseTime: 5000,
directionNav:true,
controlNav:true
});
});
</script>
application.js
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require bootstrap
I believe the problem comes from the each loop. If you notice inside the loop you have two closing div tags - just bellow this line <%= image_tag img.url, :title => '#htmlcaption' %>. Therefore you open the divs once, but close then multiple times.
What you can do instead is change slightly the code like:
<div class="slider-wrapper theme-default">
<div id="slider" class="nivoSlider">
<% #page.images.each do |img| %>
<%= image_tag img.url, :title => img.caption_for_image %>
<% end %>
</div>
</div>
Have a look at Nivo Docs. There are further example for alternative caption options.
I found a very easy fix to this issue:
I'm using a legacy CMS and that's inserting image map tags next to some images in the slider (resulting in blank slides). Simply ignoring irrelevant elements seems to work well.
In your jquery.nivo.slider.js file replace line 36
//Find our slider children
Replace this
36 - var kids = slider.children();
with
36 - var kids = slider.children("img,a");
save the change, reload your browser and issue solved.
For more details visit github gilbitron / Nivo-Slider

Rails - Yield a Partial in a Layout

I think this is a very simple problem and I could be just going about it incorrectly because I am new to rails, but here it goes......
What I want to do is render the results of my partial view in my application layout using a yield. I have a search bar at the top of my page that I want the results to display in the yield in the layout. Again, I'm not sure if this is the proper way to do this, so if it isn't, just let me know the best way.
Right now, I get a "Missing template events/search" error when I try and use the search on my page. If I add the following code to controller it gets all of the information properly, but doesn't render in the application layout, it just displays the partial:
render :partial => "events/search_results"
Here is what the relevant section controller looks like:
layout "application"
def search
#events = Event.search params[:search]
end
Here is the application layout (Please excuse the formatting, I tried to make it easily readable):
<!DOCTYPE html>
<html lang="en">
<head>
<title>XXXXXXX</title>
<!--[if lt IE 9]> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"type="text/javascript"></script> <![endif]-->
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="navbar navbar-top">
<div class="navbar-inner">
<div class="container">
<%= render :partial => "events/search" %>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span1">
</br>
<div class="well well-small">
<center><h3>Filters</h3></center> </br>
<%= render :partial => "events/left_severity_filter" %>
<%= render :partial => "events/left_date_filter" %>
</div>
</div>
<div class="span8">
<%= yield %>
</div>
<div class="span3">
<%= render :partial => "events/right_filter" %
</div>
</div>
</div>
Any help would be much appreciated. Thanks.
You do not have the events/search.html.erb template in the views. Create this view template and it will be displayed where the yield is.

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?

WickedPDF missing layout

I have a controller like this:
def show
#professor = Professor.find(params[:id])
respond_to do |format|
format.html
format.pdf do
render :pdf => "file_name"
end
end
end
And a simple view like this:
<p>Professor: <%= #professor.first_name %></p>
<p>Email: <%= #professor.email if #professor.email %></p>
I also have a layout 'application.html.erb';
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= content_for?(:title) ? yield(:title) : "Myapp" %></title>
<meta name="description" content="">
<meta name="author" content="">
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= yield(:head) %>
</head>
<body>
<header class="navbar navbar-fixed-top">
<nav class="navbar-inner">
<div class="container">
<%= render 'layouts/navigation' %>
</div>
</nav>
</header>
<div id="main" role="main">
<div class="container">
<div class="content">
<div class="row">
<div class="span12">
<%= render 'layouts/messages' %>
<%= yield %>
</div>
</div>
<footer>
</footer>
</div>
</div> <!--! end of .container -->
</div>
When I do the following command:
bundle exec wkhtmltopdf 'http://local.myapp.com:3000/professors/2' - > test.pdf
I get a pdf with all the styles and layout properly.
However, when I go to http://local.myapp.com:3000/professors/2.pdf I get an error saying:
Missing template professors/show with {:locale=>[:en], :formats=>[:pdf], :handlers=>[:erb, :builder, :coffee]}
So, then I changed my controller#show action to be like this:
def show
#professor = Professor.find(params[:id])
respond_to do |format|
format.html
format.pdf do
render :pdf => "file_name",
:template => 'professors/show.html.erb'
end
end
end
That helps me to render the view, but unfortunatelly it doesn't render the styles and the layout. My questions are:
Why do I need to specify the template?
Why is the layout not being rendered in the PDF?
You need to rename your view to show.pdf.html.erb or create a new show.pdf.erb.
The .pdf in the filename lets the handler know it can use it.
Also, you probably need to use the wicked_pdf_stylesheet_link_tag helper to get your styles to show up. If you want to have a dual-purpose view, then something like this may be necessary:
<% if params[:format] && params[:format] == 'pdf' %>
<%= wicked_pdf_stylesheet_link_tag 'application' %>
<% else %>
<%= stylesheet_link_tag 'application' %>
<% end %>