I am making a rails site and I am trying to take a text box input and convert it to a big decimal value to pass to a backend service however when I take the input (params[:amount]) and do what I think should convert this to a BigDecimal I get an error when attempting to call the service saying "error cannot convert String to BigDecimal" on the line of the service call. See approximate code below
#amt = BigDecimal(params[:amount])
Service.call(#amt)
The error message indicates it is expecting a string in Service.call and wants to do the conversion itself. Assuming you have reasons for creating #amt (such as validation/error checking), I would pass in #amt.to_s and see what happens
Barring that, we need the code behind Service.call() to know more.
Related
We are creating a grpc server using proto3. and compiling it into a ruby function.We have converted the Active Record Message into a protobuf-message (got by calling the "activerecord".to_proto method) using ActiveRecord-Protobuf gem. However while creating the protobuf message to create the ruby server we are not able to pass the "activerecord".to_proto message as while defining the type of the input value we have no other go but to define it as a message in proto3 and hence it only accepts a hash value. inorder for which we have to convert our .to_proto object to "activerecord".to_proto.to_hash. This is futile and reduces the optimality of grpc. Ironically we are shifting to grpc for its optimality. Could you suggest how to define the protobuf message (using proto3) to ensure the "activerecord".to_proto message is compatible with the proto3 input value definition.
This is the active record object.
class AppPreferenceMessage < ::Protobuf::Message
optional ::Protobuf::Field::Int64Field, :id, 1
optional ::Protobuf::Field::StringField, :preference_key, 2
optional ::Protobuf::Field::StringField, :value, 3
optional ::Protobuf::Field::StringField, :value_type, 4
optional ::Protobuf::Field::Int64Field, :vaccount_id, 5
end
This is converted to AppPreference.last.to_proto which is a protobuf message by class.
my protobuf definition of the ruby input parameters is as follows.
syntax="proto3";
service App_Preferences{
rpc Index(Empty) returns (Index_Output){}
}
message Index_Output{
int64 id=1;
string preference_key=2;
string value=3;
string value_type=4;
int64 vaccount_id = 5;
}
This parameter "Index_Output" only accepts AppPreference.last.to_proto.to_hash but however I want it to accept AppPreference.last.to_proto as an input. How do i change my protobuf code.
I think you would like to convert this AppPreferenceMessage object to a protobuf message, and then pass that to an rpc method which has been created from a separate proto file? Also I'm unfamiliar with what AppPreferenceMessage.to_proto returns, is it serialized bytes?
I'm not sure that I'm completely clear, but it sounds like you might not want to use the grpc-protobuf stub and service code generators, as these are designed with passing of ruby protobuf objects in mind.
There are some examples in https://github.com/grpc/grpc/tree/master/examples/ruby/without_protobuf that might be useful if skipping the grpc-protobuf code gen is what you need.
My Rails application stores strings containing html entity codes, e.g. "Θ", which display Greeks and other characters on browser pages. To display these same characters in Prawn documents, I need to convert "Θ" to "\u0398". Using a regexp I can extract the bare codepoint, "0398", from the original string. But I'm unable to use this to create a new string variable containing "\u0398".
I've tried many variations of string concatenation, interpolation and even array operations, but no joy. Anything that looks like
new_string_var = "\u" + my_codepoint
generates an "invalid Unicode escape" error at "\u".
Anything that looks like
new_string_var = "\\u" + my_codepoint
runs without error but inserts the literal string "\u0398" in the Prawn document.
Is it possible in Ruby to construct a string like this? Is there a better approach?
Actually, you don't need \uxxxx notation - this is for display purposes in Ruby. Try CGI.unescapeHTML(string_with_entities) from built-in CGI module.
In my struts2 application I have field named carrierNo that accepts integer, when i put string in it gives me this validation error message:
*Invalid field value for field "carrierNo".*
i can customize this error message in the properties file like this
invalid.fieldvalue.carrierNo=this field does not accept characters
but i don't want to write a customized message for every non String field in my web application, i want to make it general, i tried the following but it did not work
invalid.fieldvalue.%{getText(fieldName)}=this field does not accept characters
if there is no way to make general, please help me disable this message at all.
then i will use converstion field validator with single message that i define in the properties file.
so my request is to help me make this invalid.fieldvalue.carrierNo general something like this form invalid.fieldvalue.%{getText(fieldName)}
or disable the display of this error message Invalid field value for field "carrierNo".
You could create your own implementation of ConversionErrorInterceptor which finds out the class of failed field and gets your custom message.
Edit:
See source code for ConversionErrorInterceptor. For example you could do something like this in your custom interceptor inside intercept method
// get field by name from action
Field f = invocation.getAction().getClass().getDeclaredField(propertyName);
// get type of field
Class clz = f.getType();
String message = LocalizedTextUtil.findDefaultText(XWorkMessages.DEFAULT_INVALID_FIELDVALUE + "." + clz,
invocationContext.getLocale());
And in your messages.properties file put xwork.default.invalid.fieldvalue.int, xwork.default.invalid.fieldvalue.float, etc.
The easiest way to remove conversion messages is to remove the "conversionError" interceptor from your default stack. One problem with removing it, however, is that IIRC it's also responsible for putting the original (non-converted) value back into fields instead of having them replaced by the value of the failed conversion. This can lead to an unpleasant user experience, IMO.
Making a "... does not accept characters" conversion error message doesn't feel right: conversion errors encompass the entire application, and characters may not be the reason for a conversion error.
On my code I got a image from camera but I want to upload it on the server, I convert the image to byte array I sent it to the url of php server can any one tell me Which type of code I have to write.
One problem is that the byte array data is 11 character in length my PM told me that the byte you got is too small
I got the byte array as follows
[B#f359616f
when i run this code at php side the imagecreatefromstring($images); not create the image
Any code plz help me
It looks like you're calling 'toString()' on a byte array. This means you are calling the default implementation of toString provided by the VM, which is to format the type name, then '#' then some object specific int, likely the object identity hash code.
Typically, StackOverflow questions include source code. In this case, you would want to include the function that is uploading the image data. People helping you would be interested in the datatypes involved, and how you are formatting and delivering that data to the server's php script.
I am working on an email validation link for a website. When a user registers and finishes filling in their personal data (and it passes all the checks), they are sent to a jsp page saying that an email has been sent to the address they entered as the username, with a link to click to validate the email address. So that part is all well and good, I generate the link (for now just using my localhost) and it looks like this as an example http://localhost:9999/javawork/msc/validate/?6FRQ8RAT&u=1s3w1Iih64egX01188HT. When they click the link it goes to the jsp page index.jsp in the validation folder. At this point I need to grab the entire URL and send it to a function to make sure the URL is formatted properly (for security purposes). If it passes and the format is fine, I need to grab the 8 digit code immediately after the '?' and also the value of 'u'. I then send those values to a function that checks that they match what we have in our DB, and if they do, I update the DB record with a validation date so we know they have validated their email address.
So my question is first, how do I grab the entire URL to check the format, and second, how do I grab the 8 digit code, and the value of 'u'? I have been looking online and all examples require creating multiple functions or classes, and using the URL class. And they all want me to make an instance of a URL object and initialize it using the entire URL. But it is not a static URL, it will be different for every user that registers, as it generates a random 8 digit code to check against, and the value of 'u' is the masked user id from the DB. I don't understand how it can require you to initialize the entire URL in order to get the values, when you don't know what the values are until you get them from the URL.
Is there a simple way to grab the values, and the entire URL? Even if I can just get everything after the '?', I know the base URL and can build a new String to check the formatting if I can get from the '?' and after. Please help with that part. Thanks.
The Interface HTTPServletRequest contains a method getRequestURL which returns a StringBuffer which you may use to check the format of the entire URL.
You can get it, in a jsp page with :
<%=request.getRequestURL()%>
If you are using the format of request that you specified above, then your second question :
how do I grab the 8 digit code, and the value of 'u'?
May be answered by manipulating that StringBuffer to split at the ? and & for the 8 digit code.
Or use another request method,
ServletRequest.getParameter(java.lang.String name)
To grab each parameters, though, i'm not certain how it will end up handling the unnamed parameter of the 8 digit code. Let me know how that goes.
Don't think of the 8-digit code as an unnamed parameter. Think of it as a parameter without a value.
request.getParameterNames() will give you the 8-digit code as well as "u". So you can loop through like so:
String code = "";
for(String paramName : request.getParameterNames()) {
if(!paramName.equalsIgnoreCase("u"))
code = paramName;
}