Is posibble to have something like this? :
%div{"data-regex": "a/regular/expression"}
When I try to do this ways, I get this error:
syntax error, unexpected ':', expecting tASSOC
I tried this: %div{"data-regex": #{"a/regular/expression"}}, but is the same.
What you're probably looking for is:
%div{data: {regex: "a/regular/expression"} }
However it would be nice if you included the desired HTML in your question so we could know for sure. The other answer provided will also work, but this is especially nice if you want to provide many data attributes without repeating "data-" all over the place. That is, you can do:
%div{data: {regex: "a/reg/ex", attr2: "something", attr3: "something else" } }
Note, your problem is that the nice syntax in Ruby 1.9+ for Symbol keys in hashes doesn't work with strings preceding the colon.
{ a: 123 }
# => { :a => 123 }
{ :"a" => 123 }
# => { :a => 123 }
{ "a" => 123 }
# => { "a" => 123 }
{ "a": => 123 }
# => SyntaxError ...
To make sure that it works, you can try usual form to write a hash of parameters:
%div{:'data-regex' => "a/regular/expression"}
I guess, this may be applied to use in ruby 2.x:
%div{"data-regex": "a/regular/expression"}
Related
I'm adding pg_search into a Rails app. I'm following the instructions on github and this railscast, but I've run into a problem.
I'm setting up a multi model search, and I have a basic implementation working. But I want to extend pg_seach to use its English dictionary.
I already have an initializer:
PgSearch.multisearch_options = {
:using => [:tsearch,:trigram],
:ignoring => :accents
}
So, from what I've read, it looks like adding the dictioary should be as simple as
PgSearch.multisearch_options = {
:using => [:tsearch => [:dictionary => "english"],:trigram],
:ignoring => :accents
}
But when I start my server
...config/initializers/pg_search.rb:2: syntax error, unexpected ']', expecting tASSOC (SyntaxError)
:using => [:tsearch => [:dictionary => "english"],:trigram],
I've tried swapping square for curly brackets, and all the other syntax permutations I can think of, but no luck.
What is the correct syntax here? And why aren't my attempts valid, as I've followed the syntax for scoped searches?
What you posted is not valid Ruby syntax.
You want something like this:
PgSearch.multisearch_options = {
:using => {
:tsearch => {
:dictionary => "english"
},
:trigram => {}
},
:ignoring => :accents
}
The reason is that you must use a Hash if you want to have key-value pairs. So essentially, pg_search allows 2 syntaxes:
:using => someArray # such as [:tsearch, :trigram]
which means "use tsearch and trigram, both with the default options"
or
:using => someHash # such as {:tsearch => optionsHash1, :trigram => optionsHash2}
which means "use tsearch with some options from optionsHash1, and use trigram with some options from OptionsHash2"
Let me know if there's anything I can do to clarify. It's pretty basic Ruby syntax, but I understand that the fact that pg_search accepts both formats can be confusing to those who aren't as familiar.
I am using Rails 3.0.5 and I have setup a route using a regex constraint. It used to work on Rails 2.3.5, but it's not working in Rails 3. The route looks like this:
get '/:version_id' => 'pastes#show', :constraints => { :version_id => /[\d\w]{40}/ }
It doesn't work at all. However, the following work:
get '/:version_id' => 'pastes#show', :constraints => { :version_id => /.{40}/ }
get '/:version_id' => 'pastes#show', :constraints => { :version_id => /\w{40}/ }
get '/:version_id' => 'pastes#show'
Is there something wrong with the way Rails handles [ ] matching? or am I doing something wrong?
version_id usually looks something like this:
816616001d7ce848944a9e0d71a5a22d3b546943
I don't have a solution as to why one may not work over the other.
However, according to the PickAxe book, \w is actually a superset of \d.
\w [A-Za-z0-9\_] ASCII word character
\d [0-9] ASCII decimal digit character
Therefore, [\d\w]{40} is no different from \w{40}, which works for you.
I can't seem to find a solution to what seems to me like a simple problem.
Sitemap.find(:all, :conditions => { :controller => 'sample', :action => '!index' })
Now obviously the ! in 'index' doesn't belong there, but I put it there to illustrate that I want any result EXCEPT 'index'. I've tried something like the line below but I've gotten server errors whenever I try it.
Sitemap.find(:all, :conditions => { :controller => 'sample', "action <> 'index'" })
Use the array syntax for this:
Sitemap.all(:conditions => ["controller = ? AND action <> ?", 'sample', 'index']
Hash syntax is only useful if you're checking for equality.
Basicaly I just want to insert this + "?direction=desc" in helper method.
But once it parses it comes out like this..
/organizations/search?order_by=contactable%3Fdirection%3Ddesc
Anyone know a way around this?
My Helper Method:
def search_sort(name, sort_by, order = 'asc')
link_to(name, url_for(:overwrite_params => { :order_by => sort_by + "?direction=desc" :page => nil }), :class => 'selected save_pushstate')
...
I know what you're thinking. Just add :order into it. The problem being is that I 'm using an AJAX history saver from #175 of railscasts.
$(".save_pushstate").live("click", function() {
$.setFragment({"order_by" : $.queryString($(this).attr('href')).order_by});
//$.setFragment({"direction" : $.queryString($(this).attr('href')).direction});
return false;
});
And it rewrites my url to just one "fragment". I can't have two! So I decided that if I can just add the direction param in the href hard-coded, it could deal with this whole mess.
Try:
+ "?direction=desc".html_safe
Edit:
Since you're using rails 2.3.5, try this:
def search_sort(name, sort_by, order = 'asc')
link_to(name, url_for(:overwrite_params => { :order_by => sort_by + "?direction=desc" :page => nil }, :escape => false), :class => 'selected save_pushstate')
...
Note the ":escape => false" in url_for.
Edit2:
After reading this:
http://www.ruby-forum.com/topic/80381
Specifically this excerpt:
I think this is where the confusion is
arising. There are two different kinds
of escaping going on.
It sounds like you're talking about
the URL encoding that uses '%xx' to
represent special characters.
However, the html_escape function does
something completely different. It
takes a string and turns '&' into
'&' and '<' into '<', etc., so
that it can go into HTML without being
interpreted as literal '&'s and '<'s.
Escaping special characters in URLs
using the '%xx' scheme is mandatory,
otherwise they are not valid URLs.
I've realized that the 'escaping' that you see happening is url encoding, and it shouldn't affect your query/sorting, etc. You can test it out by taking the encoded url and typing it into your browser.
:escape => false disable html escaping, which means dangerous characters get converted to display codes, such as '&' into '&' and '<' into '<', etc.,
And the "?" in your append should be "&":
+ "&direction=desc"
Hope this helps. =)
I want to convert my XML document to Hash in Ruby/Rails. Actually, the default conversion by Hash.from_xml in Rails works for me except in one case.
I have a list of items contained in <item-list> element, these items can be of different types though. For instance, standard-item and special-item, each of which has different set of child elements.
<item-list>
<standard-item>
<foo>...</foo>
<bar>...</bar>
</standard-item>
<standard-item>
<foo>...</foo>
<bar>...</bar>
</special-item>
<special-item>
<baz>...</baz>
</special-item>
</item-list>
This XML structure can be confusing for Hash.from_xml as it does not know that both standard-item and special-item are both items and should be in the same level. Hence, from the above XML, the hash generated by Hash.from_xml will be:
{ 'item-list' => { 'standard-item' => [ { 'foo' => '...', 'bar' => '...' },
{ 'foo' => '...', 'bar' => '...' } ],
'special-item' => { 'baz' => '...' } }}
But what I want is to have all items as list members, like this:
{ 'item-list' => [ { 'standard-item' => { 'foo' => '...', 'bar' => '...' } },
{ 'standard-item' => { 'foo' => '...', 'bar' => '...' } },
{ 'special-item' => { 'baz' => '...' } } ]
Is it possible to extend/customize from_xml so that it performs to way I want to for this case? If it is not possible, what is the best way to achieve this? Given that this is the only element that contains something that deviates from general XML-to-Hash conversion, it does not seem right to me to implement the whole conversion routine where it might have already been implemented for a thousand times.
Another small note, Hash.to_xml also replaces all dashes with underscores. Is there a way to prevent this replacement?
This is correct behavior.
<a>
<b>one</b>
<b>two</b>
</a>
Think about how this would be converting to a hash, there cannot be two values assigned to the key 'b'. The only solution is to make the value of 'b' a hash containing an array of 'one' and 'two'.
{a => {
b => ['one', 'two']
}}
This is simply how Rails represents XMLs. You will need to check for an array value in the hash, and act accordingly.