I am trying to use the HTML5 manifest function using a gem called Manifesto. I am stuck on the instructions for usage. I cannot figure out where those settings are supposed to go.
Any ideas? Perhaps a better gem?
https://github.com/johntopley/manifesto#readme
Thankful for all help!
You can put you settings in a file under config/initializers/. Use an informational name (like manifesto.rb). However you don't need a config with the basic usage.
In your Gemfile file, add:
gem 'manifesto'
then install via bundle:
bundle install
create the file app/controllers/manifest_controller.rb
class ManifestController < ApplicationController
def show
headers['Content-Type'] = 'text/cache-manifest'
render :text => Manifesto.cache, :layout => false
end
end
in config/routes.rb add:
match '/manifest' => 'manifest#show'
Restart your app and view the result at http://localhost:3000/manifest
You can pass the option directly to Manifesto.cache like:
# change
render :text => Manifesto.cache, :layout => false
# to
render :text => Manifesto.cache(:directory => './mobile', :compute_hash => false), :layout => false
Or use a YAML file and an initializer.
The config/manifesto.yaml file:
# directory is relative to Rails root
directory: './mobile'
compute_hash: false
The config/initializers/manifesto.rb file:
# Load the config file and convert keys from strings in symbols (the manifesto gem need symbolized options).
MANIFESTO_CONFIG = YAML.load_file(Rails.root.join('config', 'manifesto.yml').to_s).inject({}){|config,(k,v)| config[k.to_sym] = v; config}
And pass the loaded config to Manifesto.cache like:
# change
render :text => Manifesto.cache, :layout => false
# to
render :text => Manifesto.cache(MANIFESTO_CONFIG), :layout => false
Related
I have following model and controller setup on my app
attr_accessible :upload
has_attached_file :upload,
:url => "/files/docs/:basename.:extension"
:path => "/files/docs/:basename.:extension"
include Rails.application.routes.url_helpers
def to_jq_upload
{
"name" => read_attribute(:upload_file_name),
"size" => read_attribute(:upload_file_size),
"url" => upload.url(:original),
"delete_url" => upload_path(self),
"delete_type" => "DELETE"
}
end
and controller
def create
#upload = Upload.new(params[:upload])
respond_to do |format|
if #upload.save
format.html {
render :json => [#upload.to_jq_upload].to_json,
:content_type => 'text/html',
:layout => false
}
format.json { render json: {files: [#upload.to_jq_upload],param:params}, status: :created, location: #upload }
else
format.html { render action: "new" }
format.json { render json: #upload.errors, status: :unprocessable_entity }
end
end
end
now I want to upload my files to different folders like docs, images etc, so I need to make /docs dynamic in path
In file upload form I have added a hidden field with name folder and set value "docs" but when I use it in model to make path dynamic it gives me error following is the code I tried
has_attached_file :upload,
:url => "/files/#{params[:folder]}/:basename.:extension"
:path => "/files/#{params[:folder]}/:basename.:extension"
when I check I can see folder name in params but I am not able to use it in model.
I also tried interpolation
Paperclip.interpolates :folder do |attachment, style|
attachment.instance.params[:folder]
end
but no result.
Is there any way to make path dynamic using params ?
You need to do some changes to get your desired result.
Add the field folder to your model so that it gets saved when you save your form.
Update your model and remove the path and url options form has_attached_file:
has_attached_file: :upload
Update your environment (e.g. config/environments/development.rb) configuration of paperclip to use the interpolation and a dynamic path:
# Paperclip defaults
config.paperclip_defaults = {
# other paperclip options for storage type e.g. file or S3
# important part
path: '/files/:dynamic_path'
}
Implement the interpolation to create your dynamic path within your config/initializers/paperclip.rb initializer
Paperclip.interpolates :dynamic_path do |attachment, style|
file_name = attachment.original_filename.gsub(/#{File.extname(attachment.original_filename)}$/, '')
folder = attachment.instance.folder
"#{folder}/#{file_name}"
end
I inherited a Rails project and was asked to update it so I am migrating the Rails 2.2.2 project to Rails 3.2.
I went through a few migration tutorials and ran the rails upgrade script and it loads fine when the default /public/index.html is there.
I then went ahead and removed /public/index.html, so the app would point to the file indicated in routes.rb and then I get:
LoadError (Expected /var/www/vendor_sandbox/app/controllers/application.rb to define Application):
app/controllers/application_controller.rb:1:in `<top (required)>'
app/controllers/home_controller.rb:1:in `<top (required)>'
The file that is causing the error was from the original Rails 2.2.2 code base. I left it because there was no indication in the migration docs that I was reading that mentioned removing it but clearly something is wrong.
I find it strange that I now have a Rails 3 version of application.rb in /config and an application.rb in /app/controllers/
Not sure what to do with /app/controllers/application.rb.
Here are the files mentioned:
#### /app/controllers/application.rb
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
# See ActionController::RequestForgeryProtection for details
# Uncomment the :secret if you're not using the cookie session store
protect_from_forgery # :secret => 'mysecretkey'
# See ActionController::Base for details
# Uncomment this to filter the contents of submitted sensitive data parameters
# from your application log (in this case, all fields with names like "password").
# filter_parameter_logging :password
def authenticate
return true if session[:user_id].to_i > 0
session[:after_login] = params
redirect_to :controller => "login"
return false
end
def authenticate_admin
user = User.find(session[:user_id])
#nav = [{:title => "Home", :action => {:controller => "home"}}]
return true if user and user.is_admin?
redirect_to :controller => "login"
return false
end
def clean_date_for_4D date
return "00/00/00" if !date or date == ""
return date.strftime("%m/%d/%Y") if date.class.to_s == "Date"
return Date.parse(date).strftime("%m/%d/%Y") # assume it's a string
end
def pad text, length=20
while text.length < length do
text = text + " "
end
return text
end
end
#### /config/routes.rb
VendorSandbox::Application.routes.draw do
match '/' => 'home#index'
match '/:controller(/:action(/:id))'
end
#### /config/application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
Bundler.require(*Rails.groups(:assets => %w(development test)))
end
module VendorSandbox
class Application < Rails::Application
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
# Enable escaping HTML in JSON.
config.active_support.escape_html_entities_in_json = true
config.active_record.whitelist_attributes = true
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
# Session key
config.session_store(:cookie_store, {:key => '_vendor_sandbox_session', :secret => 'secretkey'
# Time zone
config.time_zone = 'Central Time (US & Canada)' #'UTC' # Had to change this to keep created_at from being 4 hours in advance!
config.active_record.default_timezone = 'Central Time (US & Canada)'
end
end
Trying renaming your app/controllers/application.rb to application_controller.rb.
I think Rails is expecting your controller to be named with a _controller suffix, and the application.rb you have in your controllers folder isn't following that convention.
I wanted to comment on #Zajn answer, but I don't have the required "50 reputation".
Just for reference, app/controllers/application.rb was renamed to application_controller.rb in Rails 2.3
http://guides.rubyonrails.org/2_3_release_notes.html#application-controller-renamed
I have a static set of help pages that I serve in a Rails 3.2 application using Thoughbots high-voltage gem. I'm just using this in a 'vanilla' way, without serving up the html pages via the controller.
Background
I had originally tried to do this myself adapting Michael Hartl's tutorial around static pages - i.e I have a set of static pages with their own controller, and I was trying to create a sub-directory under the static pages view, but could not get my routing to work, so Google searches revealed Thoughbots High-Voltage gem.
Aspiration
What I would like is a recommendation of what gem or method is best to generated PDF files using Thoughtbots High-Voltage gem.
Has anybody done this?
I want to be able to host this on heroku so if there are any gotacha's I'd like to know about these up front.
My current implementation is a basic Rails 3.2 application with the High-Voltage gem installed and a number of views under the pages sub-directory.
pages/help/users
pages/help/products
pages/help/orders
I have images within my html pages, not sure if this causes complications.
EDIT: Added controller based on answer provided as still having issues with wicked_pdf on Rails 3.2.3, ruby 1.9.3-p125 on Lion
class PagesController < HighVoltage::PagesController
def show
respond_to do |format|
format.html do
super
end
format.pdf do
#render :pdf => "pdf_file" # wicked_pdf syntax here
render :pdf => :id,
:layout => 'application',
#:template => 'help/products/product_tolerance.html.erb',
:template => 'pages/#{:id}.html.erb',
:show_as_html => params[:debug],
:footer => {
:left => "Generated on #now",
:centre => "Centre",
:right => "Page # of page(s)"
}
end
end
end
end
Routes file contains:
match "/pages/*id" => 'pages#show', :as => :page, :via => :get, :format => false
Should the :format be true? In the controller and in high_voltage?
Override the High Voltage pages controller as described here:
https://github.com/thoughtbot/high_voltage#override
Then install either pdfkit or wicked_pdf (html to pdf converters)
and hook them into that controller to create PDF versions:
class PagesController < HighVoltage::PagesController
def show
respond_to do |format|
format.html do
super
end
format.pdf do
render :pdf => "pdf_file" # wicked_pdf syntax here
end
end
end
end
I've set up the Resque-web interface to work with my rails 3 app but it doesn't work in either development or production environments. My set up is as follows, and going to localhost:3000/resque just gives me the 404 page I've set up.
Routes.rb
mount Resque::Server.new, :at => "/resque"
Resque initializer
require 'resque'
require 'resque/server'
if Rails.env.development?
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => 'localhost', :port => '6379')
else
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end
Dir["#{Rails.root.to_s}/app/jobs/*.rb"].each { |file| require file }
Resque::Server.class_eval do
use Rack::Auth::Basic do |email, password|
user = User.authenticate( 'foo#bar.co.za', 'password' )
user && user.admin?
end
end
My routes.rb file has:
require "resque/server"
MyApp::Application.routes.draw do
# routes and stuff
mount Resque::Server.new, :at => "/resque"
root :to => 'page#welcome'
end
Maybe you forgot the require inside the routes.rb file?
I do not have a resque initializer, so I don't know if that is causing your problem or not.
I have a route that matches '/:name' to a 'model#index' route must be confusing Resque so I've just mounted it to '/resque/web'
This function is defined in the application_help.rb:
def gravatar_url_for(email, options = {})
url_for(
{
:protocol => 'http://',
:host => 'www.gravatar.com',
:controller => 'avatar',
# :controller => 'avatar.php',
:gravatar_id => Digest::MD5.hexdigest(email),
:only_path => false
}.merge(options)
)
end
It's used in views:
<%= image_tag(gravatar_url_for user.email, {:d => 'identicon', :s => 32, :r => 'g'}) %>
Occasionally, its usage will result in a routing error:
No route matches {:controller=>"avatar", :d=>"identicon", :r=>"g", :gravatar_id=>"486575e581db04b7c8ca218af8488657", :s=>32}
A valid email is being supplied when the error occurs.
If I replace the url_for() with this logic, it works as expected:
url_for("http://www.gravatar.com/avatar/" + Digest::MD5.hexdigest(email) + "?d=identicon&s=40&r=g")
** edit **
I had removed the following line from the routes.rb file:
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
match ':controller(/:action(/:id(.:format)))'
Is there a way to get the url_for to work without the 'legacy wild controller route'?
You might want to take a look at the Gravtastic plugin for Rails which supports Gravatar images in both Ruby and JavaScript.