How can I display better HAML errors in Ruby on Rails? - ruby-on-rails

for a long time now HAML syntax errors just spits out a generic error message, like this:
Encountered a syntax error while rendering template: check .component.container-xl.my-3.no-p
.component-header
%div.float-end
I'm using the better_errors gem and if I scroll down to #cause, I can see the actual error here:
#<SyntaxError: /USER PATH STUFF ETC/index.html.haml:47: syntax error, unexpected '=' ...:Haml::Util.escape_html_safe((=number_to_human(investment.va... ... ^ >
Is there a way to display the #cause error on top? Maybe I'll fork the gem and customize the UI to accomplish this...

I ended up forking and hacking better_errors to display the error I wanted here:
https://github.com/Swolie/drgn_better_errors

Related

Prevent Maruku from posting so many log warnings

I use Maruku in Rails simply to convert a Markdown file to HTML so I can use it with Nokogiri. (Maybe there's a better solution for that?) That works fine, but I get lots and lots of "Maruku tells you" messages in the log:
___________________________________________________________________________
| Maruku tells you:
+---------------------------------------------------------------------------
| Could not find ref_id = "FIX" for md_link("FIX", nil)
| Available refs are []
+---------------------------------------------------------------------------
That's really confusing and not needed here. Is there a way to silence Maruku so it only warns in the log if there's a real error?
Looking at the source code and documentation, it looks like you can set :on_error to :ignore :
Maruku.new(string, :on_error => :ignore)
It might also silence "real errors", though.
Maybe try rdiscount gem?
I find Maruku too verbose with errors and do not want to have to ignore all error.

Rails Rspec warning: "This dynamic method is deprecated."

My Rails 4/Ruby 2 app is throwing the following warning every time my RSpec tests create a FactoryGirl object: "DEPRECATION WARNING: This dynamic method is deprecated. Please use e.g. Post.find_or_create_by(name: 'foo') instead."
This warning is not thrown when I run my app in development. Is FactoryGirl's code throwing this? I tried to find some information but it doesn't look like other people are getting this.
If you tell Rails to give you a full stack trace for the deprecation warning, you should be able to diagnose it pretty easily. The warnings are coming from a library called ActiveSupport::Deprecation - tell it to run in debug mode.
# config/environments/test.rb
ActiveSupport::Deprecation.debug = true
For me, the warnings were caused by an old version of the Stringex library.
FactoryGirl would make a new model, which would trigger a call to one of the Stringex methods, which would raise the warning, although there was no way to see that until I turned on full stack traces. bundle update stringex solved the issue with no problem.
It looks like it's coming from ActiveRecord.
module DeprecationWarning
def body
"#{deprecation_warning}\n#{super}"
end
def deprecation_warning
%{ActiveSupport::Deprecation.warn("This dynamic method is deprecated. Please use e.g. #{deprecation_alternative} instead.")}
end
end
I'm not sure why you're not getting the warnings in development. Is your environment suppressing the warnings?

find_by_name Ancestry Gem

I have been working on implementing the Ancestry gem in my rails app, I have almost got it working the way I want it to.
I am having problems with the following line of code from the Ancestry README docs.
TreeNode.find_by_name('Crunchy').subtree.arrange
The code that I am using in my app is as follows:
Message.find_by_name('FedEx').#messages.arrange
With this, I am getting an error message:
syntax error, unexpected tIVAR
...find_by_name('FedEx').#messages.arrange );#output_buffer.saf...
...
Can anyone tell me what I am doing wrong, or explain to me what TreeNode and subtree should be replaced with?
I belive what you want is: Message.find_by_name('FedEx').subtree.arrange.
Message class should implement ancestry via has_ancestry according to the README.

Missing helper file error after Rails3 conversion

I'm in the midst of converting an old Rails2.3 project to 3, and I'm running into this runtime error when I load the first page:
Missing helper file helpers/activesupport.rb
Full stacktrace here
Has anyone else run into this? Looks like something changed in how helpers are loaded, but I don't see any obvious solutions.
I was able to work around the problem by created an empty file at app/helpers/activesupport.rb but I would like to know why this is happening in the first place.
Could it be a clash with ActiveSupport?
I am not sure why its even looking for such a helper - do you have a model or controller called activesupport?
I was having a similar issue with Hpricot. I had a require 'hpricot' statement in a helper, but I didn't have Hpricot in my Gemfile.
In your case, if you were explicitly requiring ActiveSupport somewhere, you would have to add it to your Gemfile (I just tried it and despite having Rails in my Gemfile, I still got the same error you were getting).

How to validate Markdown?

It's possible to write Markdown content with invalid syntax. Invalid means that the BlueCloth library fails to parse the content and throws an exception. The markdown helper in Rails doesn't catch any BlueCloth exceptions and because of that the complete page fails to render (500 Server Error page is rendered instead).
In my case, users are allowed to write Markdown content and save it to the database. If someone used invalid syntax, all successive rendering attempts of that content fail (Status Code 500 - Internal Server Error).
How do you get around this issue? Is it possible to validate the Markdown syntax at the Model-level before saving to the database?
You should write your own validation method in which you would initialize BlueCloth object, and try to call to_html method catching any exception. If you catch an exception, validation fails, otherwise it should be ok.
In your model:
protected:
def validate
bc = BlueCloth.new(your_markdown_string_attribute)
begin
bc.to_html
rescue
errors.add(:your_markdown_string_attribute, 'has invalid markdown syntax')
end
end
I've done a bit of research and decided to use RDiscount instead of BlueCloth. RDiscount seems to be much faster and more reliable than BlueCloth.
It's easy to integrate RDiscount in your Rails environment. Include the following snipped in your environment.rb and you are ready to go:
begin
require "rdiscount"
BlueCloth = RDiscount
rescue LoadError
# BlueCloth is still the our fallback,
# if RDiscount is not available
require 'bluecloth'
end
(tested with Rails 2.2.0)

Resources