How to implement autocomplete search from google in rails ? - ruby-on-rails

I am newbie in rails ,i don't know how implement this feature in application . please suggest me any basic idea.
if any user create a book ->
1) Book name should be auto suggested like Google search(if user enter a name , it should be automatically search correct name from Google and suggest to user ).
2) Author name also be auto suggested like Google Search(if user enter a name , it should be automatically search correct name from Google and suggest to user ).
Book Model has following attributes .
> Book.new
=>Book(id: integer, user_id: integer, author: string, name: string, published_at: datetime)

Related

Creating Shipping Method in spree from backend

I want to create a shipping method in spree from backend, from front end I am able to achieve it. But in backend when I run this
Spree::ShippingMethod.create(name: "Name 1", zone_id: 1).errors.full_messages
I am getting error
["Calculator can't be blank"]
It seems it needs Spree::Calculator to be created in some way. I am beginner in Spree any help will be helpful.
So, you need to specify a Calculator.
Spree comes with some calculators:
Spree::Calculator::DefaultTax
Spree::Calculator::Shipping::FlatRate
You can also create one as explained here and after that you can create a ShippingMethod this way:
Spree::ShippingMethod.create(name: "Name 1", calculator: Spree::Calculator.first) or whatever Calculator you want.
The correct way will be:-
zone = Spree::Zone.find_by_name("North America")
shipping_method = Spree::ShippingMethod.new(name: "Pickup from Market", zone_id: zone.id)
shipping_method.calculator_type = "Spree::Calculator::FlatRate"
shipping_method.save!
In place of calculator_type we can specify any type of calculator.Because I wanted the Flate Rate I used this one.

Using Wikipedia-Client Gem to Update Rails Database

My ruby and Rails is a bit rusty. I have a table in my database called institutes which has some of the columns filled. I want to use the Wikipedia-Client gem to fill some of the others. I want to use the name attribute to find the page on Wikipedia then use page.summary for the description attribute in my table and page.image_urls.first for the picture attribute. At the moment, I'm struggling to work out how I would go about this.
My current code is:
require 'Wikipedia'
Institute.each do |institute|
school = institute.pluck(:name)
page = Wikipedia.find(school)
description = page.summary
picture = page.image_urls.first
Institute.update!(description: description, picture: picture)
end
I'm clearly doing something wrong here to do with the selection and use of the name attribute to find the Wikipedia page, but can't quite work it out. I think even if I were to pluck the name correctly, it wouldn't assign anything to the right id.
If there's also a way to drop the "The" at the beginning of the name in the Wikipedia search if it exists in :name, that would also be helpful as it seems some institutes drop this on Wikipedia.
You can try to use something like this:
#use https://github.com/kenpratt/wikipedia-client
require 'wikipedia'
#select all Institutes through AR model
Institute.all.each do |institute|
#'institute' is an object, so we can get its name by dot operator
school = institute.name
#try to find school as is
#then try to find without 'The'
#and go ahead only if page exists
page = Wikipedia.find(school)
page = Wikipedia.find(school[3..-1].strip) if page.content.nil? and school[0..2].downcase == 'the'
next if page.content.nil?
description = page.summary
picture = page.image_urls.first
#update Institute object
institute.update!(description: description, picture: picture)
end

Rails: Assigning Variables to Associated models

Hey I am trying to assign my currentslide that is part of my courses model and is associated to my user model.
I have 3 models:
- Users
- Courses (title: ,currentprogress: )
- UserCourseAssignment. (association table)
I can get the courses that a user has been assigned by typing User.courses however it won't let me update variables
#<Course id: 6, title: "beginnerscourse", created_at: "2016-03-20 12:42:44",
updated_at: "2016-03-20 12:42:44", reward1: nil, reward2: nil, reward3: nil,
reward4: nil, reward5: nil, currentslide: 0, totalslides: nil>]>
User = active_user
User.courses.find(6).currentslide = 1
User.save
Sorry if I wasnt clear, Updated question
You shouldn't assign a class name (User) as a variable. In your scenario, you can just use active_user.
active_user.courses.find(6).update_attributes(current_slide: 1)
As I was writing a better explanation as to why, #max answered as well with a great explanation as to what is going on here. Please refer to his answer for a great explanation.
He is also correct in reference to just using Course.find, if you already know the course ID. There is no need to load all of the current_user's courses from the DB if you already know the ID. You'll get better performance just using Course.find(id).update_attributes...
User = active_user
User.courses.find(6).currentslide = 1
User.save
There are quite a few things going wrong here. User declares a constant which will mask your User class! In Ruby any identifier which starts with an uppercase letter is a constant.
Watch this example:
User = active_user
# somewhere else in the code
User.find(5) # NoMethodError wtf!?
Also if you know the id of the course there is no need to go through the user in the first place:
course = Course.find(6)
course.update_attributes(currentslide: 1)
If you want to scope the query to the current user as poor mans version of access controller you would do it like so:
course = Course.where(user: current_user).find(6)

Rails linkedin gem - has anyone used the search feature?

I'm trying to use the search feature of https://github.com/pengwynn/linkedin. I could not find any documentation on the search feature anywhere, not even on the gem website/github. There's some info on the profile/connections pull but nothing on the search feature.
What I want to achieve is use the gem for making a people search on linkedin. I have keywords that the user enters on my site. The keyword(s) could be the name of a person (first, last, full name), or a company name. Using these keywords I want to make a keyword search on linkedin. I'm looking for keyword matches in my first level connections, not my extended network. What I want returned is the first_name, last_name, headline, and url of my connection. I guess something like this..
client.search(:keyword => "microsoft", :fields => ["first_name", "last_name", "headline", "picture_url"])
Thanks.
For the fields you need to do something like:
:fields => [:people => ["first-name", "headline"]]
Well, I'm not a Ruby programmer, so that could be totally bogus syntax. And I've not used this gem.
But the key is that you're asking a nested set of result fields, so you need to generate a REST URL that looks like:
http://api.linkedin.com/v1/people-search:(people:(first-name,headline))?keywords=homer%20simpson
And what you are asking for is ...people-search:(first-name,headline)?....

Rails find or create based on two fields

I have a venue model and i want to do this
Venue.find_or_create_by_
but i only want a new venue to be created if one with the same name and date do not already exist
For example
=> Venue(id: integer, location: string, showdate: datetime, created_at: datetime, updated_at: datetime)
A venue is unique and needs to be created if the location and the showdate are not present in the db
You can chain columns together by using _and_. This should do the trick:
Venue.find_or_create_by_location_and_showdate(location, showdate)
In rails 4 you would do:
Venue.find_or_create_by(location: location, showdate: showdate)
Much prettier, if you ask me!
my_class = ClassName.find_or_initialize_by_showdate_and_location(showdate,location)
my_class.update_attributes!
find_or_initialize method find by name and location in database. If record doesn't exist than it initialize new record with showdate and location.
http://api.rubyonrails.org/classes/ActiveRecord/Base.html This link will help you to figure out find_or_initialize and find_or_create.

Resources