javascript_include_tag not working in rails 2? - ruby-on-rails-2

The rails tag to include javascript , and for browser caching:
<%= javascript_include_tag '/skin/js/html5.js', :cache => true %>
forget about caching simple javascript_include tag is also not working.
Ex:
<%= javascript_include_tag '/skin/js/html5.js', :cache => true %>
But the script tag below is working find stop working after doing this.
<script src="/skin/js/html5.js" type="text/javascript"></script>
using Rails 2 any reason for such a behavior or this tag did not even work in rails2?

Try
First make sure that you are including the tag inside the head
<% content_for :head do %>
<script src="skin/js/html5.js"></script>
<% end %>

Related

Using Nokogiri to generate static header list in Slate/Middleman

I'm inexperienced with middleman and ruby, but I've been trying to get Slate working so it generates a side navigation/list of header during build time instead of client side using javascript. The problem I am running into is getting the code to include the headers from partials.
An example of the directory structure:
Source
+--config.rb
+--includes
+--file.md
+--otherfile.md
+--index.html
+--layouts
+--layout.erb
Gist of layout and config.rb
Config.rb snippet for this:
require 'nokogiri'
helpers do
def toc_data(page_content)
html_doc = Nokogiri::HTML::DocumentFragment.parse(page_content)
# get a flat list of headers
headers = []
html_doc.css('h1, h2, h3').each do |header|
headers.push({
id: header.attribute('id').to_s,
content: header.content,
level: header.name[1].to_i,
children: []
})
end
[3,2].each do |header_level|
header_to_nest = nil
headers = headers.reject do |header|
if header[:level] == header_level
header_to_nest[:children].push header if header_to_nest
true
else
header_to_nest = header if header[:level] == (header_level - 1)
false
end
end
end
headers
end
end
Layout snippet for this:
<ul id="toc" class="toc">
<% toc_data(page_content).each do |h1| %>
<li>
<%= h1[:content] %>
<ul class="toc-section">
<% h1[:children].each do |h2| %>
<li>
<%= h2[:content] %>
<ul class="toc-submenu">
<% h2[:children].each do |h3| %>
<li>
<%= h3[:content] %>
</li>
<% end %>
</ul>
</li>
<% end %>
</ul>
</li>
<% end %>
</ul>
...
<div class="page-wrapper">
<div class="content">
<%= page_content %>
<% current_page.data.includes && current_page.data.includes.each do |include| %>
<%= partial "includes/#{include}" %>
<% end %>
</div>
</div>
Currently, only headers from the index.html file are getting populated and nothing from the included partials. I believe I may need the existing helper to occur post build similar to what is described in the Middleman docs for sitemaps using a ready helper. I believe I have to make another change to the config code so that it captures additional content outside of the page_content, but I'm not sure what that is due to lack of familiarity. Any pointers would be appreciated.
Edit: After looking into the middleman basics docs, there appear to be two helpers from the Padrino framework that I could make use of: capture_html and concat_content. I'm trying to find where the helper page_content is defined to get extra context for the specific changes I'm making.
Not familiar with that framework but looks like toc_data(page_content) only looks at the main content but not at the current_page.data.includes partials.
So guess you need to pass the partial to your toc_data function as well.
Maybe this works?
<%
full_content = page_content
current_page.data.includes && current_page.data.includes.each do |include|
full_content += partial("includes/#{include}")
end
toc_data(full_content).each do |h1|
%>
...
<% end %>
Hope that helps.
In order to concatenate the current page data with partials with the page_content, use the code below. This also changes what all is needed to yield a complete page.
<%
if current_page.data.includes
current_page.data.includes.each do |include|
page_content += partial("includes/#{include}")
end
end
%>
...
<%= page_content %>

yield :content doesn't show content_for :content

I think I have the same issue with this.
Using multiple yields to insert content
And I tried the solution. I tried to have <%= yield :content %> in my application.html.erb and have content_for :content and the yield inside, in my view. But it is not working on my app. Can you please explain more or give sample scenario on my problem?
The links inside, must not reload, so I will not use render partial on every template I will display.
I just recently started learning Rails so it was all a little bit confusing for me. Thank you.
I tried this; this is just an example I will fix the connection of sidebar later.
in my applicaion.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Clinks</title>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<section id="container" >
<%= render 'layouts/header' %>
<%= render 'layouts/sidebar' %>
<section id="main-content">
<section class="wrapper">
<div class="row">
<!-- %= yield % -->
<%=yield(:content) %>
</div><!--/row -->
</section><!--/wrapper -->
</section><!--/main-content -->
</section><!--/container -->
</body>
</html>
in my menu_tables.html.erb inside layouts folder.
<% content_for(:content) do %>
<%= render 'menu_tables/sidemenu' %>
<%= yield %>
<% end %>
<%= render template: "layouts/application" %>
in my routes.rb
Rails.application.routes.draw do
root :to => 'pages#home'
resources :menu_tables, except: [:show]
end
then in _sidemenu.html.erb under menu_tables controller is the code for the links, so each links from sidebar have different sidemenu.
May be you need to tell your controller to use layout 'menu'. In the link you provided names of the controller and layout matched, so it worked 'magically'.
See guides:
2.2.14 Finding Layouts
To find the current layout, Rails first looks for a file in
app/views/layouts with the same base name as the controller. For
example, rendering actions from the PhotosController class will use
app/views/layouts/photos.html.erb (or
app/views/layouts/photos.builder). If there is no such
controller-specific layout, Rails will use
app/views/layouts/application.html.erb or
app/views/layouts/application.builder. If there is no .erb layout,
Rails will use a .builder layout if one exists. Rails also provides
several ways to more precisely assign specific layouts to individual
controllers and actions.
(Note: you can check what layout is used in logs, look for line similar to Rendered menu_items/index.html.erb within layouts/application)

Heroku will not render 'paginate.render' correctly

I'm using kaminari, everything works fine locally. On heroku, any code written inside the standard kaminari paginator.render block is not getting rendered.
consider
<%= paginator.render do %>
<h1> this is the paginator</h1>
<nav class="text-center">
<ul class="pagination">
<%= first_page_tag unless current_page.first? %>
<%= prev_page_tag unless current_page.first? %>
<% each_page do |page| %>
<% if page.left_outer? || page.right_outer? || page.inside_window? %>
<%= page_tag page %>
<% elsif !page.was_truncated? %>
<%= gap_tag %>
<% end %>
<% end %>
<%= next_page_tag unless current_page.last? %>
<%= last_page_tag unless current_page.last? %>
</ul>
</nav>
<% end %>
I added the <h1>this is the paginator</h1> to tinker with what is happening. My logs look clean, there is not issue. am I missing something really obvious here? I've looked at the kaminari docs and given things are working locally, I'm not entirely sure what to look at on heroku, any pointers would be much appreciated.
snap, this hurted, my collection didn't have enough objects in it, therefore, kaminari had nothing to paginate. #usererror

JQuery Datepicker with nested Resources Rails 3.1

I try to use the datepicker function in my app wich runs with Rails 3.1. I got it working if I use the following in my application.js file:
$(function(){
$("#exam_deadline").datepicker();
});
Now I have a nested Resource and tried a lot of things to get this working for it as well, but didn´t have any success. With some research I found that a possible solution would be this:
$(function(){
$('.datePicker').datePicker();
});
But if I add the class to my fields, it doesn´t even work for the exam_deadline anymore. I´m not so familiar with Javascript and I hope, someone can tell me, what I´m doing wrong.
This are the snippets from my views and .js:
js:
function remove_fields (link) {
$(link).prev("input[type=hidden]").val("1");
$(link).closest(".fields").hide();
}
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g");
$(link).parent().before(content.replace(regexp, new_id));
}
view :
exam:
<%= semantic_form_for #exam do |f| %>
<%= f.inputs do%>
<%= f.input :deadline, :as => :string,
:start_year => Time.now.year, :label => "Anmeldefrist",
:order =>[:day,:month,:year], :class => 'datePicker'%>
<% end %>
<%= f.semantic_fields_for :examdates do |builder|%>
<%= render "examdate_fields", :f => builder %>
<% end %>
<p>
<%= link_to_add_fields "Termin hinzufügen", f, :examdates %>
</p>
<%end%>
examdate_fields:
<%= f.inputs :class => 'fields' do%>
<%= f.input :date, :label =>"Termin",
:as => :string,
:order =>[:day,:month,:year], :start_year => Time.now.year,
:class=>"datePicker" %>
<%= link_to_remove_fields "Entfernen", f %>
<%end%>
my application.html.erb:
<!DOCTYPE html>
<html>
<head>
<title><%= content_for?(:title) ? yield(:title) : "Untitled" %></title>
<%= stylesheet_link_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/redmond/jquery-ui.css",
"application", "formtastic.css", "formtastic_changes.css" %>
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js",
"http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js", "application" %>
<%= csrf_meta_tag %>
<%= yield(:head) %>
</head>
...
Thanks in advance for every little tip!
Just a quick guess. You're using single quotes on your try with a class name.
If that doesn't solve the issue I would try to stick with Ids instead of classnames (I don't know whether datepicker can still reference them properly with class selectors). You would just need another Id for every input and dynamically created javascript (a datepicker for every dynamically created input), instead of a static js file.
Ok, after a lot of trying I got it. I changed the js to:
$(function(){
$(".datePicker").datepicker();
$('#ui-datepicker-div').hide();
$.datepicker._defaults.dateFormat = 'dd M yy'
});
and my view:
<%= f.input :date,
:label =>"Termin",
:as => :string,
:order =>[:day,:month,:year],
:start_year => Time.now.year,
:input_html => {:class =>"datePicker" }%>
Now it works:) I hope it helps others who struggle with the same problem.

Rails 3 & Ajax date_select onchange => submit

I'd like my date to get saved anytime I change my date via ajax, is this possible? I tried
<%= form_for #dates,:remote => true do |f| %>
<%= f.date_select :booking_date,:onchange => "this.form.submit();" %>
<% end %>
but it does nothing, any good work arounds?
From the rails documentation:
date_select(object_name, method, options = {}, html_options = {})
Because onchange is an html option you need to provide an empty set of options to date_select, otherwise it assumes onchange is a date_select option.
You should change your call to date_select to look like this:
f.date_select :booking_date, {}, :onchange => "this.form.submit();"
Are you including this in your layout:
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>

Resources