Rails: Ransack and dates and columns on other tables - ruby-on-rails

I am using Ransack in my RoR application and so far most of how I have been using it has been simple. But I now have two issues with Ransack that I need help on.
I am trying to search on dates on my holds table. The holds table has two date fields. lowdate and highdate. I have the following jQuery code in my holds.coffee file for the jQuery datepicker to ensure that the date format is in the mm/dd/yy format. The following is an example of the lowdate, but I have another for highdate.
jQuery -> $(document).on "page:change", ->
$("#lowdate").datepicker
changeMonth: true
changeYear: true
dateFormat: "mm/dd/yy"
yearRange: "-5:+10"
It seems that the date is being passed in the mm/dd/yy format in the search critera and I think it needs to be in the yy-mm-dd format. Not sure how to do that for searches though.
My ransack search code in my index.html.erb file:
<%= search_form_for #search do |f| %>
<div class="span2 input">
<% f.label :holdfrom_gteq %>
<%= f.text_field :holdfrom_gteq, placeholder: "Low Date", id: "lowdate" %>
</div>
<div class="span2 input">
<% f.label :holdto_lteq %>
<%= f.text_field :holdto_lteq, placeholder: "High Date", id: "highdate" %>
</div>
<div class="span1">
<%= f.submit "Search", name: nil, class: "btn-small btn-primary"%>
</div>
<% end %>
And this is in my log file:
Parameters: {"utf8"=>"√", "q"=>{"holdfrom_gteq"=>"05/25/2014", "holdto_lteq"=>"05/31/2014"}}
My next problem involves searching on items that are not in the current table. For example, my hold table has a customer_id linking a hold back to a customer. I want to be able to search on customer_name (field in the customers table) but have no idea how to do that since customer_name is not part of the holds table.
Any help that can be provided to the above two issues would be much appreciated. Still relatively new to rails and still learning. I did watch the railscast #370 for Ransack but it doesn't touch on either of the two items above.

Ok, in the end the answer to this wasn't as difficult as I thought it would be.
Since I had a relationship between holds and customer, a customer can have many holds, then all I needed to do was prefix the search variable with the model name.
So, if I wanted to search on customer name I would have the variable something like this:
customer_first_name_cont
Don't recall what the exact issue was with the dates, but they are working fine too.

Related

Showing previously checked records marked 'inactive' in edit form with collection_check_boxes

Rails convention uses the same _form.html.erb form for both creating a new record, and for editing an existing record. This is causing an issue with an f.collection_check_boxes section in my form because I am attempting to distinguish between active and inactive selections.
For creating a new record, I want to display only those checkbox selections that have the active attribute marked as: true.
Editing an existing record is different. Like for new records, I want to display all checkbox selections whose active attribute is marked as true. But I also want to show any already existing checked selections, even if the active attribute is marked as false.
Here is a tangible example. I have companies, languages, and companies_languages. The companies_languages table is a rich join table, representing language interpreters that this company has on staff.
<div class="row">
<div class="col-sm-12">
<div class="field">
<%= f.label :companies_languages, "What Language Translators are on Staff?" %><br>
<%= f.collection_check_boxes("language_ids", Language.active, :id, :name ) do |language| %>
<div class="row">
<div class="col-xs-1">
<%= language.check_box(class: "check_box") %>
</div>
<div class="col-xs-11">
<%= language.object.name %>
</div>
</div>
<% end %>
</div>
</div>
</div>
This works great when creating a new company and choosing from all active languages in order to specify the language interpreters the company has on staff.
However, it does not work great for editing a company. Any languages this company has interpreters for whose active attribute is marked false is simply not shown. This makes it not possible for the user to remove those inactive interpreters, or to see them at all in the edit form.
Any tips for solving this would be appreciated.
Figured it out thanks to some help:
...
<%= f.collection_check_boxes("language_ids", Language.active + #company.languages.inactive, :id, :name ) do |language| %>
...
This works for the #agency that is new because it doesn't have any languages.inactive. It also works for #agency when we edit it because it grabs all the active agencies, but also shows any inactive languages that already exist.

Rails Year Make Model Dynamic Menus

Calling all Rails superheroes! I'm racking my brains with this one and can't find a solution anywhere online...I want to build year/make/model drop-downs to select a car from a table of 30,000+ different cars. The table is called car_variants. Here's a link to a similar thing on kbb.org
The Year is selected first which then populates the Make drop-down with those that were available in that year. This in turn populates the Model drop-down. All this filtering overcomes the dreadful problem of having the 30,000 car_variants in one unusable drop-down.
Unfortunately I don't have enough reputation to post my entity diagram, but essentially...
a car_make has many car_models
a car_model has many car_variants
a car_year has many car_variants
SIDE NOTE Since the car_variants table will be populated by importing an excel spreadsheet, I decided to join car_year to car_variants directly rather than saying a car_year has many car_models. That may or may not affect the challenge below.
The car_variants model ( a table of virtually every car since 1984) stores the car_year_id and the car_model_id. (car_make is obvious through car_model)
Now... along comes the user. They want to select their car from the car_variants table then add their car's nickname and license_plate. The user potentially has many cars so the car_variant_id's can't be stored in the user table. Instead we have a car table which contains the users cars.
a car_variant has many cars (think of a car as being like an instance of a car_variant)
a user has many cars
Ryan Bates did an excellent screencast about Dynamic Select Menus. In his example (Countries and States), he used grouped_collection_select and some javascript. That certainly appears to be heading in the right direction for this challenge too, but this is more complex because there are more has_many relationships than grouped_collection_select seems to be able to handle.
I have successfully coded the car form with 3 drop-downs for year, make and model...
<%= simple_form_for(#car) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<!-- select year to filter make dropdown -->
<p>Year</p>
<%= select_tag :car_year, options_for_select(CarYear.all.collect {|e| ["#{e.year}", e.id] }), :include_blank => true %>
<!-- Select make to filter model dropdown -->
<p>Make</p>
<%= select_tag :car_make, options_for_select(CarMake.all.collect {|e| ["#{e.name}", e.id] }), :include_blank => true %>
<!-- Select model -->
<p>Model (grouped collection select)</p>
<%= f.grouped_collection_select :car_variant_id, CarMake.order(:name), :car_models, :name, :id, :name, include_blank: true %>
<%= f.input :nickname, :hint => "If you've given your car a nickname enter it here, otherwise just leave it blank." %>
<%= f.input :plate %>
<%= f.hidden_field :user_id, :value => current_user.id %>
</div>
<div class="form-actions">
<%= f.button :submit, "Add Car" %>
</div>
<% end %>
...now how do I tie them all together so they filter one another then return the car_variant_id to be stored in the car_form.
After several late bleary nights, I'm out of ideas.

How to use the date_select and time_select in the form in rails?

I have a Date and Time column separately in the table in my database. When i click the Edit in the table, the Date and Time should display the values by params. The User can change the Date and the Time and update the form.
I tried the following:
<div class="field">
<%= f.label :Sent_Date, 'Sent Date:' %>
<%= date_select(:Appointment_Date, object: f.object) %>
</div>
<div class="field">
<%= f.label :Appointment_Time, 'Appointment Time:' %>
<%= f.time_select(:history, :Appointment_Time, include_seconds: true) %>
</div>
It's not display the values correctly and it doesn't change to next record. It display to select the value. Do I need to convert the date here? Because, In the Database, its just the value like 2013/05/13 and in the field its like 20 December 2013
Time field is not working. Please suggest me how to get solve this issue.
Thanks
Don't use constants as fields for your model. Any variable starting with a capital letter will be treated like a constant, and constants are meant not to change. Ruby will give you a warning: already initialized constant when trying to change a constant's value, so that might be the reason you can't change the time and date.

RoR 3.2.5 Railscasts #102: Autocomplete: Form is prepopulated with data (should be blank)

I'm going off Railscasts 102.
My current issue is that my search form is already populated with data when the page loads.
I have one model, listing, and 5 columns on that model: website, url, comments, doctor, and date. In this case I am just worrying about website.
My listings.js.coffee is
jQuery ->
$('#search').autocomplete
source: $('#search').data('autocomplete-source')
And my index.html.erb file (the important part) is
<%= form_tag '/listings', :method => :get do %>
<%= text_field_tag :search, data: {autocomplete_source: Listing.order(:website).map(&:website)} %>
<%= submit_tag 'Search', :class => 'echo-search-tag', :name => nil %>
<% end %>
When I load the page, the search form is already populated with
{:data=>{:autocomplete_source=>["Doctors4US", "How To Get Better", "Rafael's Epic Doctor Site", "Testing Admin", "WebMD"]}}
Obviously this shouldn't be the case. The JS console is giving me an error of:
Uncaught TypeError: Property 'source' of object # is not a function
I saw another Stack Overflow post in regards to this, I tried that solution but it didn't make a difference.
Note: when I tried test data for the source in listings.js.coffee, like ['foo', 'fore', 'food'], the autocomplete was working correctly.
Also note that the search function is working correctly aside from autocomplete.
Any input is appreciated :)
Ok the issue was that the text_field_tag takes 2 arguments, and that is why the search form was being prepopulated with the data.
So I just added nil for the 2nd argument
<%= text_field_tag :search, nil, data: {autocomplete_source: Listing.order(:website).map(&:website)} %>
And voila! Working.
Now trying to figure out how to add multiple columns to Listing.order and .map (ie, having not only :website, but :comments, :doctor, and :url). Input appreciated!

Posting to database from dropdown menu Rails

I have created a dropdown menu that is pulling Cities and Countries from two database tables (named citie and country).
I am using the following collection_select tag:
<section class="field">
<%= f.label :city %>
<%= f.collection_select(:id, Citie.all, :id, :city) %>
</section>
But when I submit my form nothing is being posted into my jobs table (the form is to generate a new job).
I have searched to find a solution for this and am sure I am just missing a small part but can't seem to figure out what it is and why it's not working.
Any advice and a solution would be much appreciated! Thanks
I'm not terribly familliar with collection_select but shouldn't you give the association name as the first argument? e.g.
<%= f.collection_select(:city, Citie.all, :id, :city) %>

Resources