gmaps4rails callback doesn't always work - callback

Probably an easy question but it's been nagging at me for ages and I can't find an answer anywhere. I'm using the gmaps4rails gem and I want to add a callback function after the map has loaded. When I use the basic helper everything works fine, like this:
<%= gmaps4rails(#json) %>
<% content_for :scripts do %>
<script type="text/javascript">
Gmaps.map.callback = function() {
alert('callback');
}
</script>
<% end %>
<%= yield :scripts %>
But if I replace <%= gmaps4rails(#json) %> with the gmaps helper, the callback doesn't happen:
<%= gmaps("markers" => { "data" => #json }) %>
<% content_for :scripts do %>
<script type="text/javascript">
Gmaps.map.callback = function() {
alert('callback');
}
</script>
<% end %>
<%= yield :scripts %>
Can anyone suggest why this might happen?

Of course the answer always comes the moment you pull your hair out and ask here. The problem is the version of the gem I was using. I still don't know why it wasn't working with 1.4.6, but 1.5.5 seems to work fine.

Related

SIlverstripe <%loop AllChildren %> with Skeleton

I'm using Skeleton for my 16 columns grid system on Silverstripe.
My code below:
<% loop $AllChildren %>
<div class="three columns"><h5>$Title</h5></div>
<% end_loop %>
produces:
Children1 Children2 Children3 Children4 Children5
Children6 Children7 Children8 Children9 Children10
My question is how do I put a space between the rows so the output will
be like:
Children1 Children2 Children3 Children4 Children5
Children6 Children7 Children8 Children9 Children10
In SS3 you can use $MultipleOf() (api docs) in your templates to output something every nth item, e.g.
<% loop $AllChildren %>
<div class="three columns"><h5>$Title</h5></div>
<% if $MultipleOf(5) %>
<hr class="extra-space">
<% end_if %>
<% end_loop %>

Filtering #getFilesAtPath results in Docpad

In Docpad, the following code (using a Query-Engine helper and eco) pulls a list of file names from a directory tree and addds their url to an array:
<% images = []; %>
<% for file in #getFilesAtPath({relativeOutDirPath: 'images/'}).toJSON() : %>
<% images.push(file.url) %>
<% end %>
How might I limit the query to a subset of files, say only PNGs?
So like stated in my answer to your other question: What methods can be called on Docpad's Query tools?
Object returned by your query has some additional default metadata you can't see. As you can see here http://docpad.org/docs/meta-data, one of the metadata is "extension". So you can query with condition like:
extension:'png'
So your code might look like (notice findAll part that gives you a possibility to set search condidtions):
<% images = []; %>
<% for file in #getFilesAtPath({relativeOutDirPath: 'images/'}).findAll(extension:'png').toJSON() : %>
<% images.push(file.url) %>
<% end %>
Or if you want to return all files and trigger different actions on different extensions you could:
<% images = []; %>
<% for file in #getFilesAtPath(relativeOutDirPath: 'images/').toJSON() : %>
<% if file.extension is 'png' : %>
<% images.push(file.url) %>
<% end %>
<% end %>

How to setup bootstrap-datepicker-rails?

Anybody knows how to setup the gem bootstrap-datepicker-rails? I followed the steps in http://rubydoc.info/gems/bootstrap-datepicker-rails/0.6.21/frames, basically:
I setup twitter-bootstrap-rails gem
I add this to gemfile gem 'bootstrap-datepicker-rails', '>= 0.6.21'
Add this line to app/assets/stylesheets/application.css
*= require bootstrap-datepicker
Add this line to app/assets/javascripts/application.js
//= require bootstrap-datepicker
Add this line to app/assets/javascripts/controllername.js.coffee
$('.datepicker').datepicker()
Finally use in view :)
<input type="text" data-behaviour='datepicker' >
But it doesn't work!! Anybody has got it to work? It should work nice as shown here:
http://jsfiddle.net/2Gg5k/43/
Look at step 5 and step 6, your selection class .datepicker in javascript but not in input field
Try this
on views (e.g form)
<%= f.text_field :mydate, 'data-behaviour' => 'datepicker' %>
<script type="text/javascript">
$('[data-behaviour~=datepicker]').datepicker();
</script>
Or As component :
<div class="input-append date datepicker" data-date="12-02-2012" data-date-format="dd-mm-yyyy">
<%= f.text_field :mydate, 'data-behaviour' => 'datepicker', :disabled => 'disable' %><span class="add-on"><i class="icon-th"></i></span>
</div>
<script type="text/javascript">
$('.datepicker').datepicker();
</script>
Example above all if the attribute using date type.
You can use bootstrap-datetimepicker-rails gem, if the attribute using datetime type
In the view, try this:
<div class="field">
<%= f.label 'Trade Date: ' %>
<%= f.text_field :trade_date, 'data-behaviour' => 'datepicker' %><br />
</div>
In your application.js, use:
$(document).on("focus", "[data-behaviour~='datepicker']", function(e){
$(this).datepicker({"format": "yyyy-mm-dd", "weekStart": 1, "autoclose": true})
});
instead of
$('.datepicker').datepicker()
I did the same, but I change the code:
$('.datapicker').datapicker()
By this code:
$('[data-behaviour~=datepicker]').datepicker()
I try using a class 'datapicker' but not found.
This is my example, I use to HAML and coffeescript
events.js.coffe
$ ->
$('[data-behaviour~=datepicker]').datepicker()
_form.haml
= simple_form_for #event, :html => {:class => "form-horizontal well"} do |f|
.field (That is the same )
= f.input :date, :input_html => { "data-behaviour" => "datepicker" }
.form-actions
= f.button :submit, 'Create Event' ,:class => 'btn btn-success btn-large'

Variants of <% when writing code inside views in MVC2

I'm currently learning MVC2, and I have seen three variants of the tags that contain the actual code for a View:
<% ... %>
With a = after the %
<%= ... %>
and with a : after the %
<%: ... %>
What is the difference of these three code containers?
And are there any other variants of these?
<% ... %>
is just a block of code
<%: "blah blah" %>
Is Shorthand for
<%= Html.Encode("blah blah") %>
Which is shorthand for
<% Response.Write(Html.Encode("blah blah")) %>
1.<% ... %> just block of code
<%if (Model.HelloWorld != null){%>
Hello World!!
<%} %>
2.<%= ... %> plain text without escaping
<%=Model.HelloWorld %>
3.<%: ... %> text with escaping equal <%= Server.HtmlEncode(Model.Something) %> Details here
<%:Model.HelloWorld %>

Rails 3 Edit Multiple Records in a Single Form

I've been stuck on this problem for a couple of days now.
I've have some success with Railscasts Episode #198, but that one is for Rails 2. There have been some changes in Rails 3 that make it so the code provided in Episode #198 won't work.
The problem lies within the edit_individual.html.erb:
Original Code (provided by Ryan # Railscasts):
<% form_tag update_individual_products_path, :method => :put do %>
<% for product in #products %>
<% fields_for "products[]", product do |f| %>
<h2><%=h product.name %></h2>
<%= render "fields", :f => f %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
Modified Code (simply changed fields_for to form_for):
<% form_tag update_individual_products_path, :method => :put do %>
<% for product in #products %>
<% form_for "products[]", product do |f| %>
<h2><%=h product.name %></h2>
<%= render "fields", :f => f %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
In the new code, each record is placed within a form of their own, all inside one single form (which is the one I only want).
My question is, how can I get the code provided by Railscasts Episode #198 to work in Rails 3?
Here is a link to the Railscast I mentioned:
http://railscasts.com/episodes/198-edit-multiple-individually
Thank You,
c.allen.rosario
I found the solution. Just need to modify the following line in the code provided by Ryan # Railscasts:
<% fields_for "products[]", product do |f| %>
and change it to:
<%= fields_for "products[]", product do |f| %>
Notice, that the <% has been modified to <%=.
final solution:
<% form_tag update_individual_products_path :method => :put do %>
<% for product in #products %>
<%= fields_for "products[]", product do |f| %>
<h2><%= h product.name %></h2>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
I was wondering if anyone could explain this solution to me. From what I understand you should only need a <% in front of the fields_for.
c.allen.rosario
The change in Rails 3 from <% fields_for to <%= fields_for is because it was confusing that form_for, form_tag, etc... were using <% form... %> even though they they were outputting html code.
With Rails 3, since they output html code, they use <%=.
Please note that your first line is deprecated:
<% form_tag update_individual_products_path, :method => :put do %>
should be
<%= form_tag update_individual_products_path, :method => :put do %>
Same for all form tags.