Liquid raising SyntaxError in lax mode - ruby-on-rails

I'm trying to set up Liquid gem so that it doesn't raise exceptions on syntax errors. According to the Liquid gem Readme page, you can set different error modes and, if lax is set, it should accept "almost anything" and wouldn't raise any exception. Even if I set that mode, an exception is raised, for instance:
1.9.3-p551 :041 > Liquid::Template.error_mode = :lax
=> :lax
1.9.3-p551 :042 > Liquid::Template.error_mode
=> :lax
1.9.3-p551 :043 > Liquid::Template.parse("Thank you for your purchase at {{shop_name}")
Liquid::SyntaxError: Liquid syntax error: Variable '{{shop_name}' was not properly terminated with regexp: /\}\}/
from /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/liquid-3.0.6/lib/liquid/block.rb:104:in `create_variable'
from /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/liquid-3.0.6/lib/liquid/block.rb:44:in `parse'
from /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/liquid-3.0.6/lib/liquid/tag.rb:10:in `parse'
from /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/liquid-3.0.6/lib/liquid/document.rb:5:in `parse'
from /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/liquid-3.0.6/lib/liquid/template.rb:122:in `parse'
from /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/liquid-3.0.6/lib/liquid/template.rb:108:in `parse'
from (irb):43
from /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/railties-3.2.17/lib/rails/commands/console.rb:47:in `start'
from /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/railties-3.2.17/lib/rails/commands/console.rb:8:in `start'
from /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/railties-3.2.17/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
What am I missing?

Seems you're missing a second }.
Instead of
Liquid::Template.parse("Thank you for your purchase at {{shop_name}")
Try
Liquid::Template.parse("Thank you for your purchase at {{shop_name}}")

It's actually the expected behavior, you can't avoid Liquid raising exceptions in this kind of cases.
(Just in case somebody bumps into the same problem, I'm not closing this question)

Related

Change input in a database

I have a ruby app that runs on heroku. I am trying to alter the input of one column in my database. I have looked around the net and stack and found that others have had luck using this line of code...
user.find(8338).update_attributes(:sport=>"men's swimming")
However when I try this I get an error stating...
irb(main):005:0> User.find(8338).update_attributes(:sport=>”men’s
swimming”)
SyntaxError: (irb):5: syntax error, unexpected tIDENTIFIER, expecting
keyword_do or '{' or '('
:sport=>”men’s swimming”)
^
from /app/vendor/bundle/ruby/2.4.0/gems/railties-
5.0.7/lib/rails/commands/console.rb:65:in `start'
from /app/vendor/bundle/ruby/2.4.0/gems/railties-
5.0.7/lib/rails/commands/console_helper.rb:9:in `start'
from /app/vendor/bundle/ruby/2.4.0/gems/railties-
5.0.7/lib/rails/commands/commands_tasks.rb:78:in `console'
from /app/vendor/bundle/ruby/2.4.0/gems/railties-
5.0.7/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /app/vendor/bundle/ruby/2.4.0/gems/railties-
5.0.7/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:9:in `require'
from bin/rails:9:in `<main>'
I have tried many alterations of the above code and get the same error. Can anyone help with this? What am I doing wrong?
User.find(8338).update_attributes(:sport=>”men’s
swimming”)
Those double quotes look suspicious. You should be using the normal " and not whatever it is you typed in there.

ActionDispatch::ParamsParser replacement after upgrade to Rails 5

Migrating an app from Rails 4.2.9 to 5.2.1.
This is latest issue:
$ rails console
/Users/meltemi/rails/myapp/config/initializers/disable_xml_params.rb:3:in `<top (required)>': uninitialized constant ActionDispatch::ParamsParser (NameError)
from /Users/meltemi/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/railties-5.2.1/lib/rails/engine.rb:657:in `block in load_config_initializer'
from /Users/meltemi/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activesupport-5.2.1/lib/active_support/notifications.rb:170:in `instrument'
from /Users/meltemi/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/railties-5.2.1/lib/rails/engine.rb:656:in `load_config_initializer'
The offending line of code in an initializer:
# config/initializers/disable_xml_params.rb
ActionDispatch::ParamsParser::DEFAULT_PARSERS.delete(Mime::XML)
Rails Guides says:
ActionDispatch::ParamsParser is deprecated and was removed from the middleware stack. To configure the parameter parsers use ActionDispatch::Request.parameter_parsers=. (commit, commit)
So I've tried the following:
ActionDispatch::Request.parameter_parsers.delete(Mime::XML)
But that begets more errors:
$ rails console
/Users/meltemi/rails/myapp/config/initializers/disable_xml_params.rb:3:in `<top (required)>': uninitialized constant Mime::XML (NameError)
from /Users/meltemi/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/railties-5.2.1/lib/rails/engine.rb:657:in `block in load_config_initializer'
from /Users/meltemi/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activesupport-5.2.1/lib/active_support/notifications.rb:170:in `instrument'
from /Users/meltemi/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/railties-5.2.1/lib/rails/engine.rb:656:in `load_config_initializer'
Is there a better way to call .delete on that object?
You have to grab the existing values from parameter_parsers, modify it to suit your needs, then reset the values to your modified values. From the ActionDispatch::Http::Parameters documentation:
parameter_parsers=(parsers)
Configure the parameter parser for a given MIME type.
It accepts a hash where the key is the symbol of the MIME type and the value is a proc.
original_parsers = ActionDispatch::Request.parameter_parsers
xml_parser = -> (raw_post) { Hash.from_xml(raw_post) || {} }
new_parsers = original_parsers.merge(xml: xml_parser)
ActionDispatch::Request.parameter_parsers = new_parsers
In your specific case, you should look at the parsers in original_parsers to see if there is anything to delete. On a simple Rails 5 app that I have handy to look at, the only values I have are:
=> {
:json => #<Proc:0x00007fe818fc6fb8#/Users/foo/.rvm/gems/ruby-2.6.0-preview2/gems/actionpack-5.2.1/lib/action_dispatch/http/parameters.rb:11 (lambda)>
}
Your app's configuration is likely different, but to answer your question about how to delete a value, this simple version should work:
ActionDispatch::Request.parameter_parsers = ActionDispatch::Request.parameter_parsers.except(:json)
You may find additional useful information in this answer.

TypeError: no implicit conversion of Symbol into Integer for search with geokit-rails

I've got the geokit-rails gem installed with geocoder, and have loaded my configuration correctly with the right API keys and settings. I've properly constructed ActiveRecord objects that have encoded lat and lng columns with float values. Now I am trying to simply search for venues (Venue activerecord) that are within 100km of the given origin as so:
Venue.within(100, origin: Geokit::LatLng.new(-27.12,124.2141))
Yet I keep receiving the error:
TypeError: no implicit conversion of Symbol into Integer
from /usr/local/lib/ruby/gems/2.1.0/gems/geocoder-1.2.3/lib/geocoder/sql.rb:14:in `[]'
from /usr/local/lib/ruby/gems/2.1.0/gems/geocoder-1.2.3/lib/geocoder/sql.rb:14:in `full_distance'
from /usr/local/lib/ruby/gems/2.1.0/gems/geocoder-1.2.3/lib/geocoder/stores/active_record.rb:157:in `distance_sql'
from /usr/local/lib/ruby/gems/2.1.0/gems/geokit-rails-2.0.1/lib/geokit-rails/acts_as_mappable.rb:228:in `distance_conditions'
from /usr/local/lib/ruby/gems/2.1.0/gems/geokit-rails-2.0.1/lib/geokit-rails/acts_as_mappable.rb:108:in `within'
from (irb):46
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/commands/console.rb:90:in `start'
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/commands/console.rb:9:in `start'
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:69:in `console'
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Would someone help?
You could use Geokit within method with Lat&Long directly - from docs.
F.e:
:origin => [37.792,-122.393].
In your case Venue.within(100, origin: [-27.12,124.2141])
Why do you use Geokit::LatLng.new for this?

rails encoding issue =(

i use rails 2.3.9 with ruby 1.9.2 and when i trying to update my model with some russian letters i have error in unicorn log:
Error during failsafe response: incompatible character encodings: UTF-8 and ASCII-8BIT
Read error: #<NoMethodError: undefined method `[]' for nil:NilClass>
trace look like this:
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:521:in
process_client'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:594:in
block in worker_loop'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:592:in
each'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:592:in
worker_loop'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:482:in
block (2 levels) in
spawn_missing_workers'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:479:in
fork'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:479:in
block in spawn_missing_workers'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:475:in
each'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:475:in
spawn_missing_workers'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:489:in
maintain_worker_count'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:299:in
join'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn.rb:13:in
run'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/bin/unicorn_rails:208:in
<top (required)>'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/bin/unicorn_rails:19:in
load'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/bin/unicorn_rails:19:in
`'
so, i can't detirminate the problem, the only thing i know - what problem in russia text ( when i update model with english letters - all is ok. what i can do ? (
You should ensure that your editor saves files in UTF-8. ASCII afaik is the first part of any charset. That should be the reason you don't get any errors when you leave out the russian chars.
I answered this one here, with a script. Why are all strings ASCII-8BIT after I upgraded to Rails 3?
You need
# coding: UTF-8
at the top of your files, with ruby 1.9. If that doesn't help, it might be your external dependency, such as DB.
you can use gem "russian" (gem is based on l18n)
https://github.com/yaroslav/russian
in model (instead of russian letters):
Russian::translate(:some_word)
in config file (must be encoded in UTF-8):
ru:
some_word: 'это строка с русскими буквами (this is string with russian letters)'

Ruby on Rails: odd stylesheet issue

When I open up a page that's using my CSS it will work once, but won't work again until you open up the CSS and re-save it. Every other time I try to directly access my CSS, it works fine. But the other times, it doesn't work and I receive this server output:
[2010-08-01 12:49:37] ERROR NoMethodError: private method `gsub!' called for #<Class:0x7f6d0639ad80>
/usr/lib/ruby/1.8/webrick/htmlutils.rb:16:in `escape'
/usr/lib/ruby/1.8/webrick/httpresponse.rb:232:in `set_error'
/var/www/rails-blog/vendor/rails/railties/lib/webrick_server.rb:94:in `handle_file'
/var/www/rails-blog/vendor/rails/railties/lib/webrick_server.rb:73:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
/var/www/rails-blog/vendor/rails/railties/lib/webrick_server.rb:60:in `dispatch'
/var/www/rails-blog/vendor/rails/railties/lib/commands/servers/webrick.rb:66
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
/var/www/rails-blog/vendor/rails/activesupport/lib/active_support/dependencies.rb:153:in `require'
/var/www/rails-blog/vendor/rails/activesupport/lib/active_support/dependencies.rb:521:in `new_constants_in'
/var/www/rails-blog/vendor/rails/activesupport/lib/active_support/dependencies.rb:153:in `require'
/var/www/rails-blog/vendor/rails/railties/lib/commands/server.rb:49
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
script/server:3
127.0.0.1 - - [01/Aug/2010:12:49:37 BST] "GET /stylesheets/scaffold.css HTTP/1.1" 500 343
- -> /stylesheets/scaffold.css?1280662371
The code I'm using to include the stylesheet is <%= stylesheet_link_tag 'scaffold' %>. I've restarted the Ruby server but with no effect. What could be causing this problem?
This is from reading one of the official guides.
This does not happen when running Mongrel.
This is a bug in WEBrick, see http://www.ruby-forum.com/topic/206225.
On 10.06.2010 03:19, Michael Pitman (mcp) posted:
To continue using Webrick, instead of
switching to Mongrel, you can also
just edit line 15 of
lib/ruby/1.8/webrick/htmlutils.rb to
read
str = string ? string.to_s.dup : ""
The problem is that NotModified
exception is getting passed to
HTMLUtils::escape as a class, rather
than a string (originally raised in
HTTPServlet::DefaultFileHandler).
Since the only things that can really
be HTML escaped are strings, it should
be safe to always convert the input
to string.
I suspect that the potential
performance penalty may prevent a
solution like that from making it to
the standard ruby library, but it's a
simple workaround.
Alternatively, in the handle_file
method of webrick's DispatchServlet
(lib/webrick_server.rb in the rails
gem), before it calls res.set_error
with the exception, you could force
the err.message to be a string, or
even clear the message, since I think
it's discarded anyway for
NotModified.
Michael

Resources