haml - Passing variable through .haml files - haml

I have view.haml and partial.haml what I want to approach is, declare a variable in view.haml but print it in partial.haml after rendering it, I am NOT using rails or synatra or aany framework, this is just with only haml, how can I do this?
view.haml code:
- test = 'Hello world'
!!! 5
%html{:lang => "es"}
%head
%body
/ Breadcrumbs
= Haml::Engine.new(File.read(File.expand_path 'src/views/components/breadcrumbs.haml', File.dirname(__FILE__))).render
partial.haml
%section.Breadcrumbs
%h2.Breadcrumbs-title Title
%ul.Breadcrumbs-links
%li
%a{:href => "#"}
#{test}
%li
%a{:href => "#"} Bread 2
%li.active Bread 3

Related

rails3 content_tag with static attributes

The following is being properly generated into HTML code
<%= content_tag(:span, (t 'hints.h'), :class => "has-tip", :title => (t 'hints.s') ) %>
But I am trying to generate
<span data-tooltip aria-haspopup="true" class="has-tip" title="title bla bla">translated h</span>
and have found no way to generate these span attributes data-tooltip aria-haspopup="true" They cannot be part of the options hash given one has only a name... and the second one has a dash which impedes from defining it as a symbol :aria-haspopup
I suggest that you use the following:
content_tag(:span, t('hints.h'), :class => 'has-tip', :title => t('hints.s'), :'aria-haspopup' => true, :'data-tooltip' => '')
Note that you can use the dash character in symbols if you enclose them in quotes.
The data attribute you could also specify as nested hash like :data => {:tooltip => ''} instead of :'data-tooltip' => '', use whatever you prefer.
As for the boolean attribute data-tooltip, setting the value to an empty string is as good as omitting it (and your best option with Rails 3 ;)). See also:
http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#boolean-attributes

rails 3.2 jquery autocomplete minlength

I am following the instructions to implement auto complete in a rails 3.2.11 application but I need to specify a minimum number of characters to type before the query triggers. THe jQuery API documentation has an attribute "minLength". I can't figure out how to implement this in a rails auto complete field tag. Here is my code for the field tag.
<%= autocomplete_field_tag 'unit', '', autocomplete_unit_identifier_subjects_path, :id_element => '#subject_id', :size => 75 %>
Here is the url to the instructions I am following.
https://github.com/crowdint/rails3-jquery-autocomplete
If anyone is looking for an updated answer, it appears you can now set minimum length with the attribute 'min-length'.
<%= autocomplete_field_tag 'group_name', '', group_autocomplete_path, 'placeholder' => 'Select a Job Number', 'size' => 35, 'class' => 'styled-select', 'data-auto-focus' => true, 'min-length' => 1 %>
Why its not 'minlength' as documented in jQuery autocomplete, I don't know..
Well, minLength doesn't work because of this code in autocomplete-rails.js, line 65 or so:
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
You can change the '2' to whatever you want the minLength to be.

How to remove "=>" from json output in Ruby on Rails 3?

I am stuck with what I think is a simple problem. I am creating json and need to have the format be:
[{ "source" : "google / organic", "visits" : 20 }]
And here is what I get:
[{"source"=>"google / organic", "visits"=>20}]
Here is the model (campaign_results.rb)
def as_json(options = {})
{ "source" => source,
"visits" => visits,
}
end
In the controller:
def show
#campaign_summary = CampaignResults.all
end
In the view:
<%= raw #campaign_summary.as_json %>
Any suggestions on what I should do to replace the "=>" with ":"?
Try calling #to_json:
<%= raw #campaign_summary.as_json.to_json %>

One efficient SQL call instead of 500

In my view, I've got a fiddly loop which creates 500 SQL queries (to get the info for 500 books). How can I avoid lots of SQL queries by loading a variable up in the controller?
My current (pseudo) code:
controller index action:
#books = Book.scoped.where(:client_id => #client.id).text_search(params[:query])
#feature_root = Book.multiple_summary_details_by_category( #books )
#...returns a hash of books
#features = #feature_root.to_a.paginate(:page => params[:page], :per_page => 4)
index.html.haml
= render :partial => "feature", :locals => { :features => #features }
_features.html.haml
- features.each_with_index do |(cat_name, array_of_books), i|
%h2
= cat_name
- array_of_books[0..10].each do |feature|
= link_to image_tag(feature[:cover], :class => "product_image_tiny"), book_path(feature[:book])
# more code
- array_of_books.sort_by{ |k, v| k["Author"] }.each do |feature|
- feature.each do |heading,value|
%span.summary_title
= heading + ':'
%span.summary_value
= value
What have you tried so far? It should be quite easy with standard ActiveRecord queries as documented in http://guides.rubyonrails.org/active_record_querying.html.
Also, instead of
array_of_books.sort_by{ |k, v| k["Author"] }
try something like
Book.order("author DESC")
(not sure about your exact model here) to let the db do the sorting rather than putting them in an array and let ruby handle it.

Rails 3 - Merge query options

I have this method form a Rails 2.3.4 app:
def self.find_all_colored(query, options={})
finder_options = {:conditions => "color = #{query}"}.merge(options)
Car.find(:all, finder_options)
end
With which I can do:
Car.find_all_colored("red", :limit => 5)
But I am having a really bad time trying to get that to work in Rails 3.1.1, by now I can make it work but without the .merge(options), if I add that part:
def self.find_all_colored(query, options={})
Car.where("color = #{query}").merge(options)
end
I get this error:
undefined method `default_scoped?' for {:limit=>5}:Hash
I've googled and searched in stackoverflow.com but no luck...thanks!
Try the following:
def self.find_all_colored(query, options={})
self.all({:conditions => {:color => query}}.merge(options))
end