Formatting date field in Sinatra form - html

I've got a form in Ruby/Sinatra where a date field is displayed:
%input{:type => "date", :name => "due_date", :value => #m.due_date}
On-screen, the form field presents as day-month-year (e.g. 23-11-2012).
I'd like the field content to be displayed as year-month-day (e.g. 2012-11-23).
Suggestions?

Try:
#m.due_date.strftime("%Y-%m-%d")
Take a look at http://www.ruby-doc.org/core-1.9.3/Time.html#method-i-strftime

Related

How to update by passing array values from HTML to Rails controller

I am trying to update a record from an html.haml form.
HTML.HAML:
%input{:name => "post[my_card_ids][]", :type => "hidden",value: "#{post["my_card_ids"]}",id: "my_card_ids"}
When I inspect the HTML page, the values are exactly like they should be.
HTML inspection:
<input id="my_card_ids" name="post[my_card_ids][]" type="hidden" value="[5d1b83a6616b6523a9020000, 5d1b9893616b653abd0b0000]">
But when I receive the same params in the controller, they become like this:
Rails Console:
<ActionController::Parameters {"my_card_ids"=>["[\"5d1b83a6616b6523a9020000\", \"5d1b9893616b653abd0b0000\"]"]
The record is being saved with all the extra paranthesis and quotation marks, because it is being passed as a string I'm guessing.
How do I pass the array cleanly, as shown in the HTML inspection and update the record?
In order to create an array in the parameters you want to create a hidden input for each value in the array:
- post["my_card_ids"].each do |card_id|
%input{ name: "post[my_card_ids][]", type: "hidden", value: card_id, id: "my_card_ids_#{card_id}" }
This works since Rack merges parameters with names ending with [] into an array. Its also how the built in FormOptionsHelper works.
You put array as string to hidden field.
Try:
%input{:name => "post[my_card_ids]", :type => "hidden",value: "#{post["my_card_ids"].join(','}",id: "my_card_ids"}
And in the controller
params[:my_card_ids].split(',')

kendo grid date column format after filtering

I have a kendo MVC grid that has a bound column like below
columns.Bound(c => c.CreatedDate).Format("{0:M/d/yyyy h:mm tt}").Title("Submitted on").Filterable(ftb => ftb.Cell(cell => cell.Operator("contains"))).Format("{0: MM/dd/yyyy HH.mm.ss}");
the column formats fine when first loading the view:
06/22/2017 15.02.00
but i have some buttons which use AJAX to post back and get back filtered data and when re-populating the grid the column looks like this:
/Date(1498161720000)/
Any help?
First off, you have two seperate .Format tags with different formats specified, which is probably causing some problems. Pick which one you want to use and try just removing the other.
If that doesn't solve the problem, I would try declaring the format using data annotations. In your model, try adding this line above the declaration of CreatedDate:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:M/d/yyyy h:mm tt}")]
and then remove .Format from your column binding.
i.e. change
columns.Bound(c => c.CreatedDate).Format("{0:M/d/yyyy h:mm tt}").Title("Submitted on").Filterable(ftb => ftb.Cell(cell => cell.Operator("contains")));
to
columns.Bound(c => c.CreatedDate).Title("Submitted on").Filterable(ftb => ftb.Cell(cell => cell.Operator("contains")));
and make sure you include the
using System.ComponentModel.DataAnnotations;
line at the top of your model if you don't already have it.

How to remove decimal values display from form field that is automatically coming from backend

In my form field i want to enter only integer values. i am using ruby moneyrails gem. my field name in db is amount_paisas. so when it is displayng in form it shows 0.00 . i need only 0 to dispaly there. how to avoid decimal values . i tried to give placeholder but no changes.
This is what iam getting in form display.here i need to avoid values after point
This value is automatically coming from backend. how to remove this?
my input field
= f.input :amount, label: 'Amount'+'('+#cart.currency+')', :input_html=>{:'data-validation' => 'required', :'data-validation-error-msg' => 'Please Select Amount'}
here iam not giving any value. but it is automatically getting 0.00 as its placeholder value from backend. this i need to avoid.
when i inspectin this form field i am getting like this
<input data-validation="required" data-validation-error-msg="Please Select Amount" class="string optional form-control valid" type="text" value="0.00" name="e_gift_card[amount]" id="e_gift_card_amount">
Any help is appreciatable ..
You have 2 options here:
Configure default formatting
Create config/initializers/money.rb file with the following content:
MoneyRails.configure do |config|
config.default_format = {
:no_cents_if_whole => true,
# :symbol => nil,
# :sign_before_symbol => nil
}
end
This configuration won't output cents if you have whole amount, i.e. $5 instead of $5.00.
Use custom helpers
If you don't want your cents at all, just use custom helpers:
money_without_cents(#money_object) # => 6
money_without_cents_and_with_symbol(#money_object) # => $6
To use it in your ruby code:
= f.input :amount, label: 'Amount'+'('+#cart.currency+')', :input_html=>{ :value => money_without_cents(#cart.amount), :'data-validation' => 'required', :'data-validation-error-msg' => 'Please Select Amount'}

Cakephp 3.0 I would like to include a Year input field with a drop down, but it is inputing as an array

I have a Year field in a form and I am using FormHelper.
echo $this->Form->input('year', [
'type' => 'year',
'minYear' => date('Y')-10,
'maxYear' => date('Y')
]);
The table file validator looks like:
->add('year', 'valid', ['rule' => 'numeric'])
->allowEmpty('year')
I have a very similar input in another app that seems to work fine. I set the MySql column to int(5) to match what I had working elsewhere.
Checking debugkit it shows the "year" input as an array while the other inputs are strings. If I remove the validation rule it throws an illegal array to string conversion, so I assume this is where the error is.
Any help is greatly appreciated.
I have just tested with your above code and it is working fine for me. Try to delete the cache and check it once more.
Creates a select element populated with the years from minYear to maxYear. Additionally, HTML attributes may be supplied in $options. If $options['empty'] is false, the select will not include an empty option:
empty - If true, the empty select option is shown. If a string, that
string is displayed as the empty element.
orderYear - Ordering of
year values in select options. Possible values ‘asc’, ‘desc’. Default
‘desc’ value The selected value of the input.
maxYear The max year to
appear in the select element.
minYear The min year to appear in the
select element.
Try this one:
<?php
echo $this->Form->year('exp_date', [
'minYear' => date('Y')-10,
'maxYear' => date('Y'),
'id' => 'cc-year',
'class' => 'form-control',
'empty' => false,
'orderYear' => 'asc'
]);
?>
Official Documentation: CookBook - Creating Year Inputs

Keeping form data in Rails

I have an HTML form in Rails. Sometimes when the user submit data, my server will reject it because the data is not appropriate (say, the user types a non-number in a field where there's supposed to be only numbers.)
<%= form_tag "myproject/win", :id => "win_form" do -%>
...
<label>
Year
<%= text_field_tag "year", '', :size => 20 %>
</label>
...
<%= submit_tag "Go!" %>
<% end -%>
In that case, my controller returns an error message
#notice = "Year must be a number"
return render action: "new"
But all the data that the user typed in disappears, and the user has to restart with a blank form. Is there a way to keep that data for the user?
That's fine if you don't have a data store behind this, but you can still leverage ActiveModel to create an object that you can use to help build forms (or use the reform gem). If you really don't want to do that, despite the additional capabilities it affords you, you can just use what you have now, but pay attention to the arguments:
From http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_field_tag:
text_field_tag(name, value = nil, options = {})
Creates a standard text field; use these text fields to input smaller chunks of text like a username or a search query.
Your code:
text_field_tag "year", '', :size => 20
So you're passing in '' as the value, and asking us why the value is ''. Perhaps what you really want is this:
text_field_tag "year", params[:year], :size => 20