I've been trying to follow the official rails guide and have run into an issue. There seems to be a syntax error message on code that was scaffolded in a new project using:
rails generate scaffold Post name:string title:string content:text.
Simple enough, right? I'm just going through the first page of the official rubyonrails.org "Make a blog" tutorial. But when I go to http://localhost:3000/posts I get this issue.
The SyntaxError Message:
/projects/blog/app/controllers/posts_controller.rb:9: syntax error, unexpected ':', expecting '}'
format.json { render json: #posts }
^
On step 6.3 I'm supposed to be seeing a simple "Listing posts" page, with presumably no blog entries.
My posts_controller.rb file:
class PostsController < ApplicationController
# GET /posts
# GET /posts.json
def index
#posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #posts }
end
end
...(continued. This was all generated via a scaffold.)
My setup:
The guide says:
This Guide is based on Rails 3.2. Some of the code shown here will not work in earlier versions of Rails.
My rails -v says:
Rails 3.2.8
rubyonrails.org says:
We recommend Ruby 1.9.3 for use with Rails. Rails 3.2 is the last one that supports Ruby 1.8. Ruby 1.8.6 and earlier are not supported, neither is version 1.9.1.
My ruby -v says:
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin12.2.0]
I always get that SyntaxError message, even after trying different versions of Ruby (using rbenv). Here are the versions I've tried:
1.9.2-p290
1.9.2-p320
1.9.3-p194 (latest)
Workaround:
Changing format.json { render json: #post } to format.json { render :json => #post } fixes the issue.
My Question:
Why, with the latest version of Ruby and the latest version of Rails, would I get a SyntaxError when the versions are supposed to be compatible? At this point in the first page of the official tutorial, I haven't even written any ruby. These lines in my posts_controller.rb file were generated by the latest stable version of Rails and the syntax is outdated? Rails hasn't been updated to use Ruby syntax going back to version 1.9.2?
In your Gemfile specify the ruby version like so ruby "1.9.3" right under the sources. See if that fixes it.
Edit I was having that problem deploying to heroku and adding that to my Gemfile fixed the error. Apparently it was trying to use 1.8.7 instead of 1.9.3 even though the ruby version specified by heroku was 1.9.3
mind putting this "puts RUBY_VERSION" at the top of your posts_controller? I'm curious if the right version is getting picked up. When you start the Rails server it should output the version number to the console.
Rails is definitely 1.9.3 friendly. 1.9.3 doesn't break anything that I'm aware of and since Rails is 1.8 compatible it should work moving forward just fine.
Related
I am getting syntax error, unexpected tLABEL in below Ruby code. The error description is pointing to ':' after 'timeout'.
def self.run(*args, timeout: nil, environment: {})
# ...
end
I have no knowledge of Ruby. I have tried few things like replacing ':' with '=' or putting nil in {} but nothing seems to work.
My ruby version is 2.1.5.
IUQ-mini:~ IUQ$ rbenv versions
system
* 2.1.5 (set by /Users/IUQ/.ruby-version)
2.1.7
2.2.3
The particular code can be found here at line #38.
Few questions over SO points that this could happen due to misplaced braces but I did not see error - again my lack of Ruby knowledge!
Please help me to understand cause of this error and How can I resolve this?
Thanks
That won't work in ruby 1.9 (if in fact JRuby is limiting you to 1.9) as-is since the splat is expected to have a hash immediately following it if it's the first argument.
You can do something like this:
def self.run (environment = {}, timeout = nil, *args)
end
The only rub is you'll have to explicitly pass something (even nil) for timeout if you want to pass stuff in to be args[].
Calabash iOS and Android require ruby >= 2.0.
The latest released version of ruby is recommended.
JRuby of any version is not supported at this time.
Travis build
If you look at the info for that build, you'll see it failed because it was running on ruby 1.9.3.
I believe that you have ruby 2.0 installed. I don't think you are using it.
$ rbenv versions
system
1.8.7-p375
1.9.3-p484
2.0.0-p481
2.1.5
2.2.2
2.2.3
* 2.3.0 (set by /Users/moody/.rbenv/version) <== Active ruby in this dir
jruby-1.7.18
$ rbenv version # Active ruby in this directory
2.3.0
You never mentioned what version of run_loop you are using. You should update to the most recent stable release.
https://github.com/calabash/calabash-ios/wiki/Updating-your-run-loop-version
I have a legacy Rails application which I want to upgrade to recent Rails and Ruby versions.To start with I am trying to setup the application with Ruby 2.1.2
$ rails -v
Rails 2.3.18
$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [i686-linux]
When I tried to run the rake task rake db:schema:load RAILS_ENV=test I encountered following error
can not load translations from /activesupport-2.3.18/lib/active_support/locale/en.yml, the file type yml is not known
Searching through Google I found the following reference https://github.com/rails/rails/issues/10514 which mentioned that there is incompatibility between Rails 2.3 and Ruby 2+ versions.
Can anybody please help me applying a monkey-patch mentioned about in the reference link?
Thanks,
Jignesh
Finally resolved the error
can not load translations from /activesupport-2.3.18/lib/active_support/locale/en.yml, the file type yml is not known
by monkey-patching the Rails’s I18n::Backend::Base#load_file(filename) method.
The solution is as follows:
1.1 Created a file named ruby2.rb at /config/initializers
1.2 Added following contents to /config/initializers/ruby2.rb
if Rails::VERSION::MAJOR == 2 && RUBY_VERSION >= '2.0.0'
module I18n
module Backend
module Base
def load_file(filename)
type = File.extname(filename).tr('.', '').downcase
# As a fix added second argument as true to respond_to? method
raise UnknownFileType.new(type, filename) unless respond_to?(:"load_#{type}", true)
data = send(:"load_#{type}", filename) # TODO raise a meaningful exception if this does not yield a Hash
data.each { |locale, d| store_translations(locale, d) }
end
end
end
end
end
1.3 Finally ran
$ rake db:schema:load RAILS_ENV=test
and the schema was successfully loaded.
Most helpful references I could find and which helped me get to the solution:
https://github.com/rails/rails/issues/10514
https://www.lucascaton.com.br/2014/02/28/have-a-rails-2-app-you-can-run-it-on-the-newest-ruby/
Switching my Rails 3.2.12 project to Ruby 2.0.0 fails a test:
NoMethodError:
private method `initialize_dup' called for #<Receipt:0x007fe06c809428>
Looks like initialize_dup is now a private method.
What can I do to get my tests to pass when using Rails 3.2.12 and Ruby 2.0.0?
For those who are worried about upgrading to 3.2.13 because of some issues it could bring, I added this in an initializer file:
# Because of the Ruby 2.0 privatizes the method and Rails 3.2.12's use of it
# , and because Rails 3.2.13 (which has a fix) also could potentially
# introduce other bugs, we're adding this patch to fix the issue.
#
# Remove this if the project contains Rails >= 3.2.13
module ActiveModel
class Errors
public :initialize_dup
end
module Validations
public :initialize_dup
end
end
class ActiveRecord::Base
public :initialize_dup
end
It been released please use Ruby 2.0.0 with Rails 3.2.13.rc2 and also fixed the above issue initialize_dup in this released and few more fixes.
http://weblog.rubyonrails.org/2013/3/7/Rails-3-2-13-rc2-has-been-released/
It works for me.
I've just installed this gem with:
gem install 'date_validation'
And I can see it's properly installed, as it appears in the list when I use:
gem list
But when I try to validate the content of a field form as a date, I get this error:
ArgumentError in ProductesController#new
Unknown validator: 'DateValidator'
I use this code to validate:
validates :data_caducitat, :date => { :after => Proc.new { Time.now } }
Any idea of what may be the problem?
Thanks in advance.
A simple date validator for Rails 3. Compatible with Ruby 1.8.7, 1.9.2 and Rubinius 1.2.2.
That is what they write at https://github.com/codegram/date_validator
Could be that it is not compatible with 1.9.3 version
Use this one instead.
https://github.com/adzap/validates_timeliness
Works for me !
I had a exception when I switch to Ruby 1.8.7 on Snow Leopard
ArgumentError: wrong number of arguments (1 for 0)
/Library/Ruby/Gems/1.8/gems/activerecord-1.15.5/lib/active_record/connection_adapters/abstract/quoting.rb:27:in 'to_s'
/Library/Ruby/Gems/1.8/gems/activerecord-1.15.5/lib/active_record/connection_adapters/abstract/quoting.rb:27:in 'quote'
/Library/Ruby/Gems/1.8/gems/activerecord-1.15.5/lib/active_record/connection_adapters/mysql_adapter.rb:190:in 'quote'
/Library/Ruby/Gems/1.8/gems/activerecord-1.15.5/lib/active_record/base.rb:2042:in 'quote_value'
/Library/Ruby/Gems/1.8/gems/activerecord-1.15.5/lib/active_record/base.rb:2034:in 'attributes_with_quotes'
Application uses Rails 1.2.5: there is no chance to update rails in this app. Somebody have solution?
There will be a lot of compatibility issues with such an old version of rails and ruby 1.8.7. I would suggest using rvm to install multiple ruby versions / rails versions to test your app. I would also look into security patches as I am not sure if they are being backported to the 1.2.x branches.
I put this in a file inside config/initializers
class ::DateTime
alias_method :to_s, :to_formatted_s
end