When I am searching for gender field say "male", it searches for "male" and "female" as "male" keywork is in "female" too.
So How can I use ransack in rails so that it matches for exact keyword instead of any partial group of chars.
PS:similarly how can I enable or disable the exact or partial key match in RANSACK gem.
I believe you could just do
<%= search_form_for #q do |f| %>
<%= f.label :gender_eq, "Gender:" %>
<%= f.search_field :gender_eq %>
<% end %>
that will tell the search to look for the exact string of your input
Here is some of the info from the developer on how it's used.
https://github.com/activerecord-hackery/ransack#in-your-view
I believe the way you do your search is <%= (field_name)_cont ... %> cont means contains.
What you want is _eq, change your view into <%= (field_name)_eq ... %> and it should work.
For more info:
https://github.com/activerecord-hackery/ransack/wiki/Basic-Searching
Related
By design, some classes will deal with only a subset of available languages.
the globalize-accessors gem is quite useful, however, the rendering requires that the following be defined
Class.globalize_attribute_names
so while available_locales = [:en, :ru, :fr, :de], the goal is to work with a smaller array [:en, :ru]
The documentation states Calling globalize_accessors with no options will therefore generate accessor methods for all translated fields and available languages. But the purported way to invoke is in the model
globalize_accessors :locales => [:en, :fr], :attributes => [:title]
How can the globalize_accessorsmethod refer to an array, something generated by the likes of
#post.owner.ownerlocales.pluck('locale')
(although the array values are quoted...)
A working solution found but that does not address the above question, is based on the fact that globalize-accessors
gives you access to methods: title_pl, title_en, title_pl=, title_en=
Thus, a controller method that generates a whitelist
#locales = []
#post.owner.ownerlocales.each do |ol|
locale = ol.locale
#locales.push(locale)
end
... then process in the view filtering out the globalize_processors from whitelist
<% Post.globalize_attribute_names.each do |lang| %>
<% my_string = lang.to_s %>
<% #locales.each do |locale| %>
<% checkstring = "_" + locale %>
<% if my_string.include? checkstring %>
<div class="row">
<%= t(lang[0..-4]) %> - <%= lang[-2, 2] %> <br />
<%= f.text_area lang, rows: "3" %>
</div>
<% end %>
<% end %>
<% end %>
Not efficient, functional.
I have a model called train that has 240 strings in it. Each string is simply named S1 through S240. I am wondering if there is a way to iterate over each string in a single record. I have a view that displays the strings. Some of the strings will have something in them and some will be empty and I just want to display the ones that are not empty in view. So I need an iterator or something to check each one and see if it is empty or not and display it if it's not empty.
I can do it using this:
<% if #train.s1 != nil then %>
<%= #train.s1 %><br>
<% end %>
Obviously I don't want to do that for 240 strings.
You probably can do something like iterating over the model's attributes.
<% #train.attributes.each do |attr_name, attr_value| %>
<% !if attr_value.blank? %>
<%= attr_value %>
<% end %>
<% end %>
This won't really work for you if you have more attributes than just those strings. Why don't you work with an association? Train has_many Strings and String belongs_to Train. So you could then iterate over #train.strings.each?
well in that case you can do like
<% (1..240).each do |index| %>
<% if #train.try("s#{index}") != nil then %>
<%= #train.try("s#{index}") %><br>
<% end %>
<% end %>
I am using gem rails-jquery-autocomplete, but It shows suggestions only after 2 letters, is there a possibility to display suggestions for 1-letter long searching?
Yes there is! Just add 'min-length' => 1 in your search form like:
<%= form_tag terms_path, method: 'get' do %>
<%= autocomplete_field_tag :search, params[:search], autocomplete_term_phrase_terms_path, 'min-length' => 1 %>
<%= submit_tag "Search" %>
<% end %>
I am trying to implement the acts_as_taggable_on gem. In my set up I have a Model called Discipline which is pre-populated with about 40 names.
I also have a Model Micropost which I want to tag - using a select box containing all the names in the disciplines database. I thought that I could call the acts_as_taggable_on the Model I wanted - in this case Disciplines but its not working with my current set up.
class Micropost < ActiveRecord::Base
acts_as_taggable
acts_as_taggable_on :disciplines
end
Here is the form......
<%= simple_form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.input :tag_list, :as => :select,
:multiple => :true,
:collection => ActsAsTaggableOn::Tag.pluck(:name) %>
<%= f.text_area :content, placeholder: "What is your question?", :style => "height:75px;" %>
<%= f.submit "Post", class: "btn btn-primary" %>
<% end %>
I can tell from the documentation that there is a way to do this....but I guess I am just not getting it. Any suggestions?
I don't think you can use acts_as_taggable_on using a model other than the default Tag and Taggings models.
Alternative Approach #1
Seed your database with the pre-populated 40 Tags containing your discipline names.
Alternative Approach #2
Use bitmask_attributes for your 40 disciplines.
For example, in my application I have:
bitmask :instruments, as: [:others, :guitar, :piano, :bass, :mandolin, :banjo,
:ukulele, :violin, :flute, :harmonica, :trombone,
:trumpet, :clarinet, :saxophone, :viola, :oboe,
:cello, :bassoon, :organ, :harp, :accordion, :lute,
:tuba, :ocarina], null: false
In my current solution, I am able to put a checkbox in the edit form so that users can delete attachment. However, there is no indication for the user that a file has been uploaded, the name of that file, etc. so that he can decide whether to delete.
Right now the form look like this. The first material is an existing one, the next 3 are due to
def edit
#post = Post.find(params[:id])
3.times { #post.post_materials.new }
end
As you can see, it's very hard to distinguish between them. Ideally, I want the first material file name to appear somehow.
<%= form_for #post, :html => { :multipart => true } do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
Materials:
<ul>
<%= f.fields_for :materials, :html => { :multipart => true } do |materials_form| %>
<li>
<%= materials_form.label :asset %>
<%= materials_form.file_field :asset %>
<%= materials_form.label :_destroy, class: "checkbox inline" do %>
Remove attachment <%= materials_form.check_box :_destroy %>
<% end %>
</li>
<% end %>
</ul>
<%= f.submit "Submit", class: "btn btn-large" %>
<% end %>
Running paperclip's generator creates a migration to add 4 attributes on your model, as you can see here. These attributes are:
<attachment>_file_name
<attachment>_file_size
<attachment>_content_type
<attachment>_updated_at
So, If you ran the generator this way: rails generate paperclip post_material asset, on your PostMaterial model, you will have these attributes:
asset_file_name
asset_file_size
asset_content_type
asset_updated_at
Then, on your code you can do something like this:
if materials_form.object.asset.exists? #object represents the current post_material instance
#show a label with object.asset_file_name
else
#render materials_form.file_field :asset
end