Ransack search for price with Money-rails - ruby-on-rails

I'm using Ransack to add a search function to my rails app and I'm also using Money-rails gem to handle price and currency.
I want to enter a price in Dollars in the Ransack search field and then it converts it to cents in order to find it in the relevant table.
So basically the problem is how to convert the input form Dollars to cents before submitting the search with Ransack.
Search form:
<div class="form-group">
<%= f.label :price_cents_gteq, "Price between" %>
<%= f.text_field :price_cents_gteq %>
<%= f.label :price_cents_lteq, "And" %>
<%= f.text_field :price_cents_lteq %>
</div>
Thanks a lot in advance!

Custom predicate can solve the problem.
You can use formatter proc to convert dollar to cents. Problems can arise when you will display ransack object value of price.
Update #1
Or even better, you can solve it with ransacker class method.
Update #2
Here is the raw solution with model and ransaker method:
# model
ransacker :price_money, type: :integer, formatter: proc { |dollars| dollars * 100 } do |p|
p.table[:price_cents]
end
# form in view
<%= f.number_field :price_money_gteq %>
<%= f.number_field :price_money_lteq %>

Related

Ransack search by a selected column dynamicaly

i want to do a search with Ransack in a User model, but i need a select with the attributes of the model as options to perform the search for that model. So if i have as attributes name, last_name, email the select should have this fields to be selected and next to it an input field to write the text that i want to search in the selected column or attribute for the User model.
Is there a way to do it with Ransack in Rails 4.1?
I think that could be something like this
<%= search_form_for #q do |f| %>
<%= f.select :selected_field, collection_of_attributes %>
<%= f.search_field :user_selected_field_cont %>
<%= f.submit %>
<% end %>
Any help will be appreciated :)
Try using ransack's attribute_select. http://railscasts.com/episodes/370-ransack?view=asciicast

Build select date and year inputs with simple_form

In my Ruby on Rails application I have some form that needs inputs to select month and year. I do this with built in Rails select_date input:
= select_date #date, :order => [ :month, :year], :discard_day => true
But now I want to know is there any way to the same easy with simple_form?
Obviously an old question but the answer is YES, you absolutely can do the same with simple_form. Posted for others looking for this answer.
You'll pass date as the input type and then rather like the select_date form_for input helper, you'll pass discard_day
So it might look like this:
= f.input :your_date_field, as: :date, discard_day: true
Hello read a this documentation
1.4 Other Helpers of Interest
Other form controls worth mentioning are textareas, password fields, hidden fields, search fields, telephone fields, date fields, time fields, color fields, datetime fields, datetime-local fields, month fields, week fields, URL fields, email fields, number fields and range fields:
<%= text_area_tag(:message, "Hi, nice site", size: "24x6") %>
<%= password_field_tag(:password) %>
<%= hidden_field_tag(:parent_id, "5") %>
<%= search_field(:user, :name) %>
<%= telephone_field(:user, :phone) %>
<%= date_field(:user, :born_on) %>
<%= datetime_field(:user, :meeting_time) %>
<%= datetime_local_field(:user, :graduation_day) %>
<%= month_field(:user, :birthday_month) %>
<%= week_field(:user, :birthday_week) %>
<%= url_field(:user, :homepage) %>
<%= email_field(:user, :address) %>
<%= color_field(:user, :favorite_color) %>
<%= time_field(:task, :started_at) %>
<%= number_field(:product, :price, in: 1.0..20.0, step: 0.5) %>
<%= range_field(:product, :discount, in: 1..100) %>
http://guides.rubyonrails.org/form_helpers.html
and more example examples here:
http://apidock.com/rails/ActionView/Helpers/DateHelper/date_select

Rails 4 Combining a Date_field and Time_field into single datetime attribute on active record model

I am trying to combine a date_field and a time_field and not date_select and time_select as I do not want 5 separate input fields
I have a school course model with a start_date attribute. This is the form as near as I would like have it.
<%= form_for #school_course do |f| %>
<%= f.label :start_date %>
<%= f.date_field :start_date %>
<%= f.submit %>
<% end %>
To pass both of these fields through to the model I am wondering do I have to pass them as an array and the manually stitch them into a single datetime object? Or is there a more rails way to combine a date and time input field?

Rails and date_select

I am trying to use date_select with some of my forms.
<div class="field">
<%= f.label :begin_date %>
<br/>
<%= f.date_select(:begin_date,:start_year => Time.now.year) %>
</div>
Reading through the documentation, I was expecting to get three select tags. What I got was: begin_date(1i), begin_date(2i), begin_date(3i)
If I include these in my model:
attr_accessible :begin_date(1i), :begin_date(2i), :begin_date(3i),
The IDE does not like it. What am I doing wrong? Also, is there a date select method that would return the values in a single tag, instead of 3?
You can just do attr_accessible :begin_date, and it will stitch together the date components into a single Date value for you.

Rails: Human-friendly date format for text_field

I have a birth_date field that is stored as a datetime value. The default rails form helpers spit out a not-too-friendly format, e.g. "2008-06-10 22:33:19.000000". The below is the vanilla rails way.
<div class="field">
<%= f.label :birth_date %>
<%= f.text_field :birth_date, :size=>"20" %>
</div>
How can I simply apply a format? I tried various approaches, for example strftime should work, I thought. But when I try the following, I get an error undefined method 'strftime' for nil:NilClass
<div class="field">
<%= f.label :birth_date %>
<%= f.text_field :birth_date, :value=>f.object.birth_date.strftime('%m/%d/%Y'), :size=>"20" %>
</div>
Based on some other questions/answers, I tried the following. It works for non-null values, but it is ugly code, and it doesn't work for blank values (actually it shows today's date).
<div class="field">
<%= f.label :birth_date %>
<%= f.text_field :birth_date, :value=> Time.parse(f.object.birth_date.to_s).strftime('%m/%d/%Y'), :size=>"20" %>
</div>
In playing around, it seems that outputting f.object.birth_date is treated as a date, rather than datetime. However, when displayed in the text_field (original, ugly formatting), it includes the time. It is friday afternoon, and my combined lack of familiarity with rails forms and ruby date/time objects is making me feel foolish.
Any simple way to get my form to display nothing if null/blank, and a friendly date otherwise?
If you want a blank string if birth_date is empty, you should simply check that birth_date is non-nil beforehand:
<%= f.text_field :birth_date, :value => (f.object.birth_date.strftime('%m/%d/%Y') if f.object.birth_date), :size => "20" %>
This way, when birth_date is nil, :value gets set to nil.
Have you tried using the date_select form builder helper method? You can also use datetime_select, but it looks like you just want to work with the date here.
<div class="field">
<%= f.label :birth_date %>
<%= f.date_select :birth_date %>
</div>
The API docs are here: http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-date_select
Add on the options you need.
I've always used that approach, or a javascript date selector like this one from jQuery UI. There are plugins for most JS frameworks these days.
If it MUST be a text_field, use a human language date parsing library like chronic. It'll work, but will require that you parse the input from the form somewhere before applying the attribute to your object.

Resources