Error RequestURITooLarge searching ruby - ruby-on-rails

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

Related

Ruby ERB render and def function

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
%>

Unable to launch rails in production environment locally - Rails 5

This is my first time launching a rails app in a production environment. I first ran rails server -e production then had to get the secret key. Afterwards I ran this line bundle exec rake assets:precompile db:migrate RAILS_ENV=production. Once I ran that line I ran rails server -e production one more time I got the following error in my terminal (See last 4 lines) along with the 404 error page in my browser:
krav#krav-Q534UXK:~/Desktop/cnd$ rails server -e production
=> Booting Puma
=> Rails 5.1.3 application starting in production on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.10.0 (ruby 2.3.3-p222), codename: Russell's Teapot
* Min threads: 5, max threads: 5
* Environment: production
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
/home/krav/Desktop/cnd/app/views/creatives/index.html.erb:20: warning: key :data is duplicated and overwritten on line 20
/home/krav/Desktop/cnd/app/views/creatives/index.html.erb:20: warning: key :data is duplicated and overwritten on line 20
/home/krav/Desktop/cnd/app/views/creatives/index.html.erb:107: warning: key :data is duplicated and overwritten on line 107
/home/krav/Desktop/cnd/app/views/creatives/index.html.erb:107: warning: key :data is duplicated and overwritten on line 107
When I go to the lines that are giving an error here they are respectively:
Line 20
<%= image_tag "AdobeStock_95578405.jpeg" ,alt: "slidebg1", :data => {bgposition: "center bottom"} , :data => {bgrepeat: "no-repeat"} ,:data => {bgfit: "cover"} , :class => "rev-slidebg" %>
Line 107
<%= image_tag "AdobeStock_108067927.jpeg" ,alt: "slidebg1", :data => {bgposition: "center bottom"} , :data => {bgrepeat: "no-repeat"} ,:data => {bgfit: "cover"} , :class => "rev-slidebg" %>
In development this works fine. I cannot figure out why these lines are giving me an error and not allowing the web app to launch they look right and got me the look I needed when I was in development mode.
The lines have duplicate key of data in them and should only have one:
i.e. this:
<%= image_tag "AdobeStock_95578405.jpeg" ,alt: "slidebg1", :data => {bgposition: "center bottom"} , :data => {bgrepeat: "no-repeat"} ,:data => {bgfit: "cover"} , :class => "rev-slidebg" %>
should be this: <%= image_tag "AdobeStock_95578405.jpeg" ,alt: "slidebg1", :data => {bgposition: "center bottom",bgrepeat: "no-repeat", bgfit: "cover"} , :class => "rev-slidebg" %>

Output integer as phone number format in view

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"

Spree deface override tutorial

I am following the Spree deface overrides developer guide: http://guides.spreecommerce.com/developer/deface_overrides_tutorial.html
My code matches their's exactly, but I keep getting this error. I looked all around but I didn't see anyone else having this problem or anything similar at all:
undefined method `content_tag' for Spree:Module
I am running Rails 4.0.2 and ruby 1.9.3 (it's possible that the tutorial wasn't updated for rails 4?)
here's my code:
app/overrides/add_sale_price_to_product_edit.rb
Deface::Override.new(:virtual_path => 'spree/admin/products/_form',
:name => 'add_sale_price_to_product_edit',
:insert_after => "erb[loud]:contains('text_field :price')",
:text => "
<%= f.field_container :sale_price do %>
<%= f.label :sale_price, raw(Spree.t(:sale_price)) %><span>*</span>
<%= f.text_field :sale_price, :value =>
number_to_currency(#product.sale_price, :unit => '') %>
<%= f.error_message_on :sale_price %>
<% end %>
")
app/models/spree/product_decorator.rb
module Spree
Product.class_eval do
delegate_belongs_to :master, :sale_price
end
end
You're getting the error because the translation for Spree.t(:sale_price) is not specified. This is failing because Rails 4.0.2 made some changes to the I18n API . You have a few choices.
Add a translation for the missing tag and remember that this content_tag issue is caused by this crazy bug.
Downgrade to Rails 4.0.0 (not recommended)
Upgrade spree to the 2-1-stable branch (or wait until 2.1.4 is released)
Apply this change to your local Spree installation. It should correct this issue.
Any of those should get you working again.

PG syntax error

I've been trying to convert the fedena project built on ruby on rails for my school from mySQL to postgreSQL for heroku deployment and ran into the following error:
Showing app/views/class_timings/index.html.erb where line #40 raised:
PG::Error: ERROR: syntax error at or near "."
LINE 1: SELECT `batches`.*,CONCAT(courses.code,'-',batches.name) as ...
^
: SELECT `batches`.*,CONCAT(courses.code,'-',batches.name) as course_full_name FROM "batches" INNER JOIN "courses" ON "courses".id = "batches".course_id WHERE ("batches"."is_deleted" = 'f' AND "batches"."is_active" = 't') ORDER BY course_full_name
Extracted source (around line #40):
37: <label ><%= t('select_a_batch') %>:</label>
38: <div class="text-input-bg">
39: <%= select :batch, :id,
40: #batches.map {|b| [b.full_name, b.id] },
41: {:prompt => "#{t('common')}"},
42: {:onchange => "#{remote_function(
43: :url => { :action => 'show' },
44: :with => "'batch_id='+value",
45: :before => "Element.show('loader')",
46: :success => "Element.hide('loader')"
47: )}"} %>
Obviously, I'm new to programming! Please help.
The controller:
class_timings_controller.rb
def index
#batches = Batch.active
#class_timings = ClassTiming.find(:all,:conditions => { :batch_id => nil,:is_deleted=>false}, :order =>'start_time ASC')
end
Both the backtick quotes ` and the CONCAT function are non-standard SQL.
If you really want to quote table-names you'll need to use double-quotes. There is a text-concatenation operator you'll want in Postgresql called ||.
So you'll have something like:
SELECT "batches".*, (courses.code || '-' || "batches"."name") as course_full_name ...
Be consistent - if you are quoting tables names in some places, quote them everywhere.
This is going to be slow going until you understand the syntax of both databases at at least a basic level. Presumably there's not too much raw SQL since you're using rails though.
Tip: set aside a couple of hours and skim through the manuals of both systems. At least you'll know where to look to check details then.

Resources