I am using Rails TextHelper "highlight" method for highlighting a search string.
<%= simple_format highlight("Some test text", "some") %>
However, it seems to be case-sensitive. Is there a case-insensitive version of this function? Or maybe some option i'm missing?
Du-oh!
It turns out, Highlight method is already case insensitive.
I had a check before the line to see if the string includes the query text. I used the ruby .include? method for that which is not case sensitive!
<% if query && faq.answer.downcase.include?(query.downcase) %>
<%= highlight(excerpt(faq.answer, query, :radius => 100), query) %>
<% else %>
<%= truncate(faq.answer, :length => 200) %>
<% end %>
Using a downcase on the include? check made it work.
Related
I'm trying to move from Font Awesome to icomoon.
With Icomoon I can get the icons to work using the following syntax
<span data-icon="" aria-hidden="true"></span>Some Text
However, as I'm using a rails app I'd really prefer to use the following syntax, or something similar.
<%= link_to icon_tag("icon-pdf", "some text"), controller_path %>
I have tried the following as well, all to no avail
<%= link_to 'Some text', controller_path, {"data-icon" => "", "aria-hidden" => "true"} %>
It doesn't matter if I put the defined name (icon-pdf) or its hex value in there, but I can't seem to get the icon to appear.
Is there a way I can achieve this, or am I stuck with the data-icon method?
Try with this:
<%= link_to controller_path do %>
<span data-icon="" aria-hidden="true">Some Text</span>
<% end %>
Or
<%= link_to content_tag(:span, 'Some Text', :data_icon => "", :aria-hidden => "true" ), controller_path %>
It should work. Thanks
I was curious: in an ERB file, when passing a block to a view helper, why does this work:
<%= div_for #thing do |x| %>
<%= x %>
<% end %>
while this doesn't?
<%= div_for #thing {|x| x.to_s} %>
In Ruby, do...end is exactly the same as {...}, so why not in ERB? Note aside: I can use x on its own on the second line above because its .to_s method returns the field I want to render. Sorry if this has been asked before, I wasn't able to find a similar question (found a similar answer though).
I suspect that the second block of code is exactly the same as:
<%= div_for #thing do |x| %>
<% x %>
<% end %>
Because the x doesn't have a "=" on it, it's not going to be output in your view.
Try :
<%= div_for #thing {|x| concat x.to_s} %>
If there are semantic errors in the form (mostly from external API), I'd like to add an explanatory message, like so:
<%= semantic_form_for #order, :url => checkout_purchase_url, :html => {:class => 'payment'}, :wrapper_html => { :class => "field" } do |f| %>
<% if f.has_errors? %>
<p>There were errors that prevented your order from being submitted. If you need assistance, please contact us toll-free at <strong>1-800-555-5555</strong>.</p>
<%= f.semantic_errors %>
<% end %>
<% end %>
However, has_errors? is a protected method. Is there a way that I can do this? Thanks.
If you have nested attributes you won't see any errors associated with them. To ensure you get all base errors and any nested attributes errors. Make sure your model contains:
validates_presence_of :nested_object
validates_associated :nested_object
and in your form:
f.semantic_errors *f.object.errors.keys
Not as hard as I thought. I fixed it by checking for errors on the object instead of the form:
<% if #object.errors.any? %>
<p>There were errors that prevented your order from being submitted. If you need assistance, please contact us toll-free at <strong>1-800-555-5555</strong>.</p>
<%= f.semantic_errors %>
<% end %>
Thanks for those who viewed.
For completeness, here's an alternative approach if you want to show similarly helpful messages on each field:
= f.label :title
- if f.object.errors.any?
.error = f.object.errors[:title].flatten.join(' and ')
= f.text_field :title
This gives a nicely formatted and easily-styled list of errors for each field. (You can use semantic_errors instead of object.errors if you prefer, same result.)
I have a text_area in a partial in a complex form that is called like so
<%= f.fields_for :notes do |notes_form| %>
<%= render :partial => 'note', :locals => {:f => notes_form, :operation => f, :count => operation.notes.count} %>
<% end %>
<p><%= add_child_link "Add note", :operation_notes %></p>
and the partial looks like this
<% count ||= 2 %>
<div class='fields'>
<%= f.text_area :note_text, :rows => "4", :class => "notes" %>
<%= remove_child_link "x", f, count %>
</div>
There can be many notes on the form hence the add and remove child links.
The issue I'm having is that if I add a note with the text 'abcd', when I bring up the edit form I get '<p>abcd</p>'. If there are line breaks in the note it adds <br /> tags. The text_area form helper seems to be using the simple_format helper but I have no idea why. Can anyone help as this is very undesirable behaviour?
Ah solved,
Earlier on the same page I was displaying the note and using simple_format to format it with
<%= simple_format note.note_text %>
It seems that simple_format is somewhat destructive as after this, a call to note.note_text always returns the formatted text. If I change the above to
<%= simple_format note.note_text.dup %>
then the note_text attribute is not altered and I get the appropriate results.
I will have to look more closely at simple_format but this really strikes me as undesirable behaviour.
EDIT
It looks like this has been corrected in Rails 3.1
I would suspect that you have something in your Note model that is processing the text. Check for callbacks in this model.
I have a partial which generates a div with some form fields in it. It uses the form builder variable "f" which is provided as input to correctly name the fields in the parameter has (fields are actually nested attributes, so the name is like "[author][book][0][title]").
I want to use that same partial when receiving an AJAX call to regenerate the div based on new user information. I am currently using <% form_for ... |f| %> in my erb file, but that generates a warning that "<% %>" is deprecated.
My erb file looks like the following:
<% if f.nil? %>
<% form_for(#author, :id => :coupon_form) do |f| %>
<%= render "books_detail1", :f => f %>
<% end %>
<% else %>
<%= render "books_detail1", :f => f %>
<% end %>
So what is the correct way to create a form builder context while discarding the generated HTML?
The correct answer is to use fields_for. It generates the same form builder object without the html. I lost track of this in it's use for sub-forms, but it's really the same thing.