My project is about an online Mobile Shopping site.
I created search controller,and I use WHERE LIKE to do that:
def create
#result = Phone.where(['name LIKE ?', "%#{get}%" ])
render :index
end
private
def get
params[:keyword]
end
Now I want to exchange to Full-text-search.So have some gem to do that ? And how to code to searching by full-text-search ?
You can use elastic-search-rails gem for implementing full text search in your Rails application.
In your app/models/phone.rb:
require 'elasticsearch/model'
class Phone < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
end
Phone.import
Then, in your controller:
#result = Phone.search('foobar').records
Here is a good tutorial on Getting Started with Elasticsearch on Rails that would help you. Another good SitePoint article that will help you get going is Full-Text Search in Rails with ElasticSearch.
Related
I'm writing the documentation about my Rails' project and I have a question.
I developed the notification's mechanism with the GoRails tutorial and the code is something like this, Me question is this is a controller or a model logic? I think controller logic but I am not sure
class EventNotification < Noticed::Base
deliver_by :database
def message
#group=Group.find(params[:group][:id])
#user=User.find(params[:user][:id])
#event=Event.find(params[:event][:id])
"#{#event.user.username} invite you in #{#event.title} into #{#group.name} click here to visit"
end
#
def url
group_event_path(Group.find(params[:group][:id]), Event.find(params[:event][:id]) )
end
end
I'm very much new to the Administrate gem for Ruby on Rails.
I can't make it sort/order posts by "Published At". Records seem to be ordered by their id's and not the published_at date, how can I make it work?
See the example app over at Heroku:
https://administrate-prototype.herokuapp.com/admin/blog/posts
Thanks in advance
Your looking for Model.order function, here are the docs:
https://guides.rubyonrails.org/active_record_querying.html#ordering
If you have a model named Post, when defining it in the controller function show or any other that you are using, you can get the record like below so that it comes sorted by published_at
Post.order(published_at: :desc) # ActiveRecord ordering
# OR
Post.sort_by(&:published_at) # pure Ruby sorting implementation
If you always want it come in sorted by published_at you can use default_scope of rails in Post like below -
default_scope { order(published_at: :desc) }
Its easy, you need to modify the request.query_parameters not params to get your job done.
Here is an example
# app/controllers/admin/users_controller.rb
# frozen_string_literal: true
module Admin
class UsersController < Admin::ApplicationController
before_action :default_params
def default_params
request.query_parameters[:user] ||= {} # change :user --> your resource
request.query_parameters[:user][:order] ||= :id # your field
request.query_parameters[:user][:direction] ||= :desc # your direction
end
What will be a better approach to display labels in a web app based on a user.
For ex: We have User groups A and B. And labels and headers differ between groups. There will be no changes in the layout and only text differs.
I was looking at Rails themes. However looks like it works well for assets and themes.
Looking for suggestions here. App is on Rails 4.
Probably you may use decorator pattern with gem draper
Implementation will look something like this:
# app/decorators/group_decorator.rb
class GroupDecorator < Draper::Decorator
def name
end
end
# app/decorators/group_one_decorator.rb
class GroupOneDecorator < GroupDecorator
def name
'group one specific message'
end
end
# app/decorators/group_one_decorator.rb
class GroupTwoDecorator < GroupDecorator
def name
'group two specific message'
end
end
Then wherever you can just call decorate on group
user.group.decorate.name
or
GroupOneDecorator.new(user.group).name
I am using Sunspot for search and my queries aren't returning any results. I am using an API to do the search.
Here is some relevant code:
class App < ActiveRecord::Base
searchable do
text :name, :boost => 5
text :description
end
end
Controller:
module Api
module V1
class AppsController < ApiBaseController
respond_to :json
class App < ::App
end
def index
#search = App.search do
fulltext params[:search]
end
#apps = #search.results
respond_with #apps
end
end
end
end
The URL I normally use to access the index method in the AppsController without searching is http://0.0.0.0:3000/api/apps
When trying to search, I use http://0.0.0.0:3000/api/apps?search=test
Is my search URL correct or should it be something like http://0.0.0.0:3000/api/apps?name=test
I've tried many different URL formats and they all return an empty result even though there is data in my db. I have also reindexed many times and it shows that 6 items were indexed, which is correct. Any ideas as to what is going on?
EDIT: it works fine when searching from the web app without using the api. It just returns empty when calling from the API url
i would try to remove the ::App subclassing in your controller. my guess is it messes up sunspots or activerecords inference mechanisms regarding the table <-> class naming conventions.
I have a simple app running Rails3 & the gmaps4rails gem.
I have the map displaying successfully.
But, when I create a new User, the lat/long is not automatically generated like in the screencast. What am I missing?
Here is my model code:
class Merchant < ActiveRecord::Base
acts_as_gmappable
def gmaps4rails_address
"#{self.address}, #{self.zip} #{self.city}, # {self.country}"
end
end
I also can't enter in an address as simply "City, State". I have to put in a complete address or I get an error.
There will not be any issue not using the example I provide in the wiki:
def gmaps4rails_address
"#{self.address}, #{self.zip} #{self.city}, # {self.country}"
end
This is just supposed to tell that you can customize it as you need.
So yes, it could simply be:
def gmaps4rails_address
self.address
end