That successfully removes column names that aren't created from foreign keys.
Let's say there's a product model that has many photos, then a has photos option will appear in the drop down, do you know how to remove it? Thank you.
Datatables is so much cleaner and provides multi-column searching. I have also created custom classes to define how datatables searches using some of the examples from one of your previous railscasts Ryan. Sometimes when it aint broke, don't fix it. :)
can you please explain how multicolumn search? i implemented the code of ryan from episode 340 and i have problem to implement multi column seach. if you can also post some code it will be really awesome.
thanks in advance
I ended up creating a library to expedite the creation of various fields using Ransack that I obviously think is worth checking out: https://github.com/jbarket/facet_for
Basically, you call f.facet_for :field_name in your search form, and it takes a smart guess, which you can then supplement. It was really created for a large project at work, so the documentation may not be what you'd call extensive... but neither is the documentation for ransack.
Is it possible to Create Ransack::Search object without querying DB ?
I'd like to be able to create a Ransack::Search object to be passed into a search_form_for, but the initial creation of the search object queries the database, wich I don't want.
I want to show an initial blank form with Ransack::Search options to search, without calling the database.
"...This works well but if the user adds too many conditions they might run into the limit of the data that can be sent over a GET request. One way around this problem is to use POST instead so we’ll implement this..."
First of all thanks for the great Episode. But I have two problems:
1) The Dropdown List of how to filter is abbreviated. As example it doesn't say "greater than or equal", but instead says "gte" and I didn't find any configurations on how to change this.
2) And this is a real problem: The javascript code won't work. I can only remove a field, but not add one. So I will always have at least one field, but I can never add one. I tried the Coffeescript code with Barista in my 3.0 app and also translated it to jQuery and tried if with that, but it never worked. Any ideas?
So, I now tried everything possible and the only thing that worked was upgrading to Rails 3.1 or newer. So my recommendation for everyone using 3.0.15 or lower would be not to try this Railscast as it seems not to work. The problem must be somewhere with the render command in the link_to_add_fields method.
As a gotcha, if you've followed any other of the railscasts on searching (e.g. Search, Sort, Paginate with AJAX remember to remove the private def self.search methods from the controllers!
Ransack gem help for searching data but searchable data is not displayed on PDFs but every-time it displays only the result data got from result method in ransack .When we search data next time and click on PDFs link next time,we get the same previous result but not the searched data.
I am sucked with this problem.
Please guide me.
Thanks and regards.
If anyone else finds this useful, I wanted to do all I could to avoid using a POST for my various search forms, so I created a gem that allows abbreviations of column and association names in the Ransack queries: Ransack Abbreviator
"SELECT \"users\".* FROM \"users\" LEFT OUTER JOIN \"profiles\" ON \"profiles\".\"user_id\" = \"users\".\"id\" WHERE (((\"profiles\".\"last_name\" ILIKE '%jane%' OR \"profiles\".\"last_name\" ILIKE '%doe%') OR (\"profiles\".\"first_name\" ILIKE '%jane%' OR \"profiles\".\"first_name\" ILIKE '%doe%')))"
which returns the expected results... But how to implement this request in my search form (in my view) and/or in my controller ?
"SELECT \"users\".* FROM \"users\" LEFT OUTER JOIN \"profiles\" ON \"profiles\".\"user_id\" = \"users\".\"id\" WHERE (((\"profiles\".\"last_name\" ILIKE '%jane doe%') OR (\"profiles\".\"first_name\" ILIKE '%jane doe%')))"
but how to implement the split(' ') to have the expected SQL ?
Any help (and a bit of explanation) would be great, documentation is a bit light on the subject...
Cheers
Thank you for the screencast. I'm having an issue with the added condition fields disappearing after every post. My controller and routes code look exactly like Ryan's. I'm using the control-group and controls classes from Twitter bootstrap for form-horizontal layout. Is Ransack looking for fieldsets/fields classes by default?
When using the predicates :true or :false, the user shouldn't have to put anything into the value field, however the condition is dropped when you hit search. Any workaround for this?
If you are having a problem with the dynamic add/remove javascript not working I found a solution. The problem for me was the rake gem. My solution was to require rake 0.9.2.2 in gemfile then run "bundle update" to rebuild gems from scratch.
Is there a way of discounting certain fields in the drop down as i have a lot of foreign keys in the table that I want to search on?
If you add this to the model, then these items will no longer appear on your dropdown list.
However, if you remove exclude an item using the list above, it will no longer become sortable.
That successfully removes column names that aren't created from foreign keys.
Let's say there's a product model that has many photos, then a has photos option will appear in the drop down, do you know how to remove it? Thank you.
Thank you this was very helpful
Datatables is so much cleaner and provides multi-column searching. I have also created custom classes to define how datatables searches using some of the examples from one of your previous railscasts Ryan. Sometimes when it aint broke, don't fix it. :)
can you please explain how multicolumn search? i implemented the code of ryan from episode 340 and i have problem to implement multi column seach. if you can also post some code it will be really awesome.
thanks in advance
I ended up creating a library to expedite the creation of various fields using Ransack that I obviously think is worth checking out: https://github.com/jbarket/facet_for
Basically, you call f.facet_for :field_name in your search form, and it takes a smart guess, which you can then supplement. It was really created for a large project at work, so the documentation may not be what you'd call extensive... but neither is the documentation for ransack.
Three items to add: twitter bootstrap inline form, non-text fields, reset form
with CoffeeScript to handle the reset button:
+1
How do you go about localizing the predicate fields?
Does anyone know how to use Geocoder with Ransack?
Searching within a certain area, etc?
Hi einstyne,
I answered this question on Stackoverflow recently, here's how I did it.
Anyone know if it's possible to control which predicates are available to the user in the dropdown? I'd like to only let my users choose from a few.
Is it possible to Create Ransack::Search object without querying DB ?
I'd like to be able to create a Ransack::Search object to be passed into a search_form_for, but the initial creation of the search object queries the database, wich I don't want.
I want to show an initial blank form with Ransack::Search options to search, without calling the database.
How can I do that ?
Asked in stackoverflow
Is there a reason why you're using POST for a search form?
Ryan explains...
"...This works well but if the user adds too many conditions they might run into the limit of the data that can be sent over a GET request. One way around this problem is to use POST instead so we’ll implement this..."
Great episode, thank you Ryan!
First of all thanks for the great Episode. But I have two problems:
1) The Dropdown List of how to filter is abbreviated. As example it doesn't say "greater than or equal", but instead says "gte" and I didn't find any configurations on how to change this.
2) And this is a real problem: The javascript code won't work. I can only remove a field, but not add one. So I will always have at least one field, but I can never add one. I tried the Coffeescript code with Barista in my 3.0 app and also translated it to jQuery and tried if with that, but it never worked. Any ideas?
Okay, I found the solution to my first problem. I didn't include the .yml translation file, which you can see here:
https://github.com/ernie/ransack/blob/master/lib/ransack/locale/en.yml
But I still haven't figured out why the add_field doesn't work...
So, I tried almost everything I know and still can't get the add_field javascript to work. Barista translates it to the following code:
And Firebug gives me this error:
TypeError: $(this).data("fields") is undefined
I would really appreciate any help or hints.
So, I now tried everything possible and the only thing that worked was upgrading to Rails 3.1 or newer. So my recommendation for everyone using 3.0.15 or lower would be not to try this Railscast as it seems not to work. The problem must be somewhere with the render command in the link_to_add_fields method.
I am pretty sure I had the same problem. In your _condition_fields.html.erb did you make sure to surround everything with:
It is triggering on that div I believe.
As a gotcha, if you've followed any other of the railscasts on searching (e.g. Search, Sort, Paginate with AJAX remember to remove the
private def self.search
methods from the controllers!I'm struggling also with the point of limiting the predicates to some possibilities.
I tried out:
<%= f.predicate_select :only => ['eq'] %>
but this didn't work. Unfortunately, the documentation seems not to help.
try
<%= f.predicate_select({only: [:eq]}) %>
or in your case
Ransack gem help for searching data but searchable data is not displayed on PDFs but every-time it displays only the result data got from result method in ransack .When we search data next time and click on PDFs link next time,we get the same previous result but not the searched data.
I am sucked with this problem.
Please guide me.
Thanks and regards.
In the ASCIIcast when you move the condition fields into it's own partial. The header for that snippet still says:
/app/views/products/index.html.erb
But it should say:
/app/views/products/_condition_fields.html.erb
Thanks for an awesome episode,
Andy
Thanks for reporting that, I've updated the ASCIIcast to show the correct filename for the partial :)
Anyone know how to turn the textfield into a dropdown when a association is chosen as an attribute?
i.e. When you want to search by Category, is there a way for the textfield to turn into a populated drop-down?
Are you still looking on how to do this?
is this causing webrick to become unresponsive for anyone else?
Is anyone else having issues where they can only get the search functionality to work within the index action only?
How can we implement Ransack gem with Datatables gem?
How do I reverse the order of a dropdown list in my search form:
I am using:
<%= f.select :project_id_eq, options_from_collection_for_select(Project.all, "id", "name", @search.project_id_eq) %>
If anyone else finds this useful, I wanted to do all I could to avoid using a POST for my various search forms, so I created a gem that allows abbreviations of column and association names in the Ransack queries: Ransack Abbreviator
Hi
I am trying to use will_paginate with ransack.
controller
@search = Style.search(params[:q])
@styles = @search.result(:distinct => true).paginate(:page => params[:page], :per_page => 5)
index
<%= will_paginate @styles %>
Rest of the code is same as in this tutorial. But pagination links are not working. Really stuck with it. Please help.
Make this change in routes. And it works.
resources :styles do
collection do
match 'search' => 'styles#index', :via => [:get, :post], :as => :search
end
end
@rvsingh, thanks appreciate this, i owe u a beer
Anyway to convert the sort_links into bootstrap buttons?
I managed to apply a form-inline class to the search_form_for thanks to Tom Harrisson, thanks!:
<%= search_form_for @search, :html => {:class => "form-inline"} do |f| %>
Any idea how to apply a css custom button to:
<%= sort_link @search, :title, "Title" %>
Thankyou
Hello
Need some help on this
I wiould like to do something like that :
the SQL generated is the following
"SELECT \"users\".* FROM \"users\" LEFT OUTER JOIN \"profiles\" ON \"profiles\".\"user_id\" = \"users\".\"id\" WHERE (((\"profiles\".\"last_name\" ILIKE '%jane%' OR \"profiles\".\"last_name\" ILIKE '%doe%') OR (\"profiles\".\"first_name\" ILIKE '%jane%' OR \"profiles\".\"first_name\" ILIKE '%doe%')))"
which returns the expected results... But how to implement this request in my search form (in my view) and/or in my controller ?
My search form is :
As it is the SQL returned is
"SELECT \"users\".* FROM \"users\" LEFT OUTER JOIN \"profiles\" ON \"profiles\".\"user_id\" = \"users\".\"id\" WHERE (((\"profiles\".\"last_name\" ILIKE '%jane doe%') OR (\"profiles\".\"first_name\" ILIKE '%jane doe%')))"
but how to implement the split(' ') to have the expected SQL ?
Any help (and a bit of explanation) would be great, documentation is a bit light on the subject...
Cheers
Thank you for the screencast. I'm having an issue with the added condition fields disappearing after every post. My controller and routes code look exactly like Ryan's. I'm using the control-group and controls classes from Twitter bootstrap for form-horizontal layout. Is Ransack looking for fieldsets/fields classes by default?
Thanks for any help.
Very useful. Thank you
Seems to be ignoring scopes. When I want to view a scoped result set and apply a Ransack filter, it views the unscoped results...
def my_queue
@search = Ticket.open.where(:analyst_id => current_user.id).search(params[:q])
@tickets = @search.result.paginate(:page => params[:page], :per_page => 25)
render 'shared/tickets.html.erb'
end
Great tutorial.
When using the predicates :true or :false, the user shouldn't have to put anything into the value field, however the condition is dropped when you hit search. Any workaround for this?
If you are having a problem with the dynamic add/remove javascript not working I found a solution. The problem for me was the rake gem. My solution was to require rake 0.9.2.2 in gemfile then run "bundle update" to rebuild gems from scratch.
After update rails version to 3.2.17, the javascript and css can work well.
I bootstrap the advanced search form as follows:
index.html.erb
<%= search_form_for @search, url: search_customers_path, method: :post, :html => {:class => "form-horizontal"} 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="form-group">
<div class="row col-md-4">
Sort:
<%= f.sort_fields do |s| %>
<%= s.sort_select({}, :class => 'form-control row_spacer') %>
<% end %>
</div>
</div>
<div class="row">
<button type="submit" class="btn btn-success">Submit</button>
<%= link_to "back", customers_path, class: "btn btn-danger" %>
</div>
<% end %>
_conditional_fields.html.erb
<div class="form-inline">
<div class="form-group">
<%= f.attribute_fields do |a| %>
<%= a.attribute_select({associations: [:category]}, :class => 'form-control') %>
<% end %>
<%= f.predicate_select({}, :class => 'form-control') %>
<%= f.value_fields do |v| %>
<%= v.text_field(:value, :class => 'form-control')%>
<% end %>
<%= link_to "remove", '#', class: "remove_fields btn btn-warning row_spacer" %>
</div>
<div class="row_spacer"></div>
</div>
application_helper.rb
def link_to_add_fields(name, f, type)
new_object = f.object.send "build_#{type}"
id = "new_#{type}"
fields = f.send("#{type}_fields", new_object, child_index: id) do |builder|
render(type.to_s + "_fields", f: builder)
end
link_to(name, '#', class: "add_fields btn btn-primary form-group", data: {id: id, fields: fields.gsub("\n", "")})
end
application.css.scss
.row_spacer {
margin-top: 5px;
}
now it is pretty. Probably won't work for everyone but it sucked trying to figure this out when you are still learning.
Is there a way of autoselecting (via Ajax let's say for example) similar associations? if you have examples please do share.
How do I add ransack to my bootstrap nav search field?
Thanks,
Joe