I'm using Princely and Rails 3.
When I try to render a pdf like this..
respond_to do |format|
format.pdf do
render :pdf => "filename", :template => "path/template.pdf"
end
end
The downloaded file is a text/html format.
How can I fix this and make it work. I have already added the line bellow to config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf
I had the same problem yesterday and came across this question in my search for a solution.
In my case, I had the Princely gem installed globally (well, in my global RVM repo) rather than anywhere in my project's directory structure. So none of the Princely code was ever being loaded. I ended up making a new "prince.rb" file in my project's conf/initializers with the following:
require 'princely'
require 'princely/rails'
At that point I still got rails errors because Princely 1.2.5/lib/princely/pdf_helper#stylesheet_file_path refers to ActionView::Helpers::AssetTageHelper, which is apparently a Rails 2ism. I just changed that method's body to "return stylesheet" and then everything worked.
I'm still fairly new to Rails; maybe someone that knows more can chime in with a better way.
Related
I'm doing some research and I've found that you can add a ckeditor in the rails admin, but my problem is that it doesn't seem to show it in the rails_admin. My current version is 4.1 and the other versions or the other solutions were way back in 2010, so the code doesn't seem to work with mine.
Here is the picture:
I have also done the following:
gem 'paperclip'
rails generate ckeditor:install --orm=active_record --backend=paperclip
config.autoload_paths += %W(#{config.root}/app/models/ckeditor)
mount Ckeditor::Engine => '/ckeditor'
//= require ckeditor/init
RailsAdmin.config do |config|
config.model user do
edit do
field :description, :ck_editor
ckeditor true
end
end
end
Also please note that I have also used devise and the default user, any help will do and thanks really hard to make this work properly
everything worked after finding out that i didnt need the
ckeditor true
also after restarting the server
I know that there have been other questions about this, but I've read through them all and haven't figured this out yet. I'm using the PDFKit gem to allow users to generate .pdfs of content on my site. I can render a pdf by adding .pdf to the url, but CSS doesn't transfer along with it. I understand from Googling that it's because of the asset pipeline, but I haven't figured out how to make it work.
I've installed both of teh necessary gems:
gem 'pdfkit', '~> 0.6.2'
gem 'wkhtmltopdf-binary'
In my application.rb file I added 'require pdfkit' at the top and the following to the application class:
config.middleware.use PDFKit::Middleware
config.cache_classes = true
config.eager_load = true
I then ran rake middleware
I also added an initializer called pdfkit.rb with teh following code that I found, in order to fix the .pdf from endlessly loading:
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
}
At this point adding .pdf to a url creates the pdf, but doesn't render the CSS at all. The README on pdfkit's github says:
"Resources aren't included in the PDF: Images, CSS, or JavaScript does not seem to be downloading correctly in the PDF. This is due to the fact that wkhtmltopdf does not know where to find those files. Make sure you are using absolute paths (start with forward slash) to your resources."
My CSS is called in the head as usual:
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
How do I make an absolute path? I don't think I just replace the stylesheet_tag with an ordinary
<link rel='stylesheet' href='bootstrap_and_customization.css.scss' type="text/css">
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.
Right now I am working on rails 3.0.0. If I run my project in terminal, I get this warning. Please help me.
/usr/share/ruby-rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.0/lib/action_dispatch/http/mime_type.rb:98: warning: already initialized constant PDF
You might have this in your config/initializers/mime_types.rb file.
Mime::Type.register 'application/pdf', :pdf
It looks like newer versions of rails already registers it.
Try using lookup_by_extenstion before defining it.
I have this on my config/initializers/mime_types.rb file.
Mime::Type.register "application/pdf", :pdf unless Mime::Type.lookup_by_extension(:pdf)
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"