When stretch attributes to multiline in Haml, I got an unbalanced brackets error - haml

The following code gives me unbalanced bracket error at line 4
%ul
- #sentences.each do |sentence|
%li
%a{id:"s-#{sentence.id}",
href:"/sentence/#{sentence.id}",
'data-type' => 'text',
'data-url' => "/sentence/#{sentence.id}",
'data-toggle' => 'manual'
}= sentence.content
%a{href:'#'}
%i.icon-pencil
Haml::SyntaxError at /user/1/sentence
Unbalanced brackets.
file: sentences.haml location: nil line: 4
any idea?

I found the problem myself, the closing } should not in a new line like any other languages
'data-toggle' => 'manual'}
= sentence.content
will work

I found other solution it may work for the others. According to the docs it is required to use | on multiline content. Of course closing bracket can't be in the new line. Example:
%a{id:"s-#{sentence.id}", |
href:"/sentence/#{sentence.id}", |
'data-type' => 'text', |
'data-url' => "/sentence/#{sentence.id}", |
'data-toggle' => 'manual'}= sentence.content

This one worked for me. Try adding a comma after your last key-value pair.
For instance
...
'data-url' => "/sentence/#{sentence.id}",
'data-toggle' => 'manual',
}
Note the , after 'manual'. This worked for me. Hope it helps someone.

Related

phpbb 3.2.x user_add including custom Profile field

This has been driving me nuts for 2 days and I can't find an answer anywhere on Google so would really appreciate a little help..
I have a custom registration form on my website, which sends the data to a fairly standard PHPBB3 user_add process as follows:
$user_row = array(
'username' => request_var('createUsername',''),
'user_password' => phpbb_hash(request_var('createPassword','')),
'user_email' => request_var('createEmail',''),
'group_id' => '2',
'user_timezone' => '1.00',
// 'user_dst' => '0',
'user_lang' => 'en',
'user_type' => $user_type,
'user_actkey' => $user_actkey,
'user_ip' => $user->ip,
'user_regdate' => time(),
'user_inactive_reason' => $user_inactive_reason,
'user_inactive_time' => $user_inactive_time,
);
// Register user...
$user_id = user_add($user_row, $cp_data);
// If creating the user failed, display an error
if ($user_id === false)
{
trigger_error('NO_USER', E_USER_ERROR);
}
That works fine and I'm happy with it, however, I have created a custom profile field in the Admin Control Panel called 'ea_real_name' which I want to hold the user's real name. This corresponds to a field on the registration form called 'createRealName' (sent through as $_POST['createRealName'])
I know that user_add takes an optional field called 'cp_data', but I can't for the life of me work out how to format this data... Should it be an array (something like 'ea_real_name' => request_var('createRealName','') or something else?
PHPBB's wiki for the field is empty (https://wiki.phpbb.com/Custom_profile::submit_cp_field) so not much help...
Thanks! :-)
I was right in my assumption! It's an array with the field name prefixed by pf_.
Finally found an answer here: https://www.phpbb.com/community/viewtopic.php?f=71&t=1638905
$cp_data = array(
'pf_ea_real_name' => request_var('createRealName','')
);
Is the correct way to do it...

Ternary operator not working in Haml

This question has been asked but the answers have not worked. The problem I am having is this hamlc code:
.UI_feed_item.deletable.clearfix{ :class => #feed.fav_post ? 'favorited' : '', feed_id: "#{#feed.id}", id: "feed_item_#{#feed.id}" }
*a lot more haml that doesn't have to do with this question*
the indentation is correct - it shows up weird on here
I want an extra class added to say favorited if feed.fav_post is true. for some reason it added a class 'true' or 'false' instead. I have also tried this:
.UI_feed_item.deletable.clearfix{ :class => (#feed.fav_post ? 'favorited' : ''), feed_id: "#{#feed.id}", id: "feed_item_#{#feed.id}" }
same result
I cannot do an if/else thing because there is no end in haml and I would have to rewrite a hundred lines of indented code. please help! none of the other solutions on the web have worked
Your second shot should work fine. The first variant returns true/false because the hash rocket wins over the ternary operator in terms of precedence - but shouldn't it break on syntax error after that?
You can do if-else in haml.
- if true
some stuff here
- else
some other stuff here
Indentation is used instead of end.
HAMLC doesn't support the ?/: ternary operators, but you can still achieve what you want using inline if/then/else. Try this:
.UI_feed_item.deletable.clearfix{ :class => "#{ if #feed.fav_post then 'favorited' else '' }", feed_id: "#{#feed.id}", id: "feed_item_#{#feed.id}" }

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.

Configuring rails database query so that blank string parameters are ignored

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)

scaml interpolation in html attributes

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}}