Rails - Yield a Partial in a Layout - ruby-on-rails-3

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.

Related

How to display date in a middleman article page

I'm developing a blog with middle man, I write the articles in markdown. Below is a sample article 2016-01-27-small-and-large-balcony-ideas.html.md
---
title: Small and large balcony ideas
category: "balcony ideas"
---
# Small and large balcony ideas
It uses the following layout layouts/blog.erb
<body class="<%= page_classes %>">
<%= partial "blog_header" %>
<div class="container">
<div class="row">
<div class="column column-75">
<%= yield %>
</div>
<div class="column column-25">
<%= partial "blog_sidebar" %>
</div>
</div>
</div>
<%= partial "blog_footer" %>
</body>
Now when I go to the article.url the page displays heading and the content straightaway. I want to display the article date above or below the main heading. How can I do that.
I add:
<p><%= current_article.date.strftime('%d %B %Y') %></p>
into my layout just above
<%= yield %>
Is that what you are looking for?

Articles appear too many times

I started using Ruby on Rails a few days ago, and I have a problem with the each do loops.
Here's my AccueilController's code :
def index
#postsTest = Post.last(1)
#articles = Article.last(1)
#articlesList = Article.last(2)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #users }
end
Here's my application.html.erb code :
<div id="container">
<div id="col1">
<%= render :partial => 'shared/listArticles', :collection => #articlesList %>
<div class="clear"></div>
</div>
<div id="col2">
<%= yield %>
</div>
</div>
</div>
And now my shared/listArticles's code :
<% #articlesList.each do |article| %>
<div id="blocArticle">
<div id="pic">
<%= image_tag (article.photo.url(:thumb)) %>
</div>
<div id="contentArticle">
<div id="titleArticle">
<%= link_to article.title, article %>
<div class="clear"></div>
</div>
<div id="contentArticleDesc">
<%= link_to article.desc, article %>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
<% end %>
And my problem is that if I write #articlesList = Article.last(2) then the last two articles will appear two times; if I write #articlesList = Article.last(3) then the last three articles will appear three times etc...
Of course, I would like that each of them appear just one time.
Does someone have any ideas about the source of the problem ?
You're rendering a partial with a collection, therefore Rails calls the partial for each item in the collection. Remove your loop from the partial view or remove the collection param, don't do both!

view not rendered properly in rails

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.

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

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] } %>