How to handle params conditions in ruby on rails - ruby-on-rails

I want to apply multiple conditions for paraams, I have a below code:
if !params[:parametersSch].blank?
#dynamicDegree = params[:parametersSch]
#dynamicFrom = params[:ParametersFromSch]
#dynamicTo = params[:ParametersToSch]
In the above code "parametersSch" is my text_field name, "ParametersFromSch" is a dropdown name that have years and "ParametersToSch" is also dropdown that also have years and the above code working fine that if params[:parametersSch] is not equal to blank then it puts values in vaiables, but when this condition is false means params[:parametersSch] is equal to blank then I puts empty string in a variables i.e below:
#dynamicDegree = ''
#dynamicFrom = ''
#dynamicTo = ''
But its not working, Kindly suggest me. Thanks

try this
if params.has_key?(:parametersSch) && params[:parametersSch].present?
#dynamicDegree = params[:parametersSch]
#dynamicFrom = params[:ParametersFromSch]
#dynamicTo = params[:ParametersToSch]
else
#dynamicDegree = nil
#dynamicFrom = nil
#dynamicTo = nil
end

Related

Why aren't these 2 strings equal

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?

IE11 clears password fields on page refresh:

Happens only in IE (Chrome and Firefox, no issues... go figure)
I have a page where customer's can update their details (name, address,password etc.) and everything is working fine, however if the customer submits the form (which also works fine) and then presses the back button all the customer's information will repopulate the form except for the password field.
My boss would like this to repopulate as well, like it does in Chrome and Firefox, but IE won't do it. I'm hoping it's something simple I've missed but I can't see it. I've tried adjusting the lines where the text fields are populated to match the rest of the form, but that just results in empty fields. Code below.
Page_Load
If TypeOf Session("Customer") Is GPCUser Then
c = CType(Session("Customer"), GPCUser)
Else
Response.Redirect("default.aspx")
End If
If Not Page.IsPostBack Then
If c.CustomerID > 0 Then
'populate the table
lblAccountName.Text = c.AccountName
txtFirstName.Text = c.FirstName
txtLastName.Text = c.LastName
txtEmail.Text = c.Email
txtAddress.Text = c.Address
txtSuburb.Text = c.Suburb
txtCityTown.Text = c.City
txtPostcode.Text = c.PostCode
txtPhone.Text = c.Phone
txtMobile.Text = c.Mobile
'chkNewsletter.checked = c.Newsletter
txtPassword.Attributes.Add("value", c.GeneratedPassword)
txtConfirmPassword.Attributes.Add("value", c.GeneratedPassword)
'txtPassword.Text = c.GeneratedPassword
'txtConfirmPassword.Text = c.GeneratedPassword
Dim subscriptions As ContactSubscriptions = New ContactSubscriptions(c.CustomerID)
chkGenernalNewsletters.Checked = subscriptions.IsGenernalNewsletters
End If
Else
End If
Update Button
If TypeOf Session("Customer") Is GPCUser Then
c = CType(Session("Customer"), GPCUser)
Else
Exit Sub
End If
c.AccountName = lblAccountName.Text
c.FirstName = txtFirstName.Text
c.LastName = txtLastName.Text
c.Email = txtEmail.Text
c.Address = txtAddress.Text
c.Suburb = txtSuburb.Text
c.City = txtCityTown.Text
c.PostCode = txtPostcode.Text
c.Phone = txtPhone.Text
c.Mobile = txtMobile.Text
'c.Newsletter = chkNewsletter.Checked
c.GeneratedPassword = txtPassword.Text
c.CustomerUpdatedRequired = false
'Update password field
txtPassword.Attributes.Add("value", c.GeneratedPassword)
txtConfirmPassword.Attributes.Add("value", c.GeneratedPassword)
GPCUser.AddUpdateCustomer(c)
subscriptions.IsGenernalNewsletters = chkGenernalNewsletters.Checked
subscriptions.Save()
Session("Customer") = c
lblMessage.Text = "Your details have been successfully updated."
pnlUpdateAccount.Visible = False

Ruby/Rails using || to determine value, using an empty string instead of a nil value

I usually do
value = input || "default"
so if input = nil
value = "default"
But how can I do this so instead of nil It also counts an empty string '' as nil
I want so that if I do
input = ''
value = input || "default"
=> "default"
Is there a simple elegant way to do this without if?
Rails adds presence method to all object that does exactly what you want
input = ''
value = input.presence || "default"
=> "default"
input = 'value'
value = input.presence || "default"
=> "value"
input = nil
value = input.presence || "default"
=> "default"
I usually do in this way:
value = input.blank? ? "default" : input
In response to the case that input might not be defined, you may guard it by:
value = input || (input.blank? ? "default" : input)
# I just tried that the parentheses are required, or else its return is incorrect
For pure ruby (not depending on Rails), you may use empty? like this:
value = input || (input.empty? ? "default" : input)
or like this (thanks #substantial for providing this):
value = (input ||= "").empty? ? "default" : input
Maybe irrelavant but I would use highline like this:
require "highline/import"
input = ask('Input: ') { |q| q.default = "default" }
It works without Rails. Really neat solution.

corona multi dimensional tables

I am trying to create a multi dimensional table like so
answers = {}
for i = 1, #answers do
answers[i] = { wrong = t[i], wrong2 = t2[i], right = t3[i]}
end
print(answers[1].wrong)
But when I try to access answers[1].wrong, I get error: attempt to index field '?' (nil value)
But when I do it this way it prints it for me
answers = {
{wrong = t[1], wrong2 = t2[1],right = t3[1]},
{wrong = t[2], wrong2 = t2[2],right = t3[2]}
}
print("----------")
print(answers[1].wrong)
print(answers[1].wrong2)
Why is the first method not working?
answers = {}
for i = 1, #answers do
#answers is 0. So the loop won't execute. You probably meant #t or something.

Wrong Number of Arguments

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.

Resources