My form has the following:
<%= f.select(:amount, Order::AMOUNT_VALUES, {:blank => false}) %>
My model has:
AMOUNT_VALUES = { '$ 0.50' => 0.5, '$ 1' => 1, '$ 2' => 2, '$ 5' => 5, '$ 10' => 10 }
validate :amount_values_to_be_of_certain_values
def amount_values_to_be_of_certain_values
puts self.amount
unless AMOUNT_VALUES.has_value? self.amount
errors.add(:amount, 'not a valid field')
end
end
If I select 0.5 and submit the form. The form complains saying "Amount not a valid field". If I select one of the other values, the form submits fine.
It seems the problem is with a decimal point that starts with 0.
What is wrong here?
If you're storing amount in an integer field, then it's going to round any floating point numbers you put into it down. That's why self.amount.to_f is 0.0, because putting 0.5 into an integer field changes it to 0, and 0.to_f is 0.0.
Related
I'm using TYPO3. I programmed an extension called 'Eventmanager', to manage events.
After installing this extension, in Backend I can add/edit event-records. With each record there is a field called end_time, which is a datetime-picker, for choosing the date and time of the events.
Today I edit an event-record. I deleted the value of this end_time field (to make it empty), and tried to save this record. But an error occurred during saving:
2: SQL error: 'Incorrect integer value: '' for column 'event_end_time' at row 1' (tx_eventmanager_domain_model_event:13)
The Settings in TCA is shown as follows:
'endtime' => array(
'exclude' => 1,
'l10n_mode' => 'mergeIfNotBlank',
'label' => 'LLL:EXT:lang/locallang_general.xlf:LGL.endtime',
'config' => array(
'type' => 'input',
'size' => 13,
'max' => 20,
'eval' => 'datetime',
'checkbox' => 0,
'default' => 0,
'range' => array(
'lower' => mktime(0, 0, 0, date('m'), date('d'), date('Y'))
),
),
),
and the definition in the file 'ext_tables.sql' is shown as follows:
event_end_time int(11) DEFAULT '0' NOT NULL
Can someone tell my what is the reason? How can I solve this problem?
I'm trying to make CMaskedTextField as quantity input field with base quantity = 0, and maximum = 99.
Can't figure out the mask and/or charMap.
I've tried:
$this->widget('CMaskedTextField', array(
'model' => $position,
'attribute' => 'Quantity',
'mask' => '99',
'value' => $currentQuantity,
'htmlOptions' => array('size' => 2, 'maxlength'=>2, 'minlength'=>1)
), true);
but this mask means there must be only 2 digits
Optional (i guess js will do the work if cmaskedtextfield not): when user clears input it should converts to '0'.
Mask might be set thru regex:
\d{1,2}
In model rules() you might define:
array('attributeName', 'match', 'pattern' => '/\d{1,2}/',
'message' => '{attribute} can only contain digits from 0 to 99'),
There is no convertion to 0 but the explicit message for user.
I am following the instructions to implement auto complete in a rails 3.2.11 application but I need to specify a minimum number of characters to type before the query triggers. THe jQuery API documentation has an attribute "minLength". I can't figure out how to implement this in a rails auto complete field tag. Here is my code for the field tag.
<%= autocomplete_field_tag 'unit', '', autocomplete_unit_identifier_subjects_path, :id_element => '#subject_id', :size => 75 %>
Here is the url to the instructions I am following.
https://github.com/crowdint/rails3-jquery-autocomplete
If anyone is looking for an updated answer, it appears you can now set minimum length with the attribute 'min-length'.
<%= autocomplete_field_tag 'group_name', '', group_autocomplete_path, 'placeholder' => 'Select a Job Number', 'size' => 35, 'class' => 'styled-select', 'data-auto-focus' => true, 'min-length' => 1 %>
Why its not 'minlength' as documented in jQuery autocomplete, I don't know..
Well, minLength doesn't work because of this code in autocomplete-rails.js, line 65 or so:
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
You can change the '2' to whatever you want the minLength to be.
My form has:
<%= f.select(:amount, Payment::AMOUNT_VALUES, {:blank => false}) %>
My model has:
AMOUNT_VALUES = { '$ 0' => 0, '$ 1' => 1, '$ 2' => 2, '$ 5' => 5, '$ 10' => 10 }.
If user select $2 and submits form. The next time he goes back to the form, I need it only to display everything above $2 in the select box. Therefore, the options should be $5 and $10 only.
How can this be achieved?
I recently wrote some helper code that helped me rule out days of the week where a store had already posted business hours for. It shouldn't be too hard to adapt to your specifications.
Updated: Added question-specific code and updated with a more-efficient reject block.
module HoursHelper
...
def generateDayStrings (unavailable_days)
all_days = [ ['Sunday', 'Sun'],
['Monday', 'Mon'],
['Tuesday', 'Tue'],
['Wednesday', 'Wed'],
['Thursday', 'Thu'],
['Friday', 'Fri'],
['Saturday', 'Sat'] ]
all_days.reject do |day|
(day - unavailable_days).size != 2
end
end
...
end
<%= f.select :day_of_week, generateDayStrings(#days_with_hours) %>
# #days_with_hours = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
# generateDayStrings(#days_with_hours) = [ ['Sunday', 'Sun'], ['Saturday', 'Sat'] ]
For your particular codebase, it'd probably look something like this:
module PaymentsHelper
...
def generatePaymentAmountSelectArray (unavailable_denoms)
all_denoms = [ ['$ 0', '0'],
['$ 1', '1'],
['$ 2', '2'],
['$ 5', '5'],
['$ 10', '10'] ]
# or all_denoms = Payment::AMOUNT_VALUES
# although not sure how this works with a hash.
all_denoms.reject do |current_denom|
(current_denom - unavailable_denoms).size != 2
end
end
...
end
<%= f.select :amount, generatePaymentAmountSelectArray(#used_values), {:blank => false}) %>
# just make sure you set #used_values in your controller
I feel like there's a better way of doing this, so I'll update if I remember it.
Background:
For example I have the following data
headings = {
:heading1 => { :weight => 25, :views => 0, :conversions => 0}
:heading2 => { :weight => 25, :views => 0, :conversions => 0}
:heading3 => { :weight => 25, :views => 0, :conversions => 0}
:heading4 => { :weight => 25, :views => 0, :conversions => 0}
}
total_views = 0
I got to serve these headings based on their weightages. Every time a heading is served its views is incremented by one and total_views also incremented. And whenever a user clicks on a served heading its conversions is incremented by one. I've written a program (in Ruby) which is performing this well.
Question:
I need to Auto Optimize best converting heading. Consider the following views and conversions for all headings:
heading1: views => 50, conversions => 30
heading2: views => 50, conversions => 10
heading3: views => 50, conversions => 15
heading4: views => 50, conversions => 5
I need to automatically increase the weightage of heading(s) which is/are converting more and vice versa. The sum of weightage will always be 100.
Is there any standard algorithm/formula/technique to do this? There might be some other parameters that need to predefined before making these calculations. But I am not getting it through.
Pleas advise.
Thanks,
Imran
Well, a simple percentage calculation would do that.
30+10+15+5=60
30/60*100=50
10/60*100=17 (rounded)
15/60*100=25
5/60*100=8 (rounded)
So you get weightings of 50,17,25 and 8. You may or may not nead to watch out for rounding errors.