I have a simple rails search form that will select rank on a column in which 3 different ranks in a table. But I cannot extend plural ranks.
Table Food: value1, rank_value1, value2, rank_value2, value3, rank_value3
View /foods/index.html:
<%= form_tag foods_path, :method => 'get' do %>
<p>
rank_value1
<%= text_field_tag :rvalue1, params[:rvalue1] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
Model food.rb:
def self.search(search)
if search
Food.where(["rank_value1= ?", "#{search}"])
else
Food.all
end
end
Controller foods_controller.rb:
def index
#foods = Food.search(params[:rvalue1])
end
To extend two ranks, I tried codes below but it did not work.
View /foods/index.html:
<%= form_tag foods_path, :method => 'get' do %>
<p>
rank_value1
<%= text_field_tag :rvalue1, params[:rvalue1] %>
<%= text_field_tag :rvalue2, params[:rvalue2] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
Model food.rb:
def self.search(search1,search2)
if search
Food.where(["rank_value1= ? and rank_value2=?", "#{search1}", "# {search2}"])
else
Food.all
end
end
Controller foods_controller.rb:
def index
#foods = Food.search(params[:rvalue1], params[:rvalue2])
end
Any suggestions as to search with data in multiple columns?
Hey for searching with multiple values you can use Ransack gem. Here you can see the video tutorial and github link for implementation.
github: https://github.com/activerecord-hackery/ransack
video : http://railscasts.com/episodes/370-ransack
Hope you are looking for this only.
Related
I am trying to do a simple search function. This is the function in the module. I have two columns: title and description. But I get an error. Instead of posts I need to have select "title" in there.
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
The error I get is:
SQLite3::SQLException: no such column: name: SELECT "posts".* FROM "posts" WHERE (name LIKE '%first%')
UPDATE:
Here is my index.html.erb file. I have essentially used a form and listed all the posts along with their content. How do I change the file to display only the searched item? Initially all should be listed. I am not able to understand how to do this. Any ideas?
<h1>Our Blog</h1>
<%= form_tag posts_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
<% #posts.each do |post| %>
<h2><%= link_to post.title,post %></h2>
<p><%= post.content %></p>
<hr />
<%= link_to "Add a new post", new_post_path %>
<% end %>
You have no column name in your table posts i think you want search on title & description so try following
find(:all,
:conditions => ['title LIKE ? OR description like ?', "%#{search}%", "%#{search}%"])
I want to update an attribute ':what_cause' of model user on a page which is in a session. User clicks on one of the 3 radio buttons and corresponding value should be transferred to a method which updates it.
I wrote following code-
<%= form_for :user do |f| %>
<label>Pratham</label>
<%= f.radio_button :what_cause, "pratham" %>
<label>Kali</label>
<%= f.radio_button :what_cause, "kali" %>
<label>Akshaya</label>
<%= f.radio_button :what_cause, "akshaya" %>
<%= f.submit "Save", :controller => "users_controller", :action => "change_cause", :method => "put" %>
<% end %>
And here is the code for updation in change_cause method of users_controller.rb-
def change_cause
if params[:radio_button] == "pratham"
#user.update_attribute(:what_cause, "pratham")
end
if params[:radio_button] == "kali"
#user.update_attribute(:what_cause, "kali")
end
if params[:radio_button] == "akshaya"
#user.update_attribute(:what_cause, "akshaya")
end
end
But it is not working. Please enlighten me. I am a newbie in RAILS!!!
def change_cause
#user.update_attribute(:what_cause, params[:user][:what_cause])
end
I am taking a rails class at my University and I am trying to create a search form which will show the results on the same page rather than show a different page of results. Is this something simple to do? I am creating a museum app with artifacts for each museum but I want the user to search artifacts from either page.
On my routes.rb I have
resources :artifacts do
collection do
get 'search'
end
end
On my museum index I have the code below that he gave us but not sure how to tweak the get routes for the same page.
<%= form_tag search_artifacts_path, :method => 'get' do %>
<p>
<%= text_field_tag :search_text, params[:search_text] %>
<%= submit_tag 'Search' %>
</p>
<% end %>
<% if #artifacts %>
<p> <%= #artifacts.length %> matching artifacts. </p>
<h2> Matching Artifacts </h2>
<% #artifacts.each do |a| %>
<%= link_to "#{a.name} (#{a.year})", a %><br />
<% end %>
<% end %>
Yes, this is easy. Just have the index page return the search results if params[:search_text] is present - this way you don't need a new route or a different page.
class ArtifactsController < ApplicationController
def index
#artifacts = Artifact.search(params[:search_text])
end
end
class Artifact < ActiveRecord::Base
def self.search(query)
if query
where('name ILIKE ?', "%#{query}%")
else
all
end
end
end
So then your form looks like:
<%= form_tag artifacts_path, :method => 'get' do %>
<p>
<%= text_field_tag :search_text, params[:search_text] %>
<%= submit_tag 'Search' %>
</p>
<% end %>
Edit:
So what you really want to do is any page you want to search, include a form which makes a request to that same page.
Then in each of those controller methods just put this line of code:
#artifacts = Artifact.search(params[:search_text])
and that will populate the #artifcats array with only artifacts that match the search query.
Try using "Ransack" gem. It can also perform some more powerful searches.
I have a search controller and index page:
# search/index.html.erb
<% #user_prices.each do |up| %>
# This needs to go to particular ID of UserPrice.
<%= link_to show_user_price_path do %>
<%= up.product_name %>
<% end %>
<% end %>
----------------------------------------------------------
class SearchController < ApplicationController
def index
#search = UserPrice.search do
fulltext params[:search]
paginate(:per_page => 5, :page => params[:page])
end
#user_prices = #search.results
end
end
Above you can see on my search form I am trying to link the product name to that particular UserPrice.How would I do this?
Thanks.
<%= link_to up.product_name, show_user_price_path(up) %>
That should do?
Not exactly sure why you're using the block form of link_to, if you check the link_to documentation there is an easier way to use the function to get what you want.
<% #user_prices.each do |up| %>
<%= link_to up.product_name, show_user_price_path(up) %>
<% end %>
Guys my check_box_tag looks like as follows
<%= form_tag({:action => 'update_survey_list_status',:projectid=>params[:id], :status=>4}, :id => 'to_be_approved_frm') do %>
<% #publishedlist.each do |b| %>
<%= fields_for "beneficiaryloan[#{b.id}]" do |bloan| %>
<%= bloan.text_field :amount, :class=>'forms_txtbx'%>
<%= bloan.text_field :rate, :class=>'forms_txtbx'%>
<%= bloan.text_field :period, :class=>'forms_txtbx'%>
<% end %>
<%= check_box_tag "benificiary_id[#{b.id}]",b.id,:name => "benificiary_id[]"%>
<% end %>
<%= submit_tag "Approve", :class=>'form_buttons' %>
<% end %>
And in controller, I'm reading all the beneficiary ids like this
params[:beneficiaryloan].each do |key, value|
beneficiary = Beneficiary.find(key) rescue nil
#benefciary_loan=beneficiary.beneficiaryloans.build(value)
#benefciary_loan.beneficiary_id=beneficiary.id
#benefciary_loan.hfi_id=session[:id].to_s
#benefciary_loan.status_id=params[:status]
#benefciary_loan.save if beneficiary
end
What I need is, Inserting all the beneficiary ids to [beneficiaryloans] table which are checked, but in my case it inserting all records even some of them are unchecked.
How to do I select only checked ids?
Try changing your check_box_tag to
<%= check_box_tag "beneficiaryloan[#{b.id}][enabled]", 1, true %>
Then in your controller do the following:
params[:beneficiaryloan].select{|k,v| v.delete(:enabled).to_i > 0 }.each do |k,v|
..
end
Since the enabled attribute has no influence in the model you can just delete it out of the resulting beneficiary_load hashes.