I have two Rails sites, Bargain Stock Funds (http://www.bargainstockfunds.com) and Doppler Value Investing (http://www.dopplervalueinvesting.com).
Bargain Stock Funds now has a search feature that allows users to obtain a list of funds meeting certain criteria. (The URL is http://www.bargainstockfunds.com/funds .) Doppler Value Investing also has a search feature that allows users to obtain a list of stocks meeting certain criteria. (The URL is http://www.dopplervalueinvesting.com/stocks/ .) Both sites use the Ransack gem to provide the search feature and the Kaminari gem to paginate the results.
Is there a way I can configure the search engines so that they are pre-loaded with certain criteria already set? For Bargain Stock Funds, I want the search engine pre-configured to exclude funds with a load_front or load_back value greater than 0.0%, and I want the results sorted with respect to the value of the pcf parameter. For Doppler Value Investing, I want the search engine pre-configured to exclude funds with a pass_legit value of false or a pass_quality value of false, and I want the results sorted with respect to the value of the dopeler_pb parameter. These pre-configured settings would save the user the trouble of having to hunt around for the criteria in the drop-down menus.
The source code for the search form for Bargain Stock Funds is:
<%= search_form_for #search, url: search_funds_path, method: :post do |f| %>
<%= f.condition_fields do |c| %>
<%= render "condition_fields", f: c %>
<% end %>
<p><%= link_to_add_fields "Add Conditions", f, :condition %></p>
<div class="field">
Sort:
<%= f.sort_fields do |s| %>
<%= s.sort_select %>
<% end %>
</div>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>
The source code for the search form for Doppler Value Investing is quite similar. (Just replace "funds" with "stocks".)
Not sure I'm completely following. By "search engine" do you mean Google? Or do you mean your applications searching logic?
If the latter, this may help: https://stackoverflow.com/a/14553363/1797331
I'll have to check out your sites. I was developing something similar, focused on stock fundamentals -- but haven't done much with it lately. You might inspire me.
Related
I'm using the cocoon gem in a Rails4 app to create a nested form
dynamically. Each Lease object my have zero or more MonthlyCharge
objects associated with it. (I.e., the Lease model has_many
:monthly_charges, and the MonthlyCharge model belongs_to :lease.)
I have a form for leases in app/views/leases/_form.html.erb that
calls monthly charges:
<h3>Monthly Charges</h3>
<div id="monthly_charges">
<%= f.fields_for :monthly_charges do |monthly_charge| %>
<%= render 'monthly_charge_fields', :f => monthly_charge %>
<% end %>
<div class="links">
<%= link_to_add_association "Add a Monthly Charge", f, :monthly_charges %>
</div>
</div>
But when I click on "Add a Monthly Charge", three sets of form fields
appear, rather than one.
On top of that, no matter if I fill out the form for one monthly
charge or not, and regardless of whether I click on "Remove Charge"
for the others, rails doesn't save the monthly charge.
Gist is
here.
What can I possibly be doing wrong? It was suggested that I might be
calling the cocoon js file more than once, but I've searched through
the source tree and haven't found any but a single reference to it, in
app/assets/javascripts/application.js. Any input appreciated. Thanks.
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.
Afternoon,
I'm finding Ransack a little confusing. I'm trying to populate my dropdown with only the values available and filter the index based on that but when I try with various options it pulls all the information into the dropdown including duplicates.
How can I create this form so that it shows only what is available and does the search based on that.
jobs_controller.rb
def index
#jobs = Job.all
#show_sub_nav = true
#q = Job.search(params[:q])
#searches = #q.result(distinct: true)
#lang = Job.find_by_sql("SELECT languages FROM jobs GROUP BY languages").map &:languages
end
_subnav.html.erb
<%= search_form_for #q do |f| %>
<%= f.select "languages", options_for_select(#lang) %>
<%= f.submit %>
<% end %>
I currently get this error:
No valid predicate for languages
Thanks for the help, I have real trouble trying to understand collection_select and these form_helpers as I don't find the API documentation very helpful so any links would be useful as well.
In Ransack, you need to add a predicate onto the field name. If you want to do an exact match against the option that is chosen you can do something like:
<%= f.select :language_eq, #lang %>
For more on predicates, see the Basic Searching page. Additional info is available in RailsCast 370.
I need to add filter search feature in my search form.
It's something like a social network. My app has societies, and a societies have categories (fashion, press, factory, ecc). I need my search to filter those categories.
Since I saw there are a plenty of search gems (solr, thinking sphinx, elasticsearch, sunspot) and look through all of them is a bit overwhelming have anybody accomplished such a task?
Any suggestion?
Things the search form should perform:
1) Search for a Society Name (And I can handle this with Sunspot)
2) Filter all Society by category chosen by user in a select tag (something like this)
3) And if both the text field and the select are filled, search for the name only in the category chosen
A great gem that I found recently is Ransack, which Ryan Bates has a RailsCasts for.
Here is an example of how it's used.
<%= search_form_for #query do |f| %>
<!-- filter: `society_name` contains text -->
<%= f.label :society_name_cont %>
<%= f.text_field :society_name_cont %>
<!-- filter: `category_id` equals -->
<%= f.label :category_id_eq %>
<%= f.collection_select :category_id_eq, Category.all, :id, :name %>
<% end %>
If you're unfamiliar with collection_select the Rails Guides will help <http://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease>.
I want to add drop down list with my own values. According to the given drop down list value i am going to do a search particular data. My drop down list should be contained trainer, venue, name and date.
I am working on ruby 1.9.2 and rails 2.3.8 versions and postgresql database adapter. I do not need to retrieve data from database. Drop down list should be contained with my given vales. How can i do this? Anybody plz help me....
You can simply add element as follows
Hear I assume there is a model call Entry and it contain two field call :name and :temperature. So I give this example to illustrate drop down menu for select temperature. It may have three values (Hot,Medium,Cold) and this is how we have to write the code
<% form_for #entry do |f| %>
<%= f.text_field :name %>
<%= f.select :temperature, [['Hot','hot'],['Medium','medium'],['Cold','cold']] %>
<%= f.submit %>
<% end %>