I have something like this:
-# var id:String
%div{:dojoType => 'dojo.data.ItemFileReadStore', :jsType => 'store', :url => "/path/to/resource?id=#{id}"}
I was hoping variable interpolation would work here, but it just puts #{id} into the html. I also tried:
%div{:url => 'path/to/resource?id='+id}
And that doesn't even compile. What is the right way to do this?
The correct syntax is:
%div{:url => {"/path/to/resource?id="+id}}
Related
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
I'm using nested params (via ransack nested as q) alongside normal params to build links on a page and am having trouble with getting the two to play nicely when I try and merge the nested params with the other params.
For example if I have:
{"freq"=>"weekly", "loan_amount"=>"350000",
"q"=>{"lowEquity_true"=>"1", "s"=>"rate asc"}}
and try and build a link to change the param "lowEquity_true" with
rates_url(params[:q].merge(:lowEquity_true => '0'))
then I end up with the new params below, which looks like its updated q but dropped the rest of the params.
{"lowEquity_true"=>"0", "s"=>"rate asc"}
If I instead try to merge q & merge into the other params it doesn't update q, and just merges what was in q into the other params instead
rates_url(params[:q].merge(:lowEquity_true => '0').merge(params))
{"freq"=>"weekly", "loan_amount"=>"350000", "lowEquity_true"=>"0",
"q"=>{"lowEquity_true"=>"1", "s"=>"rate asc"},
"s"=>"rate asc"}
I've tried all sorts of various combinations and don't appear to be getting anywhere so am sure that I'm missing something basic!
You are doing it wrong.
Let me explain with an example :
params = {:a => 1, :b => 2, :q => {:x => 24, :y => 25}}
At this point, params[:q] is
{:x=>24, :y=>25}
If I do,
params[:q].merge(:x => 99)
then my params[:q] will become
{:x=>99, :y=>25}
and this is what you are supplying as an argument to rates_url(params[:q].merge(:lowEquity_true => '0'))
that's why only {"lowEquity_true"=>"0", "s"=>"rate asc"} is passed to rates_url as parameters.
Now, if you do something like
params[:q].merge(:x => 99).merge(params)
then params[:q].merge(:x => 99) gives you {:x=>99, :y=>25} and then it merges {:x=>99, :y=>25} into the original params {:a => 1, :b => 2, :q => {:x => 24, :y => 25}}
, so this results into
{:x=>99, :y=>25, :a=>1, :b=>2, :q=>{:x=>24, :y=>25}}
Now, let me explain you what you should do :-
You params is
{"freq"=>"weekly", "loan_amount"=>"350000",
"q"=>{"lowEquity_true"=>"1", "s"=>"rate asc"}}
So, you should do :
params[:q].merge!(:lowEquity_true => '0')
rates_url(params)
That's it
I hope you khow the difference between merge and merge! :-
merge! is destructive, it will modify the original paramter where as merge will not unless you take it in a variable and use it.
Alternatively, if you want to do the same thing stated above in a single line then, just do
rates_url(params.merge!(:q => {:lowEquity_true => '0', "s"=>"rate asc"}))
OR
rates_url(params.merge(:q => params[:q].merge(:lowEquity_true => '0')))
I'm making a rails application so that users can search a database of midi records and find midi files that correspond to the attributes that I've given them.
For example, a user might enter data into an html form for a midi file with name = "blah" composer= "buh" and difficulty = "insane".
This is all fine and well, except that I would like when the user enters no data for a field, that field is ignored when doing the select statement on the database.
Right now this is what my select statement looks like:
#midis=Midi.where(:name => params[:midi][:name],
:style => params[:midi][:style],
:numparts => params[:midi][:numparts],
:composer=> params[:midi][:composer],
:difficulty => params[:midi[:difficulty])
This works as expected, but if for example he/she leaves :composer blank, the composer field should not considered at all. This is probably a simple syntax thing but i wasn't able to find any pages on it.
Thanks very much!
Not sure if Arel supports that directly, but you could always do something like:
conditions = {
:name => params[:midi][:name],
:style => params[:midi][:style],
:numparts => params[:midi][:numparts],
:composer=> params[:midi][:composer],
:difficulty => params[:midi[:difficulty]
}
#midis=Midi.where(conditions.select{|k,v| v.present?})
Try this:
# Select the key/value pairs which are actually set and then convert the array back to Hash
c = Hash[{
:name => params[:midi][:name],
:style => params[:midi][:style],
:numparts => params[:midi][:numparts],
:composer => params[:midi][:composer],
:difficulty => params[:midi][:difficulty]
}.select{|k, v| v.present?}]
Midi.where(c)
photo_url(Photo.first, :host => 'foobar') returns http://foobar/photos/2vig0 .
How do I do the same thing with something like 'photo'_url(Photo.first, :host => 'foobar')
Do I need to do something like: 'photo'.constantize._url(Photo.first, :host => 'foobar')
You can use send to invoke a method, once you've concatinated the two parts of the string together.
assuming #fn contains the string "photo":
send("#{#fn}_url", Photo.first, :host => 'foobar')
Or, possibly clearer:
send("photo" + "_url", Photo.first, :host => 'foobar')
How about url_for?
http://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html#method-i-url_for
I'm calling a pretty simple function, and can't seem to figure out whats going on. (I'm using rails 3.0.3 and the master branch of 'will_paginate' gem). I have the following code:
results = Article.search(params) # returns an array of articles
#search_results = results.paginate :page => params[:page], :per_page=>8, :order => order_clause
No matter what I make the order_clause (for example 'article_title desc' and 'article_title asc'), the results are always the same in the same order. So when I check using something like #search_results[0], the element is always the same. In my view, they are obviously always the same as well. Am I totally missing something?
I'm sure its something silly, but I've been banging my head against the wall all night. Any help would be much appreciated!
Edited to Add: The search clause does the following:
def self.search(params)
full_text_search(params[:query].to_s).
category_search(params[:article_category].blank? ? '' : params[:article_category][:name]).
payout_search(params[:payout_direction], params[:payout_value]).
length_search(params[:length_direction], params[:length_value]).
pending.
distinct.
all
end
where each of these guys is a searchlogic based function like this:
#scopes
scope :text_search, lambda {|query|
{
:joins => "INNER JOIN users ON users.id IN (articles.writer_id, articles.buyer_id)",
:conditions => ["(articles.article_title LIKE :query) OR
(articles.description LIKE :query) OR
(users.first_name LIKE :query) OR
(users.last_name LIKE :query)", { :query => "%#{query}%" }]
}
}
scope :distinct, :select => "distinct articles.*"
#methods
def self.payout_search(dir, val)
return no_op if val.blank?
send("payment_amount_#{dir.gsub(/\s+/,'').underscore}", val)
end
def self.length_search(dir, val)
return no_op if val.blank?
send("min_words_#{dir.gsub(/\s+/,'').underscore}", val)
end
Thanks.
If you look at the example from the will_paginate github page you can spot one important difference between their use of the :order clause and yours:
#posts = Post.paginate :page => params[:page], :order => 'created_at DESC'
This calls paginate on the Post object (with no objects being selected yet - no SQL has been executed before paginate comes along). This is different in your example: as you state in the first line of code "returns an array of articles". The simplest I can come up with showing the problem is
results = Model.limit(5).all
#results = results.paginate :order => :doesnt_matter_anymore
won't sort, but this will:
results = Model.limit(5)
#results = results.paginate :order => :matters
It should suffice to take the all out of the search method. It makes ActiveRecord actually perform the SQL query when calling this method. Will_paginate will do that for you when you call paginate (if you let it...). Check out the section on Lazy Loading in this post about Active Record Query Interface 3.0