Rails Find Syntax Error
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more ruby syntax checker online about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users ruby check syntax errors Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping ruby-lint each other. Join them; it only takes a minute: Sign up Why doesn't 'haml --check' find this glaring syntax error up vote 2 down vote favorite I was looking for rake tasks that would help me track down rubocop syntax errors, and I came across haml --check as a possible solution for double checking haml files. Unfortunately, when I passed it this broken example, haml says the syntax is OK. Have I misunderstood the purpose of haml --check or is this feature not fully implemented? I suppose I should mention I'm using Haml/Sass 3.0.25 (Classy Cassidy), rails 3.0.3, ruby 1.9.2p0, and Mac 10.6.6. $ haml --check /tmp/edit.html.haml
Syntax OK #/tmp/edit.html.haml - content_for :head do = include_javascripts :aspects #aspect_edit_pane #facebox_header %h4 = @aspect .description = t('contacts', :count =>@aspect_contacts.count)} The last character (curly brace) should trigger a syntax error, it certainly does when the template is executed as part of a request: ActionView::Template::Error (compile error /usr/local/app/diaspora/app/views/aspects/edit.html.haml:13: syntax error, unexpected '}', expecting ')' ruby-on-rails ruby haml syntax-checking share|improve this question edited Jan 12 '11 at 20:01 asked Jan 12 '11 at 19:56 Philip C 511412 add a comment| 1 Answer 1 active oldest votes up vote 3 down vote accepted Maybe because -c, --check Just check syntax, don't evaluate. Guess: it checks just the haml syntax and doesn't evaluate inline ruby. --edit This probably needs some more testing but I got it working on simple haml files :) haml --debug newsletter.html.haml 2> /dev/null | sed '$d' | ruby -c In theory: Haml prints out the precompiled Ruby source (and error messages in the end), we try to get just the ruby part and check the syntax. share|improve this answer edited Jan 12 '11 at 21:28 answered Jan 12 '11 at 20:04 Heikki 11.7k3946 I'm not sure that makes sense, because this is a syntax error. –Philip C Jan 12 '11 at 20:09 1 Haml is template markup, not ruby. Similar case would be validating html and not picking up javascript errors. –Heikki Jan 12 '11 at 20:10 2 @Philip C: It's a Ruby s
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to check syntax of ruby script that has script/runner as a shebang? up vote 18 down vote http://stackoverflow.com/questions/4673391/why-doesnt-haml-check-find-this-glaring-syntax-error favorite 2 I have problems to check syntax of ruby scripts that has rails script/runner on its shebang. Here are two example scripts and how they responses to ruby syntax checking: Script hello_world_runner.rb: #!/usr/bin/env script/runner p "Hello world!" Script hello_world.rb #!/usr/bin/env ruby p "Hello world!" Here is how I try to check the syntax. First line is a command and a second line is an output. $ ruby -c hello_world_runner.rb "Hello world!" $ http://stackoverflow.com/questions/7384199/how-to-check-syntax-of-ruby-script-that-has-script-runner-as-a-shebang ruby -c hello_world.rb SYNTAX OK ruby-on-rails ruby shebang syntax-checking share|improve this question edited Jun 10 at 17:56 kenorb 22.2k9155128 asked Sep 12 '11 at 6:46 Joni 1,93731521 add a comment| 5 Answers 5 active oldest votes up vote 12 down vote You can try this tail -n +2 hellow_world_runner.rb | ruby -c Not ideal but should work. share|improve this answer answered Nov 8 '11 at 11:20 mb14 12.2k22973 add a comment| up vote 4 down vote You can rewrite your script like this: Rails 2: #!/usr/bin/env ruby require File.expand_path(Dir.pwd + '/config/boot', __FILE__) require RAILS_ROOT + '/config/environment' p "Hello world!" Rails 3: #!/usr/bin/env ruby require File.expand_path(Dir.pwd + '/config/boot', __FILE__) require File.expand_path(Dir.pwd + '/config/application', __FILE__) Rails.application.require_environment! p "Hello world!" Of course, you need to define your own (absolute) paths or run this script from Rails root. $ ruby -c ./test.rb Syntax OK share|improve this answer answered Sep 12 '11 at 13:38 Oleksandr Skrypnyk 1,9481021 add a comment| up vote 2 down vote I would highly recommend that you factor the majority of the code in those scripts into your core domain models/lib directory/etc. This will allow you to test the script logic the same way you test the rest of your application (which will also end up checking syntax) and reduce the actual content of your executab
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more http://stackoverflow.com/questions/9696622/how-can-i-syntax-check-not-render-a-rails-3-erb-template-file about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping http://api.rubyonrails.org/classes/ActiveModel/Errors.html each other. Join them; it only takes a minute: Sign up How can I syntax check (not render) a Rails 3 ERB template file? up vote 9 down vote favorite 1 I'm trying to have a git pre-commit syntax error hook perform a syntax check on all Ruby code; there is one on GitHub at https://github.com/cypher/git-ruby-syntax-check. It attempts to check .erb files by erb -x to translate them into Ruby code and then passes the output to ruby -c for syntax checking. Unfortunately, Rails 3 introduced a custom ERB parser that is incompatible with Ruby's standard ERB, and so the pre-commit hook is finding errors where there are none. Is there some equivalent to erb -x that rails find syntax will output Ruby code from a Rails 3 ERB file? ruby-on-rails ruby-on-rails-3 erb share|improve this question edited Mar 14 '12 at 8:56 shingara 34.6k77895 asked Mar 14 '12 at 5:51 benzado 50.1k1695121 1 If you test all of your view, you don't need this check validation. –shingara Mar 14 '12 at 8:56 @shingara I disagree. This is a way to test your view, in a way other tests can't. Invalid markup may pass your functional tests because the browser "fixes" it for you, but still leave subtle bugs, or cause future problems, since no test is 100% comprehensive. –antinome Jul 30 '15 at 17:09 @shingara Or perhaps you were saying "validate after rendering, not before" rather than "don't validate at all"? In which case I see your point (and I could see an argument either way :-) –antinome Jul 30 '15 at 17:21 add a comment| 2 Answers 2 active oldest votes up vote 9 down vote accepted I have not dug much into either of these but you might try rails-erb-check (Git project) or this blog entry. I agree with shingara but the Blog Post describes a situation where this is useful and I wonder if you are in a similar position: Diaspora is pretty fluid right now. This means we are have some green tests, some missing tests, and other tests tha
helpers. A minimal implementation could be: class Person # Required dependency for ActiveModel::Errors extend ActiveModel::Naming def initialize @errors = ActiveModel::Errors.new(self) end attr_accessor :name attr_reader :errors def validate! errors.add(:name, :blank, message: "cannot be nil") if name.nil? end # The following methods are needed to be minimally implemented def read_attribute_for_validation(attr) send(attr) end def self.human_attribute_name(attr, options = {}) attr end def self.lookup_ancestors [self] end end The last three methods are required in your object for Errors to be able to generate error messages correctly and also handle multiple languages. Of course, if you extend your object with ActiveModel::Translation you will not need to implement the last two. Likewise, using ActiveModel::Validations will handle the validation related methods for you. The above allows you to do: person = Person.new person.validate! # => ["cannot be nil"] person.errors.full_messages # => ["name cannot be nil"] # etc.. Methods # [], []= A add, add_on_blank, add_on_empty, added?, as_json B blank? C clear, count D delete E each, empty? F full_message, full_messages, full_messages_for G generate_message, get H has_key? I include? K key?, keys M marshal_dump, marshal_load N new S set, size T to_a, to_hash, to_xml V values Included Modules Enumerable Constants CALLBACKS_OPTIONS = [:if, :unless, :on, :allow_nil, :allow_blank, :strict] MESSAGE_OPTIONS = [:message] Attributes [R] details [R] messages Class Public methods new(base) Link Pass in the instance of the object that is using the errors object. class Person def initialize @errors = ActiveModel::Errors.new(self) end end Source: show | on GitHub # File activemodel/lib/active_model/errors.rb, line 72 def initialize(base) @base = base @messages = apply_default_array({}) @details = apply_default_array({}) end Instance Public methods [](attribute) Link When passed a symbol or a