Creating a Blank Rails 3.1 Templating Handler - markup

What I want to do is make it just output the view itself, and ignore what Rails would normally think is embedded Ruby within the HTML.
For example:
<div class="current">
<div class="question">
<h3 id="homework_name"><%= homework.name %><h3 id="due">Due <%= homework.due %></h3></h3>
<h2 class="title">The Question:</h2>
<p id="question_contents"class="contents"><%= current_question.contents</p>
</div>
<div class="answer">
<h2 class="title">Your Answer:</h2>
<textarea class="contents" id="student_answer"><%= current_question.answer %></textarea>
</div>
</div>
I want an ActionView Template Handler to ignore all mentions of:
<%= homework.name %>
<%= homework.due %>
<%= current_question.contents %>
<%= current_question.answer %>
More specifically, it should ignore any tags starting with <% and ending with %>
If you need more info check out http://pastie.org/private/epsxnapldho6co2y0indg

Here you go https://gist.github.com/1144297
And use .html.lifo extensions for your templates.

Related

Solved! How to connect javascript files in Rails application?

I am writing a simple Ruby on Rails training application, and use semantic-ui css framework.
From dropdown menu need to write a little code for jquery.
If I write .js code directly in html code, it works.
If I put it in a separate file app/assets/javascripts/dropdown.js this code stop working. But! If use .coffee, the code works again!
How to connect js files to Rails?
So, i have javascript/dropdown.js. I tried to include it in a application.js file, although this is not necessary, it did not work.
I write this code directly in application.js - not work
Solution:
I made a syntax error. I mixed up the closing brackets in method $(document).ready. The .coffee file worked, because there are no brackets! Do not code at night, the brain does not work well. I found my mistake in the morning.
P.S. as a result i used $(document).on('turbolinks:load', function(){}) in dropdown.js. If use 'turbolinks:load', dropdown menu will work on all pages of the site, and not just on the index page.
application.js
//= require jquery
//= require semantic-ui
//= require rails-ujs
//= require dropdown
//= require activestorage
//= require turbolinks
//= require_tree .
dropdown.js
$('.dropdown').dropdown({transition: 'drop'});
dropdown.js not work too
$( document ).ready(function() {
$('.dropdown').dropdown({transition: 'drop'});
})
_header.html.erb
_header.html.erb
<div class="ui secondary menu">
<div class="ui container">
<%= link_to "Sample App", root_path, id: "logo", class: "item header" %>
<div class="right menu">
<%= link_to "Home", root_path, class: "item" %>
<%= link_to "Help", help_path, class: "item" %>
<% if logged_in? %>
<%= link_to "Users", '#', class: "item" %>
<div class="ui dropdown item">
Account <i class="dropdown icon"></i>
<div class="menu">
<%= link_to "Profile", current_user, class: "item" %>
<%= link_to "Settings", '#', class: "item" %>
<%= link_to "Log out", logout_path, method: "delete", class: "item" %>
</div>
</div>
<% else %>
<%= link_to "Log in", login_path, class: "item" %>
<% end %>
</div>
</div>
</div>
<script>
// Script from dropdown: assets/javascript/dropdown.js
//$('.dropdown').dropdown({transition: 'drop'});
</script>

Issue using .erb to display date with Sinatra

I am using ruby .erb to display time in a div's span. However using something similar for date does not work.
<div id="comment_space">
<% #post.comments.each_with_index do |comment,date,posted,index| %>
<span class="above"><%= date = ('%Y-%m-%d') %> </span>
<div class="comment" id="msg_<%= index+1 %>">
<%= comment.usr_comment %>
<span class="right">
<%= comment.created_at.localtime.strftime("%l: %M %p") %>
</span>
</div>
<% end %>
<span class="above">
<%= date = ('%Y-%m-%d') %>
</span>
This is the part not working. However
<span class="right">
<%= comment.created_at.localtime.strftime("%l: %M %p") %>
</span>
works perfectly.
The first error was that "created_at" was not lked for the date and it has gone down hill from there. The date and time were defined in Javascript with timestamps in a Ruby ActiveRecord table.
<%= date = ('%Y-%m-%d') %> # Ehhhhhm
Ok, compare that to your other way of doing this.
<%= comment.created_at.localtime.strftime("%l: %M %p") %>
See the difference?
If the "date" is Date class, you can do:
<%= date.strftime('%Y-%m-%d') %>
and it should work with no problem whatsoever.
If you do what you did, you say that the date is now '%Y-%m-%d'
date = ('%Y-%m-%d')
# => "%Y-%m-%d"

How to detect what media query is active with Ruby on Rails?

I want to use different views for my platform on mobile devices depending on page orientation (portrait vs landscape).
Is there a way how this could work:
<div class="row item-list-video">
<% for program in #programs %>
<% if (stylesheet_link_tag "global", :media => "only screen and (max-width: 990px)") %>
<%= render partial: 'program_preview_landscape',locals: { program: program} %>
<% else %>
<%= render partial: 'program_preview',locals: { program: program} %>
<% end %>
<% end %>
</div>
I am not sure about how this part is working:
if (stylesheet_link_tag "global", :media => "only screen and (max-width: 990px)")
My thoughts were that stylesheet_link_tag is the file name of the .css where the media queries are defined. But what I get is:
Asset was not declared to be precompiled in production.
Add Rails.application.config.assets.precompile += %w( global.css ) to config/initializers/assets.rb and restart your server
The program_preview_landscape loads this code:
<div class="col-md-3 col-sm-6 col-xs-3 program-thumbnail-landscape">
<a href="/shows/<%= program.slug %>">
<img src="<%= program.thumbnail_uri %>">
</a>
</div>
While the program_preview loads this:
<div class="col-md-3 col-sm-6 col-xs-6 program-thumbnail">
<a href="/shows/<%= program.slug %>">
<img src="<%= program.thumbnail_uri %>">
</a>
</div>
You can't read the media query from an ERB, because the media width is only calculated in the browser after the HTML is delivered, and by then the ERB has already been rendered. The usual approach to this sort of thing is to have your ERB generate HTML with semantic markup — the same HTML for both desktop and mobile — and then use CSS media queries to apply different styles depending on the screen width.

AJAX using rails: first item does not get updated on cart

I have just started learning Ruby on Rails using Agile Web Develop ment by Sam Ruby. I'm currently stuck on task F: adding Ajax to cart. Everything went well till the time I added to the code for hiding an empty cart. Now when I add the first item the cart shows up empty on the side bar (it should show up with one item n the cart) but when i add the second item the cart shows up with 2 items, as it should.The code works if I add more items. I'm facing the problem only when adding the first item on the cart. I've been tearing my hair out for a day now on this problem. Any help will be greatly appreciated. Apologies if I haven't furnished complete details. Please let me know the additional details, if any, that would be relevant. I'm using rails 3.2.8 and ruby 1.9.3 Thanks!
_cart.html.erb
<div class="cart_title">Your Cart</div>
<table>
<%= render(cart.line_items) %>
<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= number_to_currency(cart.total_price) %></td>
</tr>
</table>
<%= button_to 'Empty Cart',cart, method: :delete, confirm: 'Are you sure?'%>
_line_item.html.erb
<%if #current_item==line_item%>
<tr id="current_item">
<% else %>
<tr>
<% end %>
<td><%= line_item.quantity %> ×</td>
<td><%= line_item.product.title %></td>
<td class="item_price" ><%= number_to_currency(line_item.total_price) %></td>
<td><%= button_to 'Remove Item', line_item, method: :delete, confirm: 'Are you sure?'%>
</tr>
create.js.erb
$("#notice").hide();
if ($('#cart tr').length > 0) { $('#cart').show('blind', 1000); }
$('#cart').html("<%=j render #cart %>");
$('#current_item').css({'background-color':'#88cc88'}).
animate({'background-color':'#114411'}, 1000);
application.js.erb
<html>
<head>
<title>Pragprog Books Online Store</title>
<!-- START:stylesheet -->
<%= stylesheet_link_tag "scaffold" %>
<%= stylesheet_link_tag "depot", :media => "all" %><!-- <label id="code.slt"/> -->
<!-- END:stylesheet -->
<%= javascript_include_tag "application" %>
<%= csrf_meta_tag %><!-- <label id="code.csrf"/> -->
</head>
<body id="store">
<div id="banner">
<%= image_tag("logo.png") %>
<%= #page_title || "Pragmatic Bookshelf" %><!-- <label id="code.depot.e.title"/> -->
</div>
<div id="columns">
<div id="side">
Home<br />
Questions<br />
News<br />
Contact<br />
<%if #cart%>
<%= hidden_div_if(#cart.line_items.empty?, id:"cart") do%>
<%=render #cart%>
<% end %>
<% end %>
</div>
<div id="main">
<%= yield %><!-- <label id="code.depot.e.include"/> -->
</div>
</div>
</body>
</html>
~
managed to solve it myself :)..while typing out the problem i got a hint that the problem was somehow with the rendering itself and so it was .. the solution is to set the show parameter to 0 in { $('#cart').show('blind', 1000); } the code should now be { $('#cart').show('blind', 0); }
#Btuman done!!

Ruby on Rails 3 : Playing with views

I am sorry for my bad english. I just try to description my question. :)
I have an application layout that have a yield for display post in body. I have another yield :footerpost3 for display title of recent post on the footer.
When I in localhost:3000, the yield :footerpost3 display a recent of title correctly. but when i am click a post link, which is the url is localhost:3000/posts/3, the yield :footerpost3 display nothing.
Here is my code:
app/views/layout/application.html.erb
<!-- begin footer comment widget -->
<div class="footer_list widget_recent_comments">
<div class="title"><h5>Artikel Terkini</h5></div>
<%= yield :footerpost3 %>
</div>
<!-- end footer comment widget -->
app/views/store/index.html.erb
<% content_for :footerpost3 do %>
<% #postsMain.each do |dopostAll| %>
<div class="entry">
<ul>
<li class="recentcomments"><%= link_to dopostAll.title, dopostAll %></li>
</ul>
</div>
<% end %>
<% end %>
i hope my question is easy to understand.. :)
Looks like your root url is stores#index .
You must be initializing #postsMain in the stores#index action and generating the content_for footerpost3 in stores/index.html.erb.
When you click on a post, you will be taken to posts#show page. So you have to initialize #postsMain even in posts#show action and generate the content for footerpost3 even in posts/show.html.erb
The answer is there in your question. You are defining the "content for" footerpost3 in that block, which exists in index.html.erb. When you're on /posts/3, index.html.erb is not rendered, but rather show.html.erb is.
To solve this, you'd need to add the content in the show.html.erb template as well.
You could solve this in multiple ways. Using nested layouts would be one. For example, you might create a posts layout at app/views/layout/posts.html.erb, like so:
<% content_for :footerpost3 do %>
<% #postsMain.each do |dopostAll| %>
<div class="entry">
<ul>
<li class="recentcomments"><%= link_to dopostAll.title, dopostAll %></li>
</ul>
</div>
<% end %>
<% end %>
<%= render :file => 'layouts/application' %>
In this way, all the views of your PostsController would use this layout, which simply adds your footer content, then renders the application_layout.

Resources