ActiveRecord error when when using simple search - ruby-on-rails

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}%").

Related

pagination ruby on rails with will_paginate

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?

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 %>

Ruby on Rails search form doesn't recognise capital letters

I have a working search form in my RoR app however it doesn't recognise capital letters. So if i search for something it has to be case sensitive. I tried adding UPPER in the search method however it raises an error. How do I get it not be case sensitive?
Here is the controller:
def index
if params[:search]
#restaurants = Restaurant.search(params[:search]).order("created_at DESC")
else
#restaurants = Restaurant.joins("LEFT JOIN reviews ON restaurants.id = reviews.restaurant_id").group("restaurants.id").order("AVG(reviews.rating) DESC NULLS LAST")
end
end
This is the method from the model:
def self.search(query)
where("name LIKE ?", "%#{query}%")
end
And finally the form:
<section class="search-form">
<%= form_tag(restaurants_path, :method => "get", id: "search-form", class: "navbar-form navbar-left") do %>
<div class="form-group">
<%= text_field_tag :search, params[:search], placeholder: "Search restaurants", :autocomplete => :off, class: "form-control", id: "search-field", required: "required" %>
</div>
<%= submit_tag "Search", :name => nil, id: "search-button", class: "btn btn-default" %>
<% end %>
</section>
Would appreciate any advice. Thank you.
You can always convert params[:search] to lowercase, I'd do something like this:
Restaurant.search(params_search).order("created_at DESC")
private
def params_search
params[:search].to_s.downcase
end

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" %>

Ruby on Rails 3: Toggle Boolean with Forms

Users are able to send each other messages on my Ruby app. The table is as followed:
t.string "content"
t.integer "from_id"
t.integer "reply_to_id"
t.boolean "read", :default => false
I have recently added the boolean, :read, and I have the form linked to messages in their inbox. When they click "Mark Read" (the link provided via the form) I want it to toggle :read => true.
I am currently unable to accomplish this. This is my current setup.
_mark_read.html.erb
<%= form_for(:message, :mark_read => {read: true}) do |f| %>
<div><%= f.hidden_field :read %></div>
<%= f.submit "Mark Read", class: "btn-link" %>
<% end %>
Class MessagesController < ApplicationController
def mark_read
#message = current_user.messages.build
#message.toggle!(:read)
end
config/routes.rb
match '/mark_read' => 'messages#mark_read', :via => :post, :as => :mark_read
When I click "Mark Read", here is my debug info:
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ✓
authenticity_token: ---
message: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
read: 'false'
commit: Mark Read
controller: static_pages
action: connect
I can see it is not even using my controller... and even so; I don't think I have it set up correctly.
Any advise as to how this should be set up would be kindly appreciated.
Believe it or not.. I didn't even need a model, nor controller :D As I said it is rendered within a paginated set. This paginated set is defined at #inbox_items identified as inbox_item.id.
Here is the form that works without any need of a model or controller. It is simply defined in config/routes.rb as:
match '/mark_read' => 'messages#mark_read', :via => :post
The form:
<ul class="share-actions" id="mark_read<%= inbox_item.id %>">
<%= form_for :mark_read do |f| %>
<%= f.check_box inbox_item.toggle!(:read) %>
<%= f.submit "Mark Read", class: "btn btn-link" %>
<% end %>
</ul>
Ammusing, huh? :D
More information in-case you are curious. The base Class of rendered #inbox_items, is Class Message. Messages define all of user's private posts. In the User class I begin to set up how #inbox_items will be defined:
Class User
def inbox
Message.from_users_inbox(self)
end
Then I build the rest:
Class Message
def self.from_users_inbox(user)
"SELECT best_friend_id FROM friendships WHERE new_friend_id = :user_id"
where("user_id = :user_id OR reply_to_id = :user_id", user_id: user.id)
end
MemberpagesController
def inbox
#message = current_user.messages.build
#inbox_items = current_user.inbox.paginate(page: params[:page])
end
feeds/_inbox.html.erb
<% if #inbox_items.any? %>
<%= render: 'feeds/inbox_item', collection: #inbox_items %>
<%= will_paginate #inbox_items %>
<% end %>
And finally:
feeds/_inbox_item.html.erb
<div id="<%= inbox_item.user.name %>_<%= inbox_item.id %>">
...
<ul class="share-actions" id="mark_read<%= inbox_item.id %>">
<%= form_for :mark_read do |f| %>
<%= f.check_box inbox_item.toggle!(:read) %>
<%= f.submit "Mark Read", class: "btn btn-link" %>
<% end %>
</ul>
...
And that did it! :)

Resources