How to resolve JSON syntax errors in rails? - ruby-on-rails

I started learning rails using 'Agile Web Development with Rails, 4th Edition'
with
rails 3.2.7 and ruby 1.9.3p448 (2013-06-27 revision 41675) [i686-linux].
When i tried to
edit an html form with
<%= f.text_area :description :rows=>6 %>
it returned an error
/media/ashku/New Volume/RoR/depot/app/views/products/_form.html.erb:19: syntax error, unexpected ':', expecting ')'
...= ( f.text_area :description :rows=>6 );#output_buffer.safe...
then i tried to change the :rows=>6 to rows: 6 but the results where the same
Discussion here suggests it as problem with JSON
controller code is given here
so how to resolve this problem ?

You have missed the comma(,) after tag name ie.(:description).
So replace your tag with following and try to run..
<%= f.text_area :description, :rows => 6 %>

You're missing a comma in between :description, and :rows.

You can use "Better Errors" gem for debugging in development env., this is a better tool to find errors in good way. read about them from http://railscasts.com/episodes/402-better-errors-railspanel?view=asciicast

Related

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.

Rails link_to syntax error: unexpected ',', expecting ')'

I recently attempted to add bootstrap-sass to my rails app, and it seemed to have broken all my links (the links were fully functional before installing the bootstrap-sass gem). I am using ruby 1.8.7.
A sample link I have is:
<%=link_to image_tag ("right_arrow.png", :size => '20x20'), project_step_path(#project, #step.number+1) %>
The error I am getting is:
syntax error, unexpected ',', expecting ')' ...o(image_tag ("right_arrow.png", :size => '20x20'), project_s...
Anyone know how to fix this?
There is an extra-space after image_tag.
image_tag ("right_arrow.png", :size => '20x20')
It should be
image_tag("right_arrow.png", :size => '20x20')

Rails doesn't expand the variable "count" when displays

I am developing a Rails 2.3.8 app and I stumbled upon a strange form error:
in all errors messages in any input forms, the variable count is not expanded.
As instance if I fill in the following form with a too long string (> 300 chars, I've set max=300), the form will display the message:
"is too long (maximum is {{count}} characters)" instead of:
"is too long (maximum is 300 characters)"
- form_for(#event) do |f|
= f.label :description
%br
= f.text_area :description
= f.error_message_on :description
Do you have any ideas why this happens ?
many thanks
Redmine had this issue and they fixed it in 1.0.5.
See here for the changes:
http://www.redmine.org/news/49
in the message they say something along these lines:
Among 11 bug fixes, Redmine 1.0.5
solves the incompatibility with the
i18n 0.5.0 gem. The appropriate i18n
gem version (0.4.2) is now required
so i assume you need to install the 0.4.2 i18n gem :)
I was having the same issue, and installing i18n 0.4.2 solved it for me.

call upload controller, but error will appears

I'm coding with Ruby on rails 2.3.2 under BitNami RubyStack
When i call the upload controller, i get error like this:
compile error
C:/Users/BitNami RubyStack projects/killerapp/app/views/upload/index.html.erb:6: unterminated string meets end of file
C:/Users/BitNami RubyStack projects/killerapp/app/views/upload/index.html.erb:6: syntax error, unexpected $end, expecting ')'
The code of index view:
{ :multipart => true } do |form| %>
First_Name:
Last_Name:
What's the problem? Please, help me.
When I used 1.9 The problem didn't appear, but i like to use ruby 1.8.7

Importing XML file in Rails app, UTF-16 encoding problem

I'm trying to import an XML file via a web page in a Ruby on Rails application, the code ruby view code is as follows (I've removed HTML layout tags to make reading the code easier)
<% form_for( :fmfile, :url => '/fmfiles', :html => { :method => :post, :name => 'Form_Import_DDR', :enctype => 'multipart/form-data' } ) do |f| %>
<%= f.file_field :document, :accept => 'text/xml', :name => 'fmfile_document' %>
<%= submit_tag 'Import DDR' %>
<% end %>
Results in the following HTML form
<form action="/fmfiles" enctype="multipart/form-data" method="post" name="Form_Import_DDR"><div style="margin:0;padding:0"><input name="authenticity_token" type="hidden" value="3da97372885564a4587774e7e31aaf77119aec62" />
<input accept="text/xml" id="fmfile_document" name="fmfile_document" size="30" type="file" />
<input name="commit" type="submit" value="Import DDR" />
</form>
The Form_Import_DDR method in the 'fmfiles_controller' is the code that does the hard work of reading the XML document in using REXML. The code is as follows
#fmfile = Fmfile.new
#fmfile.user_id = current_user.id
#fmfile.file_group_id = 1
#fmfile.name = params[:fmfile_document].original_filename
respond_to do |format|
if #fmfile.save
require 'rexml/document'
doc = REXML::Document.new(params[:fmfile_document].read)
doc.root.elements['File'].elements['BaseTableCatalog'].each_element('BaseTable') do |n|
#base_table = BaseTable.new
#base_table.base_table_create(#fmfile.user_id, #fmfile.id, n)
end
And it carries on reading all the different XML elements in.
I'm using Rails 2.1.0 and Mongrel 1.1.5 in Development environment on Mac OS X 10.5.4, site DB and browser on same machine.
My question is this. This whole process works fine when reading an XML document with character encoding UTF-8 but fails when the XML file is UTF-16, does anyone know why this is happening and how it can be stopped?
I have included the error output from the debugger console below, it takes about 5 minutes to get this output and the browser times out before the following output with the 'Failed to open page'
Processing FmfilesController#create (for 127.0.0.1 at 2008-09-15 16:50:56) [POST]
Session ID: BAh7CDoMdXNlcl9pZGkGOgxjc3JmX2lkIiVmM2I3YWU2YWI4ODU2NjI0NDM2
NTFmMDE1OGY1OWQxNSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxh
c2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA==--dd9f588a68ed628ab398dd1a967eedcd09e505e0
Parameters: {"commit"=>"Import DDR", "authenticity_token"=>"3da97372885564a4587774e7e31aaf77119aec62", "action"=>"create", "fmfile_document"=>#<File:/var/folders/LU/LU50A0vNHA07S4rxDAOk4E+++TI/-Tmp-/CGI.3001.1>, "controller"=>"fmfiles"}
[4;36;1mUser Load (0.000350)[0m [0;1mSELECT * FROM "users" WHERE (id = 1) LIMIT 1[0m
[4;35;1mFmfile Create (0.000483)[0m [0mINSERT INTO "fmfiles" ("name", "file_group_id", "updated_at", "report_created_at", "report_link", "report_version", "option_on_open_account_name", "user_id", "option_default_custom_menu_set", "option_on_close_script", "path", "report_type", "option_on_open_layout", "option_on_open_script", "created_at") VALUES('TheTest_fp7 2.xml', 1, '2008-09-15 15:50:56', NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL, NULL, NULL, NULL, '2008-09-15 15:50:56')[0m
REXML::ParseException (#<Iconv::InvalidCharacter: "਼䙍偒数 (followed by a few thousand similar looking chinese characters)
䙍偒数潲琾", ["\n"]>
/Library/Ruby/Site/1.8/rexml/encodings/ICONV.rb:7:in `conv'
/Library/Ruby/Site/1.8/rexml/encodings/ICONV.rb:7:in `decode'
/Library/Ruby/Site/1.8/rexml/source.rb:50:in `encoding='
/Library/Ruby/Site/1.8/rexml/parsers/baseparser.rb:210:in `pull'
/Library/Ruby/Site/1.8/rexml/parsers/treeparser.rb:21:in `parse'
/Library/Ruby/Site/1.8/rexml/document.rb:190:in `build'
/Library/Ruby/Site/1.8/rexml/document.rb:45:in `initialize'
Rather than a rails/mongrel problem, it sounds more likely that there's an issue either with your XML file or with the way REXML handles it. You can check this by writing a short script to read your XML file directly (rather than within a request) and seeing if it still fails.
Assuming it does, there are a couple of things I'd look at. First, I'd check you are running the latest version of REXML. A couple of years ago there was a bug (http://www.germane-software.com/projects/rexml/ticket/63) in its UTF-16 handling.
The second thing I'd check is if you're issue is similar to this: http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/ba7b0585c7a6330d. If so you can try the workaround in that thread.
If none of the above helps, then please reply with more information, such as the exception you are getting when you try and read the file.
Since getting this to work requires me to only change the encoding attribute of the first XML element to have the value UTF-8 instead of UTF-16, the XML file is actually UTF-8 and labelled wrongly by the application that generates it.
The XML file is a FileMaker DDR export produced by FileMaker Pro Advanced 8.5 on OS X 10.5.4
Have you tried doing this using JRuby? I've heard Unicode strings are better supported in JRuby.
One other thing you can try is to use another XML parsing library, such as libxml ou Hpricot.
REXML is one of the slowest Ruby XML libraries you can use and might not scale.
Actually, I think your problem may be related to the problem I just detailed in this post. If I were you, I'd open it up in TextPad in Binary mode and see if there are any Byte Order Marks before your XML starts.

Resources