Text input value as a number - ruby

I want input field value to be a number instead of string.
A simple scenario is you have 2 input fields and submit button on a page. When you click submit you should get sum of numbers keyed in both the input fields and not appended strings.
I tried using "number_field_tag" for input type=number but the value is still a String and not Fixnum what I want.

As Surya stated in his comment, what comes from the form fields is always a string. But you can do something like this in the controller action that processes the form (presuming you're using Rails):
def process_form
#result = params[:first_field].to_i + params[:second_field].to_i
end

Related

IMPORTXML To Return a Value from dividendhistory.org

I want to return the value of "Yield:" from a series of stocks from a URL dividenhistory.org, for example HDIF into a Google sheet using IMPORTXML, where F7 represents the user supplied ticker.
=IMPORTXML("https://dividendhistory.org/payout/TSX/"&F7, "/html/body/div/div[2]/div[2]/p[4]")
The problem with the above is the yield value is not always located in the same paragraph, depending on the ticker. It also returns with the word "Yield:" as part of the value.
I believe I should be using the XPATH parameter which should find and return the yield value only, but I am lost. I am open to all suggestions!
I tried with a few of the tickers there, and this should work. For example:
=IMPORTXML("https://dividendhistory.org/payout/ctas/", "//p[contains(.,'Yield')]/text()")
Output:
Yield: 1.05%
Obviously, you can change 'ctas' for any user input.
Try this and see if it works on all tickers.
EDIT:
To get only the number 1.05, you need to split the result and output the 2nd part:
=index(split(IMPORTXML("https://dividendhistory.org/payout/ctas/", "//p[contains(.,'Yield')]/text()"), ": "),2)
Output:
0.0105

Can We generate 2 pairs of (key, value) in one map function? If yes how?

I have a dataset of userID and a post related to each UserID.
I want to count the number of posts by each user. I also want to put all the posts of each userID together (concat all the posts with some separation).
Any suggestions how to go about it?
IMHO, you can have a mapper and a reducer.
Mapper:
class PostMapper extends Mapper < Object, Text, Text, Text>
map() can write a key which is the UserID (Text) and a value which is a Post(Text) to the Context.
Reducer:
class PostReducer extends Reducer < Text, Text, Text, Text >
reduce() can have an iterable loop with (i) a counter that counts
for every fetched Post and (ii) a Text variable can be used to
concatenate every fetched Post with suitable delimiter.
After completing the loop, the key / UserID and, the value / the
concatenated Text can be written to reducer's context.
After the job ran successfully, the resulting file would contain the UserID and the concatenated posts, separated by a tab.
Note: Remove all tab characters in the posts before you concatenate. Prefix the count followed by a tab and append it with concatenated posts if you want the count also in the output.
Your key in the key/value pair would be the userId. The value would be a list of strings (the messages). Most lists have a count property.
The information you are looking for would be accessed something like this:
var userId = 39;
Get the first message of user 39: userMessages[userId][0].
Get the number of message posted by user 39: userMessages[userId].Count()

Getting model value and generated ID from within simple_form custom input

I'm trying to make a custom input type with simple_form that will implement combobox-type functionality using jQuery-Autocomplete
. What I need to do is output a hidden field that will hold the ID of the value selected and a text field for the user to type in.
Here's what I have so far:
class ComboboxInput < SimpleForm::Inputs::Base
def input
html = #builder.hidden_field(attribute_name, input_html_options)
id = '' #what?
value = '' #what?
return "#{html}<input class='combobox-entry' data-id-input='#{id}' value='#{value}'".html_safe
end
end
I need to get the ID of the hidden field that simple_form is generating to place as an HTML attribute on the text entry to allow the JavaScript to "hook up" the two fields. I also need to get the value from the model to prepopulate the text input. How do I do this from within my custom input?
I'm looking for the id as well, but I did get the value:
def input
current_value = object.send("#{attribute_name}")
end
I just found a hokey id workaround:
html = #builder.hidden_field(attribute_name, input_html_options)
id = html.scan(/id="([^"]*)"/).first.first.to_s
I know it's a hack, but it does work. Since we don't have access directly to this type of resolution, it is likely to keep working even if the underlying id creation code changes.

getting the value of text field using rspec selenium

How do we use selenium webdriver + ruby to check to see if the value of a text field is equal to a certain value?
I was doing:
#tester.browser.find_element(:id => "id_of_text_field").text.should == 'test value'
Why doesn't that work?
this test failed ... couldn't get the value of the text field.
Text fields do not have text. The value you see in the text field is actually the value of their value attribute.
You can get an element's value attribute by doing:
element['value']
Therefore, your test needs to do:
#tester.browser.find_element(:id => "id_of_text_field")['value'].should == 'test value'
Write as below using should eql(expected)
Passes if given and expected are of equal value, but not necessarily the same object.
# I write in below way, just for readability, you can write it in one line.
elem = #tester.browser.find_element(:id => "id_of_text_field")
elem.text.should eql('test value')

Can I add params values to a hash?

I have a User model. I also have a form_for(#user...) form. This form spans 3 partials. In order for every partial to remember values I use the following command inside my create action in my UsersController:
session[:user_params].deep_merge!(params[:user]) if params[:user]
This way every partial adds params[:user] to session[:user_params]. I also have other form values stored inside the params hash which are not part of the User model. Is there a command which would allow me to add all single params values (not just the :user hash) to the session[:user_params] hash without adding every single value one by one like this:
session[:num_children] = params[:num_children] if params[:num_children]
...etc...
Try:
params.each {|key,value| session.deep_merge!(key=>value)}

Resources