I am using Rails 4.1.6 and Ruby 2.1.5 and for view I am using html.haml file.
In Form Page I want input date format should be in mm/dd/yyyy format.
For this in in config/locales/en.yml file i have:
date:
formats:
long: ! '%-m/%-d/%Y'
In _form.html.haml I have:
f.input :date_of_birth,:input_html =>{:format => :long}
The problem I have is it's not working. Any suggestions?
Try -
= f.input :date_of_birth, :value => t(#model.date_of_birth,:format => :long)
Related
I got following ruby example:
require 'tilt'
data = { "site_link" => "http://www.example.com", "title" => "example"}
template = Tilt.new('../templates/test.erb', :default_encoding => 'UTF-8')
output = template.render(data)
puts output
and this is test.erb file:
This should be a link - <%= site_link %>
I can't find a proper syntax to get a value from data hash into template.
Ok,
it looks like I need to specify to tilt that data are a hash. Correct code is:
output = template.render(Hash,data)
I am using simple_form to generate date input like so:
= f.input :date_of_death
However, it always pre-populates with the current date.
How can I have it come up "blank" and allow the user to easily NOT enter a date?
Thanks!
There are a couple of ways to do this while still keeping the form split into dropdowns for Y/M/D.
= f.input :date_of_death, include_blank: true
Or if you want the first option to have text use prompt.
= f.input :date_of_death, prompt: "Choose Wisely"
Simplest way that I have found
f.input :date_of_death, as: :string
It will turn the input into a blank input field. You will have to parse the string as a date in your controller.
Use include_blank
= f.input :fecha_at, include_blank: true, start_year: Date.today.year - 90, end_year: Date.today.year
I am having trouble populating a field with two columns concatenated. For example: I want to populate Worker field with both first name and last name in AJAX rails 4.
Currently I'm populating with just first name using
<%= collection_select(:work_order ,:worker_id ,Worker.all, :id ,:first_name) %>
You can do something like:
<%= f.input :user_id, :as => :select, :collection => [[current_user.firstname+' ' +current_user.lastname, current_user.id]], :label=>"Names"%>
I'm using the axlsx ruby gem to create Excel-compatible .xlsx files. I can't figure out how to override the cell type that is generated by it's automatic type detection. For Active Record model attributes of type string the gem is setting the Excel cell format to General, but I want it to use Text explicitly. That way I can avoid stripping leading zeros off of zip codes, etc.
Anybody know how to accomplish this?
You can override the type of data using the types option on add row.
Something like:
worksheet.add_row ['0012342'], :types => [:string]
Grab me on irc (JST) if you need any help getting that to work.
Best
randym
edit --
I've added an example for this to examples/example.rb in the repo.
wb.add_worksheet(:name => "Override Data Type") do |sheet|
sheet.add_row ['dont eat my zeros!', '0088'] , :types => [nil, :string]
end
https://github.com/randym/axlsx/blob/master/examples/example.rb#L349
format_code: '#' will work for you. Please find below code for reference.
def default_data_type_as_string
#xlsx_package = Axlsx::Package.new
#workbook = #xlsx_package.workbook
#worksheet = #workbook.add_worksheet(:name => "Introduction")
default_style = #workbook.styles.add_style({ format_code: '#' })
row_data_array = ['1', '2%', '3$']
#worksheet.add_row row_data_array, :style => [nil, default_style, nil]
#xlsx_package.serialize('default_data_type_as_string.xlsx')
end
For gem versions gem 'axlsx', '2.1.0.pre', gem 'axlsx_rails' in order to have the file columns in text type should specify both style and type
default_style = worksheet.styles.add_style({ format_code: '#' })
worksheet.add_row ['0012687'], :types => [:string], :style => [default_style]
I had a recent error using omniauth trying to populate some fields from Google's login
Encoding::CompatibilityError: incompatible character encodings:
ASCII-8BIT and UTF-8
"omniauth"=>
{"user_info"=>
{"name"=>"Joe McÙisnean",
"last_name"=>"McÙisnean",
"first_name"=>"Joe",
"email"=>"someemail#gmail.com"},
"uid"=>
"https://www.google.com/accounts/o8/id?id=AItOawnQmfdfsdfsdfdsfsdhGWmuLTiX2Id40k",
"provider"=>"google_apps"}
In my user model
def apply_omniauth(omniauth)
#add some info about the user
self.email = omniauth['user_info']['email'] if email.blank?
self.name = omniauth['user_info']['name'] if name.blank?
self.name = omniauth['user_info'][:name] if name.blank?
self.nickname = omniauth['user_info']['nickname'] if nickname.blank?
self.nickname = name.gsub(' ','').downcase if nickname.blank?
unless omniauth['credentials'].blank?
user_tokens.build(:provider => omniauth['provider'],
:uid => omniauth['uid'],
:token => omniauth['credentials']['token'],
:secret => omniauth['credentials']['secret'])
else
user_tokens.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
end
end
I'm not hugely knowledgeable about UTF encoding, so I'm not sure where I should be specifying the encoding? But I'm guessing it's here before it get's put into the user model and created, I'm unsure what to do about it?
UPDATE:
Rails 3.0.10
Omniauth 0.2.6
Ruby 1.9.2
PG 0.11.0
Default encoding is UTF-8
That didn't seem to be it, so I dug further and found this in the view:
Showing /Users/holden/Code/someapp/app/views/users/registrations/_signup.html.erb where line #5 raised:
incompatible character encodings: ASCII-8BIT and UTF-8
Extracted source (around line #5):
2: <%= f.error_messages %>
3:
4: <%= f.input :name, :hint => 'your real name' %>
5: <%= f.input :nickname, :hint => 'Username of your choosing' %>
6:
7: <% unless #user.errors[:email].present? or #user.email %>
8: <%= f.input :email, :as => :hidden %>
UPDATE UPDATE:
It seems to be the omniauth gem which is returns the ASCII-8BIT chars, so my next question is how can I parse the hash and convert it back into UTF8 so my app doesn't explode?
session[:omniauth] = omniauth.to_utf8
Another part to this crazy ride is when I type this into the console
d={"user_info"=>{"email"=>"someemail#gmail.com", "first_name"=>"Joe", "last_name"=>"Mc\xC3\x99isnean", "name"=>"Joe Mc\xC3\x99isnean"}}
It automatically converts it to UTF-8, but it explodes when shoved into a session
=> {"user_info"=>{"email"=>"someemail#gmail.com", "first_name"=>"Joe", "last_name"=>"McÙisnean", "name"=>"Joe McÙisnean"}}
This is a painful nightmare if there ever was one.
Omniauth proved to be the problem producing the ASCII-8BIT
I ended up forcing the Omniauth hash into submission using:
omniauth_controller.rb
session[:omniauth] = omniauth.to_utf8
added recursive method to force convert the rogue ASCII-8BIT to UTF8
some_initializer.rb
class Hash
def to_utf8
Hash[
self.collect do |k, v|
if (v.respond_to?(:to_utf8))
[ k, v.to_utf8 ]
elsif (v.respond_to?(:encoding))
[ k, v.dup.force_encode('UTF-8') ]
else
[ k, v ]
end
end
]
end
end
Special thanks to tadman
recursively convert hash containing non-UTF chars to UTF