4 Replies - 687 Views - Last Post: 16 February 2014 - 01:30 PM Rate Topic: -----

#1 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 225
  • Joined: 03-March 11

RoR Semantics3 Help Required

Posted 15 February 2014 - 04:42 PM

Hey guys, I need some help with using Semantics3 with my Rails app, I'm struggling getting it set up, and I can't work out what the problem is; it keeps telling me that the sem3 var is not defined, but I've copied all the stuff from the setup instructions.

This is my main controller which contains the methods for the views. I've placed the initialisers in here, I'm not sure if this is the correct place for them as the set up is for a general Ruby script and not for a Rails application.

(main_controller.rb)
class MainController < ApplicationController
	require 'semantics3'

	# Your Semantics3 API Credentials
	API_KEY = 'CENSORED'
	API_SECRET = 'CENSORED'

	# Set up a client to talk to the Semantics3 API
	sem3 = Semantics3::Products.new(API_KEY,API_SECRET)

  	def index
  	end
  	def live_search
  		unless params[:q] == ""
			@records = sem3.products_field( "brand", "Toshiba" )
			render :layout => false
		else
			@records = { name: "No results found..." }
		end
	end
end



This is my view, I'm simply trying to check to see if the var is set in the view.
(live_search.html.erb)
<%= @sem3.inspect %>



Screenshot of the Error:
Posted Image

Basically I'm just looking for some guidance on where this stuff should go if it isn't supposed to be here and what not, any help is greatly appreciated, thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: RoR Semantics3 Help Required

#2 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,424
  • Joined: 28-November 09

Re: RoR Semantics3 Help Required

Posted 15 February 2014 - 11:48 PM

class MainController < ApplicationController
  # require 'semantics3' # Not necessary, include in your gemfile

  # Your Semantics3 API Credentials (Which you should put in a config.yml)
  API_KEY = 'CENSORED'
  API_SECRET = 'CENSORED'

  # Set up a client to talk to the Semantics3 API
  

  def index
  end

  def live_search
    if params[:q].present? # Don't use unless in a branch
      @records = sem3.products_field( "brand", "Toshiba" )
      @sem3 = Semantics3::Products.new(API_KEY,API_SECRET) # It helps if you put it in an ivar that's rendered by the action/
      render :layout => false
    else
      @records = { name: "No results found..." }
    end
  end
end



This should work now, assuming you're rendering the correct action.
<%= @sem3.inspect %>


Was This Post Helpful? 2
  • +
  • -

#3 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 225
  • Joined: 03-March 11

Re: RoR Semantics3 Help Required

Posted 16 February 2014 - 09:18 AM

View PostLemur, on 15 February 2014 - 11:48 PM, said:

class MainController < ApplicationController
  # require 'semantics3' # Not necessary, include in your gemfile

  # Your Semantics3 API Credentials (Which you should put in a config.yml)
  API_KEY = 'CENSORED'
  API_SECRET = 'CENSORED'

  # Set up a client to talk to the Semantics3 API
  

  def index
  end

  def live_search
    if params[:q].present? # Don't use unless in a branch
      @records = sem3.products_field( "brand", "Toshiba" )
      @sem3 = Semantics3::Products.new(API_KEY,API_SECRET) # It helps if you put it in an ivar that's rendered by the action/
      render :layout => false
    else
      @records = { name: "No results found..." }
    end
  end
end



This should work now, assuming you're rendering the correct action.
<%= @sem3.inspect %>



I'm still getting the exact same error unfortunately.

I'm using Rails 4, do I need some sort of accessor or something within the controller, it seems like it should totally work using this code, but for some reason the sem3 var just doesn't seem to be present when it's getting used in the live_search method. It's really strange. I'm also using Ruby 2.1.0

Hope this helps :) Thanks for the help so far.

Okay, so I moved this line:

sem3 = Semantics3::Products.new(API_KEY,API_SECRET)


down into the live_search method and it seems to work now, however, when I inspect the records ivar it simply prints '1..1' surely it should contain json with the records details no?
Was This Post Helpful? 0
  • +
  • -

#4 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 225
  • Joined: 03-March 11

Re: RoR Semantics3 Help Required

Posted 16 February 2014 - 09:24 AM

Got it all working now, it was just listing the fact that it had a result, I then needed to fetch the results details and store it in an ivar. The final code looks like so after all the changes, just pasting the code here in case anybody else has trouble with semantics:

class MainController < ApplicationController

	def index
	end

	def live_search
		sem3 = Semantics3::Products.new(API_KEY,API_SECRET)
		if params[:q].present? && sem3.present? # Don't use unless in a branch
			sem3.products_field( "brand", params[:q] )
			@records = sem3.get_products()
			@sem3 = Semantics3::Products.new(API_KEY, API_SECRET) # It helps if you put it in an ivar that's rendered by the action/
			render :layout => false
		else
			@records = { name: "No results found..." }
		end
	end

end


Was This Post Helpful? 0
  • +
  • -

#5 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,424
  • Joined: 28-November 09

Re: RoR Semantics3 Help Required

Posted 16 February 2014 - 01:30 PM

View Postmccabec123, on 16 February 2014 - 10:24 AM, said:

Got it all working now, it was just listing the fact that it had a result, I then needed to fetch the results details and store it in an ivar. The final code looks like so after all the changes, just pasting the code here in case anybody else has trouble with semantics:

class MainController < ApplicationController

	def index
	end

	def live_search
		sem3 = Semantics3::Products.new(API_KEY,API_SECRET)
		if params[:q].present? && sem3.present? # Don't use unless in a branch
			sem3.products_field( "brand", params[:q] )
			@records = sem3.get_products()
			@sem3 = Semantics3::Products.new(API_KEY, API_SECRET) # It helps if you put it in an ivar that's rendered by the action/
			render :layout => false
		else
			@records = { name: "No results found..." }
		end
	end

end



You may want to turn your editor down to 2 space tabs, it'll help readability a lot. Also, look into render locals. You're still doing things that won't hit the view, remember only ivars or locals will go over.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1