I'm using gem 'ransack' to search records.
The page I'm programming starts with ONLY a dropdown to select #department.
After that, I want Ransack to search within #department.
I need to include an additional parameter department=2 with the Ransack search parameters. (It works if I insert that into the browser URL).
Browser URL after I type in department=2
http://localhost:5000/departments/costfuture?department=2&utf8=%E2%9C%93&q%5Bid_eq%5D=&q%5Bproject_year_eq%5D=&q%5Boriginal_year_eq%5D=&q%5Bclient_id_eq%5D=43&q%5Blocation_name_cont%5D=&q%5Bproject_name_cont%5D=&q%5Bcoststatus_id_in%5D%5B%5D=&q%5Bcoststatus_id_not_in%5D%5B%5D=&q%5Brebudget_true%5D=0&q%5Bnew_true%5D=0&q%5Bconstruction_true%5D=0&q%5Bmaintenance_true%5D=0&commit=Search
This is the controller:
def costfuture
#departments = current_user.contact.departments
if params[:department]
#department = Department.find(params[:department])
#search = #department.costprojects.future.search(params[:q])
#costprojects = #search.result.order(:department_priority)
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: #departments }
end
end
I tried this in the view:
<%= search_form_for #search, url: departments_costfuture_path(:department => #department.id) do |f| %>
But, the resulting URL is missing the department=2.
If you want to pass the department option through to Ransack, you can do this with a hidden field within your search_form_for:
<%= search_form_for #search, url: departments_costfuture_path do |f| %>
<%= f.hidden_field :department_eq, value: #department.id %>
<%# Other form code here %>
<% end %>
But if you want to search a particular department, then it's better to use Rails routes for that. You can generate URLs like /departments/2/costfuture by modifying config/routes.rb:
resources :departments do
get 'costfutures', on: :member
end
Now you can use the resulting URL helper to generate links that will set params[:id], and you can use that to retrieve #department.
I ended up using hidden_field_tag inside search_form_for.
<%= search_form_for #search, url: departments_costfuture_path do |f| %>
<%= hidden_field_tag :department, #department.id %>
<%= f.search_field :last_name_or_first_name_cont %>
<% end %>
Then params[:department] and params[:search] will be accessible in the controller.
Related
I'm struggling to get a search form for my active record history.
I'm using the paper_trail gem as well as ransack. Both, alone, are working well. But whenever I try to implement a search form I get a strange error:
undefined method `paper_trail_versions_path' for #<#:0x007fede31da910>
This line from my views gets highlighted:
<%= search_form_for #q, html: {class: "input-group"} do |f| %>
Here's the code of the controller:
def history
#all_versions = PaperTrail::Version.order('created_at DESC')
#versions = #all_versions.paginate(:page => params[:page], :per_page => 100)
#q = #versions.search(params[:q])
#versions = #q.result
respond_to do |format|
format.html
end
end
the views:
<%= search_form_for #q, html: {class: "input-group"} do |f| %>
<%= f.search_field :whodunnit_or_item_id_cont, placeholder: "Search for Users or Actions..", class: "form-control" %>
<span class="input-group-btn">
<%= button_tag(type: 'submit', class: "btn btn-default") do %>
<i class="fa fa-search"></i>
<% end %>
</span>
<% end %>
Any Ideas?
Thanks in advance!
EDIT resolved, thanks to answer:
<%= search_form_for #q, html: {class: "input-group"}, :url => "/dashboards/history/" do |f| %>
The error your see comes from the URL the search_form_for helper tries to guess for your form's action.
Under the hood, Ransack uses the polymorphic_path rails helper, which is, according to the doc, a method
for smart resolution to a named route call when given an Active Record model instance
In your case, your model is a PaperTrail::Version, which resolves to paper_trail_versions_path.
To fix the error, you can either :
define the corresponding resource in your routes.rb file, so that the helper is defined
manually define the helper
provide to the search form a url option, with the path to your form's action.
Hope it helps!
I have created a search form
<%= form_tag url_for(:action => "index") do %>
<%= text_field_tag 'fromdate' %>
<%= text_field_tag 'todate' %>
<%= submit_tag 'Search' %>
<% end %>
I need to pass all these values as a single params in my controller to model. This is my controller
def index
#client = Customer.search
#something like this #client = Customer.search(as_single_params)
respond_to do |format|
format.html
end
end
How can I do this?
You can do this in your view:
<%= text_field_tag "some_fields[]" %>
<%= text_field_tag "some_fields[]" %>
<%= text_field_tag "some_fields[]" %>
Then in your controller simply retrieve the values using:
some_fields = params[:some_fields] # this is an array
If you pass values as keys, Rails will give you a Hash:
<%= text_field_tag "some_fields[fromdate]" %>
<%= text_field_tag "some_fields[todate]" %>
In your controller you can do this now:
#clients = Customer.search(params[:some_fields])
^
I added an s to #client since you'll have probably more than one clients matching your criteria.
When you submit your form, all of the parameters you selected will be passed to the controller in the params hash. For example, params[:fromdate] and params[:todate].
In your controller, if your params hash contains only Customer fields you want to search on (i.e., the form only has Customer fields defined, then you can do:
def index
#clients = Customer.where(params)
respond_to do |format|
format.html
end
end
Note that I show #clients since where will return a collection. You can select the first one (especially if you're sure there's only one) by doing this:
#client = Customer.where(params).try(:first)
Hi how can I edit this code so when I click on search, I am redirected to specifed page not stay on the same?
<%= form_tag products_path, :method => 'get',:id => "products_search" do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %> </li>
<% end %>
You can do it like this:
<%= form_tag "http://www.stackoverflow.com/questions", :method => 'get',:id => "products_search" do %>
Put whatever you need in place of that URL.
for more info please look at the form_tag helper documentation
But if you need actually to be trough that path you were using, you have to use a redirect or a forwarding.
For using a forwarding just call the name of the next action near the end of that action yuo are calling.
Something like:
def another_action
...
end
def product
...
another_action
end
For using a redirect just do it like:
def products
redirect_to another_action
end
I am trying to set up a form in a Rails view to submit an id back to the show method in the controller. My form uses autocomplete to set the hidden id field:
<%= form_tag students_path, id: 'search_student_name', method: :get do %>
<%= text_field_tag :search_name, '', size: 30 %>
<%= hidden_field_tag :id %>
<%= submit_tag "Search", name: nil %>
<% end %>
I'm using the standard controller 'show' method generated by the scaffold:
# GET /students/1
# GET /students/1.json
def show
#student = student_scope.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #student }
end
end
I'd be grateful for any advice on the combination of form url / post method / additional routes to get this to work. Is this the way you'd normally do this is Rails, or should I set up a new controller method to handle the submitted form?
Thanks in advance
Because it is not exactly a Restful show, you should create a new action, search.
#routes.rb
post 'search' => 'students#search'
#student_controller.rb
def search
#student = Student.find_by_name params[:search_name]
render action: 'show'
end
The form doesn't need to send the :id as far as I can tell.
<%= form_tag search_path, method: :get do %>
<%= input_tag :search_name, type: 'text' %>
<%= submit_tag "Search" %>
<% end %>
A model named 'book' with attributes 'name' and 'id' is given. How can i use this collection select to call the show-action of a certain book? The one code mentioned below returns the following error message:
Couldn't find Book with ID=book_id
<% form_tag(book_path(:book_id)), :method => :get do %>
<p>
<%= label(:book, :id, 'Show Book:') %>
<%= #books = Books.find(:all, :order => :name)
collection_select(:book, :id, #books, :id, :name)
%>
</p>
<p>
<%= submit_tag 'Go' %>
</p>
<% end %>
book_path is generated once only, for the form tag itself. It won't be updated whenever your selection changes.
When you submit that form, it's going to request the following URL:
/books/book_id?book[id]=5
Since your book_path thinks book_id is the ID number you wanted, it tries to look that up. You could do what you want you by changing the code in your controller from:
#book = Book.find(params[:id])
to:
#book = Book.find(params[:book][:id])
But it kind of smells bad so be warned.
You can create a new route that is not based on the id, like
get 'books/show' # put this above your "resources :books"
and change your form to
<% form_tag books_show_path, :method => :get %>