I am getting this error couldn't find id with id = '' i dont know how to fix it if any know how to fix it please tell me
#req2 = ActiveSupport::JSON.decode(#req)
##req3 = JSON.parse(#req2)
##reqId = #req3
#reqId = #req2[0]
#req3 = JSON.parse(#reqId)
#req3.each do |item|
#id = item['id']
#id = GaBadge.find(#id)
if #id.present?
#id.destroy
end
##obj = #id.destroy
end
I think, you are assigning value to #id as string.
Just try this,
#id = GaBadge.find_by_id(#id.to_i)
Error says GaBadge's table not having any record with id = 3, use
GaBadge.find_by_id(#id)
it will return nil instead of error.
Change your loop to
#req3.each do |item|
#id = item['id']
#ga_badge = GaBadge.find_by_id(#id.to_i)
unless #ga_badge.nil?
#ga_badge.destroy
end
end
i add .int and fixed
#req3.each do |item|
#id = item['id']
#ga_badge = GaBadge.find_by_id(#id.to_i)
unless #ga_badge.nil?
#ga_badge.destroy
end
end
Related
Going blind here.. I can't understand why these 2 strings are not equal.. When I puts them to the terminal they are both class string and when I just compare the output they ARE equal. But somehow in my code they are not.. I can't figure out why.
Here is my Ruby code:
def prep_for_duplicate_webhook
#redis_cart = Redis.new
cart_stamp_saved = #redis_cart.get("cart_stamp_saved")
if cart_stamp_saved.nil?
cart_stamp_saved = {}
cart_stamp_saved[:token] = cart_params['token']
cart_stamp_saved[:updated_at] = cart_params['updated_at']
#redis_cart.set("cart_stamp_saved", cart_stamp_saved.to_json)
end
#cart_stamp_incoming = {}
#cart_stamp_incoming["token"] = cart_params['token']
#cart_stamp_incoming["updated_at"] = cart_params['updated_at']
end
def duplicate_webhook?
prep_for_duplicate_webhook
#cart_stamp_saved = redis_cart.get("cart_stamp_saved")
cart_stamp_saved == cart_stamp_incoming.to_json
end
And then the hash's I'm comparing are these two:
cart_stamp_saved = {"token"=>"4a093432ba5c430dd545b16c0e89f187",
"updated_at"=>"2017-02-17T15:27:22.923Z"}
cart_stamp_incoming= {"token"=>"4a093432ba5c430dd545b16c0e89f187",
"updated_at"=>"2017-02-17T15:27:22.923Z"}
If I just copy and paste the above into a new page, and the do this, the response is true
pp cart_stamp_saved == cart_stamp_incoming.to_json
What am I missing?
Hi I am building a an app with rails and angular. I keep on getting a type error executing this code
$scope.makeTip = function(tip){
data = {
tip: {
bookies: tip.bookies,
user_id: $scope.currentUser.id
},
prediction: $scope.madePredictions
},
$http.post('/tips.json', data).success(function(data){
console.log(data)
});
};
$http.get('/predictions/fixtures_this_week').success(function(response){
$scope.fixturesThisWeek = response.data;
});
//Updating the scores the dependent of on the type of bet id
$scope.addPrediction = function(prediction, fixtureId) {
data = {};
data.fixtureId = fixtureId;
data.predictionGoalsHomeTeam = prediction.scores.predictionGoalsHomeTeam[fixtureId];
data.predictionGoalsAwayTeam = prediction.scores.predictionGoalsAwayTeam[fixtureId];
data.typeOfBet = prediction.typeOfBetId[fixtureId];
$scope.madePredictions.push(data);
console.log($scope.madePredictions)
}
}]);
However I believe the problem stems from the method in my tips controller, possibly on the 4th line down
def create
#tip = Tip.new(params[:tip])
#tip.save
#prediction = Prediction.find(params[:prediction][:fixtureId])
#prediction.predictionGoalsHomeTeam = params[:prediction][:predictionGoalsHomeTeam]
#prediction.predictionGoalsHomeTeam = params[:prediction][:predictionGoalsAwayTeam]
#prediction.save
#tip.predictions << #prediction
respond_with(#tip)
end
Does anyone have any idea about how I can approach this type error?
Thanks
A couple of errors.. Inside your create method you are assigning #prediction.predictionGoalsHomeTeam twice in a row.
#prediction.predictionGoalsHomeTeam = params[:prediction][:predictionGoalsHomeTeam]
#prediction.predictionGoalsHomeTeam = params[:prediction][:predictionGoalsAwayTeam]
So you need to change the last one to AwayTeam as that is what you are getting from the params.
Your main issue is that params[:prediction] is an array so Prediction.find(params[:prediction][:fixtureId]) won't work as you are trying to grab the fixtureId of that array which doesn't exist. You need to loop through the params[:prediction] and store each object in #tip.predictions individually. Try using this:
def create
#tip = Tip.new(params[:tip])
#tip.save
params[:prediction].each do |p|
#prediction = Prediction.find(p[:fixtureId])
#prediction.predictionGoalsHomeTeam = p[:predictionGoalsHomeTeam]
#prediction.predictionGoalsAwayTeam = p[:predictionGoalsAwayTeam]
#prediction.save
#tip.predictions << #prediction
end
respond_with(#tip)
end
i ve this method. I m not at all able to understand the error which is
Couldn't find Company without an ID
in ActiveRecord::RecordNotFound in CustomersController#bulk_create
This method is written to create customers for a company in bulk by taking their name and numbers in format name:number.
The method is as follows:
def bulk_create
res = ""
comp_id = params[:customer][:selected_companies].delete_if{|a| a.blank?}.first
comp = Company.find(comp_id)
s = SentSmsMessage.new
s.set_defaults
s.data = tmpl("command_signup_ok", customer, comp) unless params[:customer][:email].length > 0
s.data = params[:customer][:email] if params[:customer][:email].length > 0
s.company = comp if !comp.nil?
s.save
unless comp_id.blank?
params[:customer][:name].lines.each do |line|
(name, phone) = line.split(/\t/) unless line.include?(":")
(name, phone) = line.split(":") if line.include?(":")
phone = phone.gsub("\"", "")
phone = phone.strip if phone.strip.to_i > 0
name = name.gsub("\"", "")
name = name.gsub("+", "")
phone = "47#{phone}" if params[:customer][:active].to_i == 1
customer = Customer.first(:conditions => ["phone_number = ?", phone])
if customer.nil?
customer = Customer.new
customer.name = name
# customer.email
# customer.login
# customer.password
customer.accepted_agreement = DateTime.now
customer.phone_number = phone
customer.active = true
customer.accepted_agreement = DateTime.now
customer.max_msg_week = params[:customer][:max_msg_week]
customer.max_msg_day = params[:customer][:max_msg_day]
customer.selected_companies = params[:customer][:selected_companies].delete_if{|a| a.blank?}
res += "#{name} - #{phone}: Create OK<br />" if customer.save
res += "#{name} - #{phone}: Create failed<br />" unless customer.save
else
params[:customer][:selected_companies].each do |cid|
new_company = Company.find(cid) unless cid.blank?
if !new_company.nil?
if !customer.companies.include?(new_company)
customer.companies << new_company
if customer.save
res += "#{name} - #{phone}: Customer exists and the customer was added to the firm #{new_company.name}<br />"
else
res += "#{name} - #{phone}: Customer exist, but something went wrong during storage. Check if the client is in the firm.<br />"
end
else
res += "#{name} - #{phone}: Customer exists and is already on firm #{new_company.name}<br />"
end
end
end
end
s.sms_recipients.create(:phone_number => customer.phone_number)
end
s.save
s.send_as_sms
#result = res
respond_to do |format|
format.html { render "bulk_create"}
end
else
#result = "You have not selected any firm to add these users. Press the back button and try again."
respond_to do |format|
format.html { render "bulk_create"}
end
end
end
I want to update one situation here. That when i submit the form blank then it gives this error. Also if i filled the form with the values then its show the situation which the method is returning in case of fail.
res += "#{name} - #{phone}: Create failed <br />"
The tmpl method
private
def tmpl(setting_name, customer, company = nil)
text = ""
if customer.companies.count > 0
sn = "#{setting_name}_#{#customer.companies.first.company_category.suffix}".downcase rescue setting_name
text = Setting.value_by(sn) rescue ""
end
textlenth = text.length rescue 0
if textlenth < 3
text = Setting.value_by(setting_name) rescue Setting.value_by("command_error")
end
return fill_template(text, customer, company)
end
From the model customer.rb
def selected_companies=(cmps)
cmps.delete("")
# Check the old ones. Make a note if they are not in the list. If the existing ones are not in the new list, just remove them
self.companies.each do |c|
self.offer_subscriptions.find(:first, ["customer_id = ?", c]).destroy unless cmps.include? c.id.to_s
cmps.delete c.id.to_s if cmps.include? c.id.to_s
end
# Then create the new ones
cmps.each do |c2|
cmp = Company.find(:first, ["id = ?", c2])
if cmp && !c2.blank?
offerSubs = offer_subscriptions.new
offerSubs.company_id = c2
offerSubs.save
end
end
end
def selected_companies
return self.companies.collect{|c| c.id}
end
The association of customer is as follows:
has_many :offer_subscriptions
has_many :companies, :through => :offer_subscriptions
This code is written by the some one else. I m trying to understand this method but so far not being able to understand this code.
Please help.
Thanks in advance.
You are getting 'Couldn't find Company without an ID' error because your Company table doesn't contain record with id = comp_id
Change comp = Company.find(comp_id) to comp = Company.find_by_id(comp_id).
This will return nil instead of an error.
Add comp is not nil condition is already handled in your code.
Your comp_id line is returning nil.
comp_id = params[:customer][:selected_companies].delete_if{|a| a.blank?}.first
Post the params that get passed to this function and we could hopefully find out why. In the meantime you could enclose the block in a begin - rescue block to catch these errors:
begin
<all your code>
rescue ActiveRecord::RecordNotFound
return 'Unable to find a matching record'
end
try this:
comp = ""
comp = Company.find(comp_id) unless comp_id.nil?
instead of comp = Company.find(comp_id)
further nil checking present in your code.
Reason being
params[:customer][:selected_companies].delete_if{|a| a.blank?} = []
so [].first = nil
therefor, params[:customer][:selected_companies].delete_if{|a| a.blank?}.first = nil
and comp_id is nil
So check the log file and check what is coming in the parameter "selected_companies"
when you will find the parameter, everything will be understood well....
I understand what causes the wrong number of arguments error but my code doesn't pass any parameters to initialize any of the classes so I'm not sure at all why my code is giving me this error. I'm also pretty new to Ruby on Rails so that doesn't help things. My code is below:
def create_google_file
#products = Product.find(:all)
file = File.new('dir.xml','w')
doc = REXML::Document.new
root = REXML::Element.new "rss"
root.add_attribute("xmlns:g", "http://base.google.com/ns/1.0")
root.add_attribute("version", "2.0")
channel = REXML::Element.new "channel"
root.add_element channel
title = REXML::Element.new "title"
title.text = "Sample Google Base"
channel.add_element title
link = REXML::Element.new "link"
link.text = "http://base.google.com/base/"
channel.add_element link
description = REXML::Element.new "description"
description.text = "Information about products"
channel.add_element description
#products.each do |y|
item = channel.add_element("item")
id = item.add_element("g:id")
id.text = y.id
title = item.add_element("title")
title.text = y.title
description = item.add_element("description")
description.text = y.description
googlecategory = item.add_element("g:google_product_category")
googlecategory.text = y.googlecategory
producttype = item.add_element("g:product_type")
producttype.text = y.producttype
link = item.add_element("link")
link.text = y.link
imglink = item.add_element("g:image_link")
imglink.text = y.imglink
condition = item.add_element("condition")
condition.text = y.condition
availability = item.add_element("g:availability")
availability.text = y.availability
price = item.add_element("g:price")
price.text = y.price "USD"
gtin = item.add_element("g:gtin")
gtin.text = y.gtin
brand = item.add_element("g:brand")
brand.text = y.brand
mpn = item.add_element("g:mpn")
mpn.text = y.mpn
expirationdate = item.add_element("g:expiration_date")
expirationdate.text = y.salepricedate
end
doc.add_element root
file.puts doc
file.close
end
The error I'm getting is:
ArgumentError in ProductsController#create_google_file
wrong number of arguments (1 for 0)
At the request of the poster, I am putting my comments in to an answer:
Based purely on the consistency of the other lines, but without knowing which line is actually failing, it may be this part: price.text = y.price "USD". Is y.price a method that takes in a parameter? Is it defined as def price(type) or something? If not, if it doesn't take any parameters, then it's because you're not supposed to send any parameters to that method. It looks like it's just a getter.
#FranklinJosephMoormann As I suspected, that's the line. Were you trying to make a string like "4.50 USD"? Then you probably wanted: price.text = "#{y.price} USD". That will take the result of y.price and put it in a string, and allow you to keep typing more in the string. It's called string interpolation.
How can I save myself from typing
TypeOfVoyagePortActivity.find_by_description(activity_description)
twice?
This is the context I am using it in:
if TypeOfVoyagePortActivity.find_by_description(activity_description)
pl.activity = TypeOfVoyagePortActivity.find_by_description(activity_description)
else
pl.activity = TypeOfVoyagePortActivity.find_by_description("Custom")
end
model = TypeOfVoyagePortActivity
pl.activity = model.find_by_description(activity_description) ||
model.find_by_description("Custom")
You could assign result of find_by_description to a variable
if t = TypeOfVoyagePortActivity.find_by_description(activity_description)
pl.activity = t
else
pl.activity = TypeOfVoyagePortActivity.find_by_description("Custom")
end
Or as a better alternative:
p1.activity = [activity_description, "Custom"].each_with_object(nil) do |a, memo|
memo ||= TypeOfVoyagePortActivity.find_by_description(a)
end