ROR error with Salesforce: The requested resource does not exist - ruby-on-rails

I am using the Ruby Gem "databascdotcom" to integrate Salesforce in a Rails app and all works fine.
But getting following error on first call to salesforce:
**The requested resource does not exist**
If I refresh (Ctrl + F5) the page again, it works fine without any error.
Here is my code:
def SalesForceFeed
#oppID = params[:oppid]
client = Databasedotcom::Client.new client.client_id #=> foo client.client_secret #=> bar
client.authenticate :username => "foo#bar.com", :password => "ThePasswordTheSecurityToken" #=> "the-oauth-token"
client.materialize("Opportunity")
begin
#client=SalesForce::Connection.new.client
#opp = Opportunity.find_by_Id(#oppID)
rescue Exception=>e
if(e.message == "The requested resource does not exist")
return redirect_to :action => 'SalesForceFeed', :oppid => #oppID
end
end
On the first call to "SalesForceFeed" it returned the error "The requested resource does not exist", so I added an excepton check and called "SalesForceFeed" again, but I know this is not the right way.
Please suggest me a better way to fix this. Thanks.

Finally i got the issue. There was a wrong object added in materialize. I removed that and it's fixed.

Related

How To Subscribe To Real Time Updates For My App Users Feeds Using Koala Gem Rails

Having trouble getting the real time updates from Facebook working using Koala gem...
https://github.com/arsduo/koala/wiki/Realtime-Updates
I have followed the guide in the wiki. I have set up a callback url with the following in my routes file:
match "facebook/subscription", :controller => :facebooks, :action => :subscribe, :as => 'subscribe', :via => [:get,:post]
Then when Facebook makes the get request I have tried both the following in my controller to 'meet the challenge' as required in their docs (https://developers.facebook.com/docs/graph-api/webhooks/getting-started#verification-requests):
class FacebooksController < ApplicationController
def subscribe
verify_token = "VERIFY_TOKEN"
if params["hub.mode"] == "subscribe" && params["hub.verify_token"] == verify_token
params["hub.challenge"]
else
false
end
end
end
I have also tried (with the route amended to the right action):
class FacebooksController < ApplicationController
def realtime_request?(request)
((request.method == "GET" && params['hub.mode'].present?) ||
(request.method == "POST" && request.headers['X-Hub-Signature'].present?))
end
def subscription
if(realtime_request?(request))
case request.method
when "GET"
challenge = Koala::Facebook::RealtimeUpdates.meet_challenge(params,'SOME_TOKEN_HERE')
if(challenge)
render :text => challenge
else
render :text => 'Failed to authorize facebook challenge request'
end
when "POST"
p params['object']
render :text => 'Thanks for the update.'
end
end
end
end
The result every time is the following error on their dashboard when I try to subscribe:
The URL couldn't be validated. Response does not match challenge, expected value="2090763306", received="\u003C!DOCTYPE html>\n\u003Chtm..."
And the following when I try to run this in the console:
Console cmd:
#updates.subscribe("user", "feed", "https://www.bettrplay.com/facebook/subscription" , "YOUR_VERIFY_TOKEN")
Console response:
Koala::Facebook::ClientError: type: OAuthException, code: 2201, message: (#2201) response does not match challenge, expected value="773833772", received="\u003C!DOCTYPE html>\n\u003Ch...", x-fb-trace-id: GbtUB+FcLJS [HTTP 400]
I am unsure what the issue is as I can not see what is being returned to Facebook and i am still a little unsure of what I should be using as the VERIFY_TOKEN.
Any help appreciated.
OK after much testing and cURLing I found the problem.
First the problem was devise trying to authenticate and so responding with the HTML error doc.
Then I had used a piece of code off here to respond to the request, which was fine but old.
Just in case anyone missed it, render_text: is now render_plain : What to use instead of `render :text` (and `render nothing: true`) in rails 5.1 and later?
I am now subscribed for updates from my app users and if you want to do the same you can use the code above, just remember to skip authentication in the FacebooksController and use render_plain instead of render_text!

How to save data using Redmine issue model

I need to modify the issue's start_date and due_date some how,
But I haven't used Rails before, so I don't know how the it run in server,
And I need to implement it in a short time.
So I add these code in the controller,
def date
issue = Issue.find(params[:id])
issue.start_date = params[:start_date]
issue.due_date = params[:due_date]
ntc_str = "Fail to save!" + params[:id]
if issue.save
ntc_str = 'Issue saved!'
end
flash[:notice] = ntc_str;
redirect_to :controller => 'gantts', :action => 'show', :project_id => params[:p_id]
end
It runs when I access it by myself
It always failed and the "ntc_str" always is "Fail to save!" if I use javascript to access it.
For example:
It runs when I input the url "http://xxx.xxx.xxx.xxx/date?id=6&project_id=test&start_date=2016-06-08&due_date=2016-06-30" by hands,
But it failed when I use javascript "window.location.href='/date?id=6&project_id=test&start_date=2016-06-08&due_date=2016-06-30'"
It runs when I input the params in the form I create and click to submit it,
But it failed when I use javascript "document.getElementById('start_date').value = '2016-06-30'; /..../ $('#test-form').submit()"
Could you tell me why it always fails and how can I use the issue model? I have be crazy now.
It would be useful, if you provide some logs with each cases you try.
Also, you can see what goes wrong with issue, when you try to save it, with:
if issue.save
ntc_str = 'Issue saved!'
else
Rails.logger.error(issue.errors.full_messages)
end

How to troubleshoot quickbooks-ruby

I have a Rails app set up with the quickbooks-ruby gem.
Oauth seems to be working—there are no errors, I follow the Quickbooks login in a pop-up, and I get a confirmation that the account is connected. No errors.
However, in my audit log on Quickbooks Online, there is not indication that I have logged in.
And when I follow the steps to try to get a list of customers, I get this error:
undefined method 'get' for "qyprd...":String
Which may or may not be a different problem.
This is the code:
def index
#customers = Customer.all
service = Quickbooks::Service::Customer.new
service.company_id = session[:realm_id]
service.access_token = session[:token]
customers = service.query()
end
I don't understand why it isn't working, or how to troubleshoot the issue.
From the above code I see you are not passing the query in last but one line. Query should look something like this i.e customers.query("Select Id, GivenName From Customer")
To understand why it isn't working and to troubleshoot the issue you can use begin and rescue.
begin
oauth_client = OAuth::AccessToken.new($qb_oauth_consumer, token, secret)
service = Quickbooks::Service::Customer.new(:access_token => oauth_client, :company_id => realm_id)
customers = customers.query("Select Id, GivenName From Customer")
rescue Exception => e
# e.message will show response of quickbooks for you request
puts e.message
# e.message.inspect will show logs
puts e.backtrace.inspect
end
You can also try your queries on API explorer and see if get the result set you want.

Amazon Mechanical Turk - Rturk - RestClient::BadRequest: 400 Bad Request

I'm trying to set up Rturk to outsource some work to Amazon's Mechanical Turk. When I try to create my HITs, I keep running into t he following error in console:
RestClient::BadRequest: 400 Bad Request
When I copy the URL and paste it in my browser to get the response, I get the following message:
This user is not authorized to perform the requested operation
Do you guys have any idea what could be happening here? I am following the rturk documentation on github. https://github.com/mdp/rturk Is it possible that the gem has to be updated?
RTurk.setup(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY_ID'], :sandbox => true)
hit = RTurk::Hit.create(
:title => "Sample turk",
:assignments_duration => 1.hour,
:expires_at => 1.day.from_now
) do |hit|
hit.lifetime = 1.day
hit.assignments = 1
hit.description = "Test description."
hit.keywords = "places, search, map, location"
hit.question(mturk-fb_path, :frame_height => 750)
hit.reward = reward
if approval_rate
hit.qualifications.add :approval_rate, { :gt => approval_rate }
end if abandoned_rate
hit.qualifications.add :abandoned_rate, { :lt => abandoned_rate }
end
if is_us
hit.qualifications.add :country, { :eql => "US" }
end
end
}
A couple things might be going on here:
I don't believe :assignments_duration and :expires_at are valid options. You should be using hit.duration to specify the amount of time a worker has to complete a hit once they accept it and hit.lifetime to set the amount of time until the hit will expire.
It might just be a typo in your post, but mturk-fb_path is not a valid ruby variable name. (can't use a dash)
Try starting off with the simplest example that you can build off of once you get it working. This example from rturk should be a good start: https://github.com/mdp/rturk#creating-hits.
Also, I definitely recommend reading through the mturk docs for creating a hit: http://docs.aws.amazon.com/AWSMechTurk/2011-10-01/AWSMturkAPI/ApiReference_CreateHITOperation.html.

Ruby on Rails - Checking against HTTP errors in controller

Just today I've found my fbgraph implementation has started returning a 400 Bad Request error which is causing an internal server error.
The controller looks like:
def fb
fbclient = FBGraph::Client.new(:client_id => 'ID', :secret_id => 'SECRET')
#fbname = fbclient.selection.user('129220333799040').feed.info!['data'][0].from.name
#fbmessage = fbclient.selection.user('129220333799040').feed.info!['data'][0].message
end
How can I check before calling #fbname in my view that I've received a 200 status?
Thanks.
Update: following Devin M's suggestion, I've switched the above action to
def fb
fbclient = FBGraph::Client.new(:client_id => 'ID', :secret_id => 'SECRET')
begin
#fbname = fbclient.selection.user('129220333799040').feed.info!['data'][0].from.name
#fbmessage = fbclient.selection.user('129220333799040').feed.info!['data'][0].message
rescue
#fbname = "Facebook Account"
#fbmessage = "Facebook's API is a nightmare"
end
end
I think that you should write some tests for this, Its hard to work with Facebooks nightmare of an API.
Although if you wanted to catch this error try using that way you can catch the specific error and take some action on it in the rescue portion.
begin
rescue
end
If you want me to take a look at the docs and see what you should catch let me know.

Resources