Object appears as existing and not at the same time - ruby-on-rails

I've got some very strange issue which I cannot debug.
This is my code:
def find_order_and_payment
#payment = Spree::Payment.find_by_identifier(params['session_id'])
Rails.logger.info "payment_id #{#payment.id}"
#order = #payment.order
end
And this is the output:
I, [2014-01-17T16:39:43.084827 #12342] INFO -- : payment_id 187
I, [2014-01-17T16:39:43.090718 #12332] INFO -- : Completed 500 Internal Server Error in 448ms
NoMethodError (undefined method `id' for nil:NilClass):
app/controllers/payu_status_controller.rb:36:in `find_order_and_payment'
Line 36 is the line with Rails.logger. I don't understand, why I get correct id, but same line returns undefined method id? If I'll call above code from console everything works as expected.

Related

Inject a shopify page through shopify-api in Rails

Im trying to inject a new page to a shopify-store through the shopify-api which is included in the shopify-app gem. I added the authentication from the gem to my controller, which works perfect for GET-requests. Somehow the POST-Method does not work.
The method inject_page_method shall be called in my create method, which would then represent a second POST request.
Im confused, because the GET-methods already work. So I thought, the gem routes this endpoints for me like this:
def inject_page_method
#create_new_page = ShopifyAPI::Page.new(:title => "test", :body_html =>"<h1>Test</h1>")
#create_new_page.save
end
helper_method :inject_page_method
Finding out this does not work I tried it with a full endpoint request:
def create_new_page
page_data = {
"page": {
"title": "Test",
"body_html": "<h1>Test succeded</h1>"
}
}
#json_request_url = #shop_domain + "/admin/api/2020-07/pages.json"
Rails.ajax({
type:"POST",
url:#json_request_url,
data:page_data
})
end
Heroku logs --tail shows me, that the method is POST, which is fine, but I get a status=500 with following errors:
NoMethodError (undefined method `+' for nil:NilClass):
app/controllers/my_controller.rb:101:in `create_new_page'
app/controllers/my_controller.rb:37:in `create'
When I change the concatenation of #json_request_url to:
#json_request_url = "#{#shop_domain}/admin/api/2020-07/pages.json"
I get a status 500:
NoMethodError (undefined method `ajax' for Rails:Module):
Which is impossible because both Jquery and Rails/ujs are required in the application.js
Edit: This is on rails 6
What is missing?

nameerror unitialized constant rails

I am working on deploying a rails 5 api. After much trail error its almost functioning. The problem I am having is that I am running into a 500 server error. I sshed into the instance to check the logs and I keep getting this name error. The log output:
[2017-11-14T15:41:47.289647 #10484] INFO -- : [6d21f1c2-6770-42a2-93be-cd68ee0441fc] Completed 500 Internal Server Error in 244ms (Views: 0.2ms | ActiveRecord: 3.6ms)
F, [2017-11-14T15:41:47.290161 #10484] FATAL -- : [6d21f1c2-6770-42a2-93be-cd68ee0441fc]
F, [2017-11-14T15:41:47.290225 #10484] FATAL -- : [6d21f1c2-6770-42a2-93be-cd68ee0441fc] NameError (uninitialized constant Net::HTTP):
F, [2017-11-14T15:41:47.290245 #10484] FATAL -- : [6d21f1c2-6770-42a2-93be-cd68ee0441fc]
F, [2017-11-14T15:41:47.290263 #10484] FATAL -- : [6d21f1c2-6770-42a2-93be-cd68ee0441fc] app/models/user.rb:41:in `authorization'
[6d21f1c2-6770-42a2-93be-cd68ee0441fc] app/models/user.rb:17:in `create_new_auth_token'
I thought I forgot to require net/http, but when I check the require statement was already there. I'm not understanding this at all. Further, I checked the console and its showing that the package has been included. The code:
require "uri"
require "net/http"
def authorization(client_id, token)
encrypted_token = Digest::SHA256.hexdigest(token)
params = { "token": encrypted_token, "client": client_id }
Net::HTTP.post_form(URI.parse("path to api"), params)
end
Does anyone have any insight as to what is going on?

Getting ActionView::Template::Error error when trying to make Rails Ajax call

I’m using Rails 4.2.3. In my “app/controllers/user_objects_controller.rb” controller file, I have defined this method …
def find_totals
respond_to do |format|
#current_user = User.find(session["user_id"])
format.json {
#month_total = UserObject.find_total_by_user_object_month_and_year(session["user_id"], params[:object], params[:month], params[:year])
#year_total = UserObject.find_total_by_user_object_and_year(session["user_id"], params[:object], params[:year])
render :json => {:month => #monthTotal,
:year => #yearTotal }
}
end
end
I’m trying to use Ajax to invoke this method, and so in my “./app/assets/javascripts/user_objects.js.coffee” script, I have
updateTotal = (arg) ->
object = $('#user_object_object').val()
date = $("#datepicker").datepicker('getDate')
day = $('#user_object_day').val()
month = date.getMonth() + 1
year = date.getFullYear()
if (object != "" && day != "")
# Populate the month and year totals
$.ajax
url: "/user_objects/find_totals"
type: 'GET'
data: {month: month, year: year, object: object}
success: (data) ->
alert(data);
$('#month_total').val(data)
$('#year_total').val(data)
error: ->
alert "Something went wrong getting month and year total"
but I always get the error condition. In my Rails server the below log is printed:
D, [2016-02-24T15:42:24.475092 #12929] DEBUG -- :
D, [2016-02-24T15:42:24.475206 #12929] DEBUG -- :
D, [2016-02-24T15:42:24.475236 #12929] DEBUG -- :
D, [2016-02-24T15:42:24.475265 #12929] DEBUG -- :
I, [2016-02-24T15:42:24.475505 #12929] INFO -- : Started GET "/user_objects/find_totals?month=2&year=2016&object=3" for 127.0.0.1 at 2016-02-24 15:42:24 -0600
I, [2016-02-24T15:42:24.475539 #12929] INFO -- : Started GET "/user_objects/find_totals?month=2&year=2016&object=3" for 127.0.0.1 at 2016-02-24 15:42:24 -0600
F, [2016-02-24T15:42:24.500710 #12929] FATAL -- :
ActionView::Template::Error (undefined method `object' for nil:NilClass):
2:
3: <p>
4: <strong>object:</strong>
5: <%= #user_object.object %>
6: </p>
7:
8: <p>
app/views/user_objects/show.html.erb:5:in `_app_views_user_objects_show_html_erb__2677150194333288712_70363943163180'
F, [2016-02-24T15:42:24.500751 #12929] FATAL -- :
ActionView::Template::Error (undefined method `object' for nil:NilClass):
2:
3: <p>
4: <strong>object:</strong>
5: <%= #user_object.object %>
6: </p>
7:
8: <p>
app/views/user_objects/show.html.erb:5:in `_app_views_user_objects_show_html_erb__2677150194333288712_70363943163180'
Anyone know what this error means and how I can fix it?
It looks like Rails is trying to render show.html.erb, which implies that the ajax request is hitting the show action on your UserObjects controller, which would be indicative of incorrect routes.
Check your routes file. Routes are matched in the order they're specified, so if you have resources :user_objects before you have something like get "user_objects/find_totals" ... then Rails will think the "user_objects/find_totals" URL matches to the show action on your UserObjects controller, with an id of find_totals.
undefined method `object' for nil:NilClass
means that #user_object here
<%= #user_object.object %>
is nil. Which makes sense given that #user_object is no where to be found in your controller method/action def find_totals
You will need to set that instance variable somewhere in your controller for it to be available for you in your view
#user_object is nil.
undefined method `object' for nil:NilClass

Ruby error: undefined method `casecmp' for nil:NilClass

I have the following code (with a few debug lines added):
Ruby:
re_dict = {}
re_dict['state'] = 'pending' #set initial status to pending
puts re_dict, re_dict.class.to_s
puts re_dict['state'], re_dict['state'].class.to_s
puts re_dict['state'].casecmp('pending')
while re_dict['state'].casecmp('pending') == 0 do
stuff
end
Output
state: pending
state class: String
class compared to 'pending': 0
Completed 500 Internal Server Error in 66ms
NoMethodError (undefined method `casecmp' for nil:NilClass):
What is causing this? How am I losing the value of my hash?
This will happen only when you remove 'state' key from re_dict hash inside your while loop:
while re_dict['state'].casecmp('pending') == 0 do
puts re_dict
re_dict = {}
end
#=> {"state"=>"pending"}
#=> NoMethodError: undefined method `casecmp' for nil:NilClass
Since, key 'state' is not available anymore, calling re_dict['state'] will give nil, that's why you're getting undefined method casecmp' for nil:NilClass

Problem with Rails 3 and AMF , rails3-amf, RocketAMF

im trying to get AMF to work with Rails3.
I have succesfully installed rails3-amf-0.1.0 gem and the RocketAMF-0.2.1 gem.
In my app there is a controller with the following code:
def getRandomCards
#incoming = params[0]
#cards = Cardvo.first
respond_with(#cards) do |format|
format.amf { render :amf => #cards.to_amf}
end
end
through a call from Actionscript i would like to return some data in amf format.
further more, as mentioned in the instructions for rails3-amf i did the following.
in my production.rb under config/environment i added the line
config.rails3amf.map_params :controller => 'CardvosController', :action => 'getRandomCards'
an my amf gateway got
config.rails3amf.gateway_path = "/gateway"
The problem is:
Any call from Actionscript / Flash raises the following
(taken from the log )
Started POST "/gateway" for 192.178.168.1 at Fri Nov 19 15:13:28 +0100 2010
Processing by CardvosController#getRandomCards as AMF
Parameters: {0=>100.0}
[1m[36mSQL (0.4ms)[0m [1mSHOW TABLES[0m
[1m[35mCardvo Load (0.2ms)[0m SELECT `cardvos`.* FROM `cardvos` LIMIT 1
Completed 200 OK in 13ms (Views: 0.9ms | ActiveRecord: 0.5ms)
NoMethodError (undefined method `constructed?' for #<RocketAMF::Envelope:0x39ba868>):
The Amf file is created but the method, which is in remoting.rb from RocketAMF could not be found.
I think the error is thrown at request_parser.rb from Rails3AMF asking for constructed?
# Wrap request and response
env['rack.input'].rewind
env['rails3amf.request'] = RocketAMF::Envelope.new.populate_from_stream(env['rack.input'].read)
env['rails3amf.response'] = RocketAMF::Envelope.new
# Pass up the chain to the request processor, or whatever is layered in between
result = #app.call(env)
# Calculate length and return response
if env['rails3amf.response'].constructed?
For me it seems it is looking at the wron class for the method.
Where
NoMethodError (undefined method `constructed?' for #RocketAMF::Envelope:0x39ba868):
the essential part is
RocketAMF::Envelope:0x39ba868
which should be
RocketAMF:ANOTHER_CLASS:Envelope:0x39ba868
Am i right and where the heck is the error ?
Any help would be appreciated!
chris

Resources