Rails with Jader gets 'failed to require "fs"' error - ruby-on-rails-3

I am trying to use client side Jade templates that are precompiled by Ruby on Rails and available through JST.
I added the jader gem
gem "jader", "~> 0.0.8"
In configuration/initializers I created an initializer called jader.rb that contains:
Jader.configure do |config|
config.views_path = Rails.root.join('app','assets','javascripts','views')
config.mixins_path = Rails.root.join('app','assets','javascripts','mixins')
end
In app/assets/javascripts/views I added an index.js.jst.jade file
Hello World!
Lastly in my javascript file I have:
$('#app-content').append(JST["views/index"]());
When I run Rails and browse to the page triggering the code I am getting the following error:
failed to require "fs"
(in /my-project/app/assets/javascripts/views/index.js.jst.jade)
I understand that the problem is that jade is a node.js project and the require function is having a problem. How do I fix the require error?

If you are looking at using Jade templates only on the client side (with Rails asset pipeline), I recommend https://github.com/inossidabile/jade

It's likely there is an error in your index.jade template.
From the jade rails-fix pull request here, ExecJS used by rails does not support require("fs") and will throw 'failed to require "fs" even if it's a template error. Hopefully this should point you in the right direction.
To debug, you can replace the content of index.jade template with with a 1-liner jade placeholder to see if it renders well. Alternatively, try it out rendering the index.jade template with a nodejs server if possible to be sure the template renders without any whinning.

Related

How to set sinatra-authentication to use erb instead of haml?

I'm trying to set up the sinatra-authentication gem in a simple sinatra app, and running into an issue where sinatra can't find the correct views. I understand that sinatra-authentication uses haml by default, but I'm using erb in this app.
This in mind, I found in the sinatra-authenticaiton docs that there is a setting which allows you to change the template engine, by adding the following to your app file:
configure do
set :template_engine, :erb # for example
end
I've added this to my app.rb file, and sinatra is still looking for the signup.haml when I try to hit the /signup route in my app.
A couple of notes:
I've included the gem in my Gemfile, and successfuly run a bundle install on my app.
source 'https://rubygems.org'
gem 'sinatra'
gem 'data_mapper'
gem 'pg'
gem 'dm-postgres-adapter'
gem 'sinatra-authentication'
I saw something in the documentation that suggested that I may need to specify the location of my view files, so I added the following to my configuration block.
set :sinatra_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "views/"
**I think I've required the gem accurately in my app file by adding
require "sinatra-authentication"
use Rack::Session::Cookie, :secret => 'mys3cr3tk3y'
This gist is a current representation of my app.rb file in the root of my sinatra app. https://gist.github.com/rriggin/5378641#file-gistfile1-txt
Here is a screenshot of the error sinatra throws: http://cl.ly/image/0y041t0K3u3O
When I run the app locally, a 'dm-users' table is created in my local db as expected.
Is there another configuration setting that I'm missing in order to get sinatra-authentication to properly look for the erb templates rather than haml files. Any help would be greatly appreciated.
Thanks
The specs don't test that the template_engine setting works, and looking at the way the setting is called, I believe it's not correct, i.e.
send settings.template_engine, get_view_as_string("index.#{settings.template_engine}"), :layout => use_layout?
might better work as:
send app.settings.template_engine, get_view_as_string("index.#{app.settings.template_engine}"), :layout => use_layout?
that's what I reckon. If you fork the project, change the line and add it to your Gemfile and it works then consider writing a quick spec for it and you'll have improved the mainline of that project as well as fixed your problem.

Gem with models

I created a Gem with models (actually, extracted it from the main project) to share amongst the projects we have in our platform.
We have dozens of models, so instead of requiring them one by one, I wrote the following code:
Gem.find_files("my_gem/models/*.rb").each { |path| require path }
I access one of the projects that has my_gem in the gem file and running rails c I get the following output:
/Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/dynamic_matchers.rb:50:in `method_missing': undefined method `has_attached_file' for #<Class:0x007fad4b93ccb8> (NoMethodError)
One of my models is using the gem paperclip, what is weird is:
If I remove the line declared above to load all the models
automatically, rails c runs fine
If I try to include onlye the model that uses paperclip require
"my_gem/models/paperclip_model" I receive the same error
So then I change my gem to not load any model, and when I try to reference any model from rails console, it says the class is not loaded, but then I run Gem.find_files("my_gem/models/*.rb").each { |path| require path } or require "my_gem/models/paperclip_model" 'they work perfectly and I am able to work with the model.
Has any of you seen the same issue?
Seems that changing require for autoload solved the problem
I changed Gem.find_files("my_gem/models/*.rb").each { |path| require path }
for
Gem.find_files("my_gem/models/*.rb").each do |f|
filename = File.basename(f, '.*')
class_name_symbol = filename.classify.to_sym
autoload class_name_symbol, "my_gem/models/#{filename}"
end
and now it is working.
It sounds like one of the models in your gem depends on Paperclip, but you don't explicitly set it as a dependency. So what's happening is that if your models get loaded before paperclip gets loaded, you'll see the UndefinedMethod error for has_attached_file.
If you use your models in a Rails application which has paperclip as a dependency, and you load those models after the console (or server) has spun up, Paperclip will be present, so you won't see this error.
The solution is to explicitly add paperclip as a dependency in your gemspec, something like:
s.add_dependency('paperclip')
Assuming that this gem will always be used in the context of a Rails application, this should work. If not, you might also need to add the following line to the top of your models that use paperclip:
require "paperclip"

CKeditor in Ruby on Rails

I installed Ckeditor following the instruction in this repository
I created a simple rails application adding
gem 'ckeditor'
to the Gemfile and then running bundle install from command line.
I created a scaffold with only one text field. I wanted to have a cktext_area rather than a normal text_area in the create/update action.
However, I get this error
undefined method 'cktext_area' for #<ActionView::Helpers::FormBuilder:0xb66fe5a8>
if I substitute the form text area with this
<%= javascript_include_tag :ckeditor %>
<%= f.cktext_area :name%>
I ran the installation as written in github, it seems it isn't able to find the appropriate helper. Even if I try to insert cktext_area_tag("test_area", "Ckeditor") it isn't able to find the correct helper.
I'm using Rails 3.1.3
Hey first I also got this error and then when i restarted the server everything worked fine.
Try it.Hope it gets working.

pdfkit not rendering correctly in rails 3.1

I have followed the following railscast about adding pdfkit to an application, and I am having some issues with the generation of pdfs. Here are the following things that I have done:
I downloaded wkhtmltopdf via the homebrew package manager
brew install wkhtmltopdf
Then I added the pdfkit gem to my gemfile and ran the bundle install command. I added the following to my config/application.rb file
require 'pdfkit'
...
config.middleware.use PDFKit::Middleware, :print_media_type => true
I then changed my application layout file to include all stylesheet types.
If I run rake middleware, the command works and I can see the pdfkit middleware
When I try to append pdf to the end of my routes the application just hangs and I have to exit via the command line. If I create a link to the page I want to make into a pdf, it changes all of the markup so it looks like a corrupted file. (it looks like you opened a text file into a word processor or vice versa I can provide images if that helps) If I try to make css changes in my stylesheet they do not go into effect when I view them with the link to pdf. I am assuming that this has something to do with the new asset pipeline in rails has anyone else experienced this issue?
So I was right in assuming that my error had something to do with the asset pipeline, after doing some research it looks like you need to create a new initializer and add the following code:
ActionController::Base.asset_host = Proc.new { |source, request|
if request.env["REQUEST_PATH"].include? ".pdf"
"file://#{Rails.root.join('public')}"
else
"#{request.protocol}#{request.host_with_port}"
end
}

Multiple public folders, single rails installation

I have a rails application I would like to use for multiple sites, each with different designs.
I would like to change the rails installation /public directory to something else (dynamically eventually). However, I have run into a problem (bug?) changing directories...
In my application.rb file I change the paths.public path to something other than "public" (let's say "site_one"). Here is the code:
puts paths.public.paths
paths.public = "site_one"
puts paths.public.paths
The two "puts" commands are for debugging. Now run "rails s" and you will see:
/home/macklin/app/public
/home/macklin/app/site_one
This verifies the path is changed correctly. However, shortly afterward, rails throws the following error (let me know if you need the full trace):
Exiting
/usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/paths.rb:16:in `method_missing': undefined method `javascripts' for #<Rails::Paths::Path:0x7f422bd76f58> (NoMethodError) from /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_controller/railtie.rb:47
My guess is it cannot find the javascripts directory even though it is clearly sitting in the "site_one" folder.
Does anyone know why I am getting this?
I know this question is pretty old, but I think I found an answer for this in Rails 4.2.
You just simply have to put this line in your config/application.rb:
middleware.use ::ActionDispatch::Static, "#{Rails.root}/another_public_folder_name", index: 'index', headers: config.static_cache_control
This makes all files in /another_public_folder_name to be served by Rails.
This is the way Rails use to setup the standard /public folder. I found it checking the sources:
https://github.com/rails/rails/blob/52ce6ece8c8f74064bb64e0a0b1ddd83092718e1/railties/lib/rails/application/default_middleware_stack.rb#L24
Duh. Just add 2 more rules for stylesheets and javascripts (I guess they get wiped when you change the parent path)
paths.public.stylesheets = "site_one/stylesheets"
paths.public.javascripts = "site_one/javascripts"