I am trying to show show this as a formatted phone number like: xxx-xxx-xxxx instead of just a number string like it currently is ..any help would be great!
<% if #post.phone.present? %>
<h4>Phone: <small> <%= #post.phone %><br></h4>
<% end %>
You can use number_to_phone helper, like this:
<% if #post.phone.present? %>
<h4>Phone: <small> <%= number_to_phone #post.phone %><br></h4>
<% end %>
By default, it formats the phone number as xxx-xxx-xxxx :
2.1.1 :009 > number_to_phone(1235551234)
=> "123-555-1234"
2.1.1 :010 > number_to_phone("1235551234")
=> "123-555-1234"
Use the phone gem. https://github.com/carr/phone
pn = Phoner::Phone.parse('+385915125486')
pn.to_s # => "+385915125486"
pn.format("%A/%f-%l") # => "091/512-5486"
pn.format("+ %c (%a) %n") # => "+ 385 (91) 5125486"
pn.format(:europe) # => "+385 (0) 91 512 5486"
pn.format(:us) # => "(234) 123-4567"
pn.format(:default_with_extension) # => "+3851234567x143"
Related
I got an strange issue with my experiments with ERB. Here what code do I have:
# cat ./services_lbs2.erb
<%= def renderVip(description, template)
puts "zxc dfg"
end
%>
# locally and remote
Printing: <%= renderVip('123','456') %>
And here what I am getting in the irb:
irb(main):034:0> #template=File.read('./services_lbs2.erb')
=> "<%= def renderVip(description, template)\nputs \"zxc dfg\"\nend\n%>\n# locally and remotely monitored (all externals)\nPrinting: <%= renderVip('123','456') %>\n"
irb(main):035:0> zxc = ERB.new(#template,nil, "-")
=> #<ERB:0x00000000068d4d88 #safe_level=nil, #src="#coding:US-ASCII\n_erbout = String.new; _erbout.concat(( def renderVip(description, template)\nputs \"zxc dfg\"\nend\n).to_s); _erbout.concat \"\\n# locally and remotely monitored (all externals)\\nPrinting: \"\n\n; _erbout.concat(( renderVip('123','456') ).to_s); _erbout.concat \"\\n\"\n; _erbout.force_encoding(__ENCODING__)", #encoding=#<Encoding:US-ASCII>, #frozen_string=nil, #filename=nil, #lineno=0>
irb(main):036:0> zxc.result(binding)
zxc dfg
=> "renderVip\n# locally and remotely monitored (all externals)\nPrinting: \n"
I could not get the output like:
# locally and remotely monitored (all externals)\nPrinting: zxc dfg\n
Why is it so and how it can be fixed?
Thanks!
The return value puts function is nil so in your case, the method will return nil.
So after execution nil is being appended inside the body tag. For this to work change it to
<%
def renderVip(description, template)
"zxc dfg"
end
%>
Working with and array of symbols
> I18n.available_locales
[:en, :it, :fr, :de]
> I18n.locale
:en
running an array calculation
> I18n.available_locales - I18n.locale
returns the error:
TypeError (no implicit conversion of Symbol into Array)
one cannot operate on the component to calculate with to transform it into an array
> I18n.locale.to_a
NoMethodError (undefined method `to_a' for :en:Symbol)
So what is the way to execute the calculation if the end-intent is to
<% I18n.inactive_locales.each do |locale| %>
<li><%= link_to locale.to_s, { locale: locale } %></li>
<% end %>
Make the second operand an array too, like this (for example):
I18n.available_locales - [I18n.locale]
So what is the way to execute the calculation if the end-intent is to
I'd probably do it inline (this way you can avoid patching I18n)
<% I18n.available_locales.each do |locale| %>
<% next if locale == I18n.locale %>
<li><%= link_to locale.to_s, { locale: locale } %></li>
<% end %>
It's because operator - needs an array
try
2.2.10 :003 > [:a,:b,:c] - [:a]
=> [:b, :c]
In your case would be something like
I18n.available_locales - [I18n.locale]
In such cases, you can use Array() method
Array(I18n.locale)
#=> [:en]
Array([I18n.locale])
#=> [:en]
Array(nil)
#=> []
I18n.available_locales - Array(I18n.locale)
#=> [:it, :fr, :de]
Reference: https://ruby-doc.org/core-2.6/Array.html
I want to filter the orders using stock_location field. I have the added the field to the filter section:
# app/overrides/admin.rb
Deface::Override.new(:virtual_path => "spree/admin/orders/index",
:name => "stock_locations",
:insert_top => "div.omega.four.columns",
:text => "<%= label_tag :q_line_items_variant_stock_locations_id_eq, 'Stock Locations' %><%= f.select :line_items_variant_stock_locations_id_eq, Spree::StockLocation.pluck(:id, :name).collect { |id, name| [name.strip, id]}, {:include_blank => true}, :class => 'select2 js-filterable'")
From UI, the value of the stock location is being passed, but the result is displaying all orders.
production log
==> log/thin.3001.log <==
Started GET "/admin/orders?utf8=%E2%9C%93&q%5Bcreated_at_gt%5D=&q%5Bcreated_at_lt%5D=&q%5Bstate_eq%5D=&q%5Bnumber_cont%5D=&q%5Bemail_cont%5D=&q%5Bbill_address_firstname_start%5D=&q%5Bbill_address_lastname_start%5D=&q%5Bline_items_variant_stock_locations_id_eq%5D=10&q%5Bcompleted_at_not_null%5D=0&q%5Bpromotions_id_in%5D=&button=" for 127.0.0.1 at 2015-02-12 07:57:41 +0000
Processing by Spree::Admin::OrdersController#index as HTML
Parameters: {"utf8"=>"✓", "q"=>{"created_at_gt"=>"", "created_at_lt"=>"", "state_eq"=>"", "number_cont"=>"[FILTERED]", "email_cont"=>"", "bill_address_firstname_start"=>"", "bill_address_lastname_start"=>"", "line_items_variant_stock_locations_id_eq"=>"4", "completed_at_not_null"=>"0", "promotions_id_in"=>""}, "button"=>""}
Rendered /home/deploy/.rvm/gems/ruby-2.1.2/bundler/gems/spree-080df18614ba/backend/app/views/spree/admin/orders/index.html.erb within spree/layouts/admin (212.6ms)
Rendered /home/deploy/.rvm/gems/ruby-2.1.2/bundler/gems/spree-080df18614ba/backend/app/views/spree/admin/shared/_translations.html.erb (4.7ms)
Rendered /home/deploy/.rvm/gems/ruby-2.1.2/bundler/gems/spree-080df18614ba/backend/app/views/spree/admin/shared/_head.html.erb (6.5ms)
In my console I am seeing less data than I am seeing from UI. Basically, in UI it seems filtering is not working.
2.1.2 :005 > Spree::Order.ransack(line_items_variant_stock_locations_id_eq: 4).result.to_a.count
=> 156
2.1.2 :015 > y Spree::Order.ransackable_associations
---
- user
- created_by
- approver
- bill_address
- ship_address
- state_changes
- line_items
- payments
- return_authorizations
- adjustments
- line_item_adjustments
- shipment_adjustments
- inventory_units
- products
- variants
- promotions
- shipments
=> nil
I had used wrong associations. Working code is:
Deface::Override.new(:virtual_path => "spree/admin/orders/index",
:name => "stock_locations",
:insert_top => "div.omega.four.columns",
:text => "<%= label_tag :q_shipments_stock_location_id_eq, 'Stock Locations' %><%= f.select :shipments_stock_location_id_eq, Spree::StockLocation.pluck(:id, :name).collect { |id, name| [name.strip, id]}, {:include_blank => true}, :class => 'select2 js-filterable'")
This is how I went to the final Ransack attribute naming, which does the query internally.
2.1.2 :016 > o = Spree::Order.find_by_number('R482860025')
=> #<Spree::Order id: 144, ...>
2.1.2 :020 > o.shipments.first.stock_location.name
=> "Warehouse"
2.1.2 :020 > o.shipments.first.stock_location.id
=> "4"
Here the thing: I have multiples search fields in my form. But i got the error RequestURITooLarge when I tried to do a multiples searches...I'm wondering if it is exist a method to avoid this problem. Every time you fill the text box or date field increment the url and got the error. I also tried a reset button but just clean the text fields. Here the code for you to understand the method I use and perhaps will help you in some way. Tks!!
View_item
def self.search(s_codigo, s_den_cont, s_marca)
where("codigo ilike :s_c and den_cont ilike :s_d and marca ilike :s_m", s_c: "%#{s_codigo}%", s_d: "%#{s_den_cont}%", s_m: "%#{s_marca}%")
end
#Search section #2
def self.search_fec_min(s_codigo, s_den_cont, s_marca, s_fec_min)
where("codigo ilike :s_c and den_cont ilike :s_d and marca ilike :s_m and date_expired >= :s_fmin", s_c: "%#{s_codigo}%", s_d: "%#{s_den_cont}%", s_m: "%#{s_marca}%", s_fmin: "#{s_fec_min}")
end
index
<%= text_field_tag :s_codigo, params[:s_codigo], placeholder: "Search for cod" %>
<%= text_field_tag :s_den_cont, params[:s_den_cont], placeholder: "Search for Denominación" %>
<%= text_field_tag :s_marca, params[:s_marca], placeholder: "Search brand" %>
<%= date_select :s_fec_min, params[:s_fec_min], placeholder: "Search for date", :include_blank => true %>
Controller
if self.is_date(params[:s_fec_min])
#items = View_item.all.order("date_expired ASC").page(params[:page]).per(15).search_fec_min(params[:s_codigo], params[:s_den_cont], params[:s_marca], params[:s_fec_min])
elsif (params[:s_codigo] != "" or params[:s_den_cont] != "" or params[:s_marca] != "")
#items = View_item.all.order("date_expired ASC").page(params[:page]).per(15).search(params[:s_codigo], params[:s_den_cont], params[:s_marca])
else
#items = View_item.all.order("date_expired ASC").page(params[:page]).per(15)
end
MAX_URI_LENGTH is set to only 2083 characters for webrick server. On accessing url of length more than this limit throws WEBrick::HTTPStatus::RequestURITooLarge exception.
Try to switch to different server :
#Gemfile
gem 'thin'
$bundle install
$ rails s
=> Booting Thin
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on localhost:3000, CTRL+C to stop
just wondering how I should do internationalization in rails when using a checkbox in a normal (very simple) form;
This is what I tried:
view/form file:
<%= f.check_box :do_you_agree, {}, t('submissions.yes'), t('submissions.no') %>
<%= f.check_box :do_you_agree, {}, I18n.t('submissions.yes'), I18n.t('submissions.no') %>
<%= f.check_box :do_you_agree, {}, "#{t('submissions.yes')}", "#{t('submissions.no')}" %>
...and all them return:
translation_missing in HTML.
When I use :
<%= f.check_box :do_you_agree, {}, 'NO', 'YES' %>
...everything is ok!
The YML file is ok. Many thanks.
In your config/application.rb you must have the line config.i18n.default_locale = :es (:es for spanish, as an example)
And the yml must be like this:
es:
submissions:
'yes': Si
'no': No
Edit: Make the yes/no keys explicit strings. And it will works.
Why? Because Rails does yaml conversion with psych gem. And psych return true and false with all those values (see the links).
Then, if you define a locales this way:
es:
submissions:
yes: Si
no: No
at the rails console you get:
irb> I18n.t('submissions')
=> {true=>"Si",false=>"No"}
irb> I18n.t('submissions')[true]
=> "Si"
irb> I18n.t('submissions.true')
=> "translation missing: es.submissions.true"