pagination ruby on rails with will_paginate - ruby-on-rails

Hey guys I am trying to implement pagination using the will_paginate gem, and it doesn't seem to be working. I have added the gem to my Gemfile and tried the code below:
controller
def team_search
#teams = CareTeam.all.order('created_at ASC').paginate(page:params[:page], per_page: 10)
end
view
<h1>Search for a team</h1>
<p>search for a team by team name</p>
<%= form_tag search_team_results_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search], placeholder:
"Search Teams By name", required: true %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<%= will_paginate #teams %>
model
def self.search(team_name)
if team_name
team_name.downcase!
self.where('LOWER(team_name) LIKE ?', "%#{team_name}%").order('created_at ASC')
else
CareTeam.all.order('created_at ASC')
end
end
When I navigate to my view, nothing shows up. Does anyone analyse from above what could be wrong?

Related

rails - multiple paths for a search form

I'm implementing the website. I got a problem for a search form. I upload my code, and what i want to ask is how to set the search 'path' through 'index' and 'historical' on homes_controller
Below, my code:
app/controllers/homes_controller
def index
#homes = Home.where(:category => 1).reverse
end
def historical
#homes = Home.where(:category => 2).reverse
end
app/views/layouts/application.html.erb
Below, this code is temporary code for now. I should change it.
<%= form_tag(homes_path, :method => 'get', id: "search-form" do %>
<%= text_field_tag :search, params[:search], placeholder: "검색" %>
<%= submit_tag "검색", :name => nil %>
<% end %>
Am not sure what you are supposed to do here
But as per the question - I can give a solution to your problem
Keep an instance variable in your controller actions - like this
app/controllers/homes_controller
def index
#homes = Home.where(:category => 1).reverse
#search_path = "path you want to give"
end
def historical
#homes = Home.where(:category => 2).reverse
#search_path = "path you want to give"
end
and in your layout you can use it like this
app/views/layouts/application.html.erb
<%= #search_path.present? %>
<%= form_tag(#search_path, :method => 'get', id: "search-form" do %>
<%= text_field_tag :search, params[:search], placeholder: "검색" %>
<%= submit_tag "검색", :name => nil %>
<% end %>
<% end %>

Rails - Simple search function

I'm fairly new to rails and am building an online shop just to write some rails. I'm in the process of implementing a simple search function at the moment and get some strange behaviour I can't explain.
Model method:
def self.search(query)
where("title like ?", "%#{query}%")
end
Controller methode:
def index
if params[:search]
#products = Product.search(params[:search])
else
#products = []
#Only lists available products (in cart counts as available)
#available_items = Item.where(user_id: nil).select(:product_id).uniq
#available_items.each do |item|
#products << item.product
end
end
end
Search form:
<%= form_tag(products_path, :method => "get", id: "search-form", enforce_utf8: false) do %>
<%= text_field :search, params[:search], placeholder: "Search..." %>
<%= submit_tag "Search", :name => nil %>
<% end %>
When I trigger a search I get no results and the url looks like this:
http://localhost:3000/products?search%5B%5D=Paper
When I remove '%5B%5D' from the url it all works fine and I get my results. '%5B%5D' stands for '[]' in URI encoding, can't figure out where that come from though. Any help would be greatly appreciated!
Best,
Paul
See reference how text_field and reference for text_field_tag helper works:
<%= text_field :search, params[:search], placeholder: "Search..." %>
It will give you search input field with name=search[], thats why its passing search[]='text'.
<input type="text" name="search[]" placeholder="Search..." />
Use text_field_tag instead:
<%= text_field_tag :search, params[:search], placeholder: "Search..." %>

ActiveRecord error when when using simple search

I'm very new at Ruby on Rails and trying to make a simple search function for my application. The search function keeps returning this ActiveRecord error (here I searched for "london" in "from"):
SQLite3::SQLException: near "from": syntax error: SELECT "journeys".* FROM "journeys" WHERE (from LIKE '%london%')
Model:
class Journey < ActiveRecord::Base
validates :from, presence: true, length: { minimum: 1 }
def self.search(search_from)
self.where("from LIKE ?", "%#{search_from}%")
end
end
Controller:
class JourneysController < ApplicationController
def index
#journeys = Journey.all
if params[:search_from]
#journeys = Journey.search(params[:search_from])
else
#journeys = Journey.all.order('created_at DESC')
end
end
View:
<p>
<%= form_tag(journeys_path, :method => "get", from: "search-form") do %>
<%= text_field_tag :search_from, params[:search_from], placeholder: "Search from", :class => 'input' %>
<%= submit_tag "Search", :class => 'submit' %>
<% end %>
</p>
<% if #journey.present? %>
<%= render #journey %>
<% else %>
<p>There is nobody going from <%= params[:search_from] %>.</p>
<% end %>
Like I said, I'm very new at RoR so this is probably stupid to even ask for, but I would really appreciate the help.
Problem is that from is reserved in SQL. I highly suggest to rename this field, though if you want to use it as is - self.where("\"from\" LIKE ?", "%#{search_from}%").

Search form with checkboxes

Im trying to create a form that finds products with checkboxes. I think the problem lies in the fact that im not passing an array to my controller. Does anyone know how to fix this?
Model
def self.search(params)
arel = order('created_at DESC') # note: default is all, just sorted
arel = arel.where('name LIKE ?', "%#{params[:search]}%").order('created_at DESC') if params[:search].present?
arel
end
Controller
def index
#products = Product.search(params)
end
View
<%= form_tag(products_path, :method => "get", id: "search-form") do %>
<%= check_box_tag :search, "product1", nil %>
<%= check_box_tag :search, "product2", nil %>
<%= submit_tag "Search" %>
<% end %>
Both checkboxes are the same :search so only the last checked one is being sent. Try something like this:
<%= check_box_tag "search[]", "product1" %>
<%= check_box_tag "search[]", "product2" %>

Jquery Tokeninput & Acts-as-taggable is not working with a parent-child-child nested form

I have form which is built like this:
<%= form_for #location do |f| %>
<%= f.fields_for :product_dates do |d| %>
<%= d.fields_for :products |p| %>
<%= p.text_field :tag_list,"data-pre" => #product.tags.map(&:attributes).to_json %>
Now when i go to the page i get an error when using the line: "data-pre" => #product.tags.map(&:attributes).to_json which is undefined method tags for nil:NilClass but everything is fine when i take it away. This some type of TokenInput bug? Anyone else had to deal with this?
ProductsController:
def new
#location = Location.new
product_date = #location.product_dates.build
product_date.products.build
end
You simply didn't set your #product variable => it's nil.
You should show your controller
EDIT:
replace:
<%= p.text_field :tag_list,"data-pre" => #product.tags.map(&:attributes).to_json %>
with:
<%= p.text_field :tag_list,"data-pre" => p.object.tags.map(&:attributes).to_json %>
This should work for edit as well.
It's really good sense here: you can't invoke something you didn't set.

Resources