I am working with creating zip file after read users cv's. Code is working fine on local machine. But there is error when deploy this code on heroku.
def get_stream(contractors)
#t = Tempfile.new("my-temp-filename-#{Time.now}") #this code on local
t = Tempfile.new("#{Rails.root}/tmp/my-temp-filename-#{Time.now}") #this line on heroku break
begin
Zip::OutputStream.open(t.path) do |zos|
contractors.each_with_index do |contractor, index|
if contractor.cv.path
zos.put_next_entry("#{index+1}_#{contractor.full_name.parameterize.underscore}.#{contractor.cv_file_name.split(".").last}")
zos.print IO.read(contractor.cv.path)
end
end
end
send_file t.path, :type => 'application/zip', :filename => "Contractors_cvs.zip", :x_sendfile => true
t.close
rescue
t.close
end
end
while heroku logs --app just show me "Completed 500 Internal Server Error in 56ms"
Any help please.
Thanks
Finally this works for me on both local and heroku.
def get_stream(contractors)
begin
Zip::File.open("#{Rails.root}/tmp/zipfile_name.zip", Zip::File::CREATE) do |zipfile|
contractors.each_with_index do |filename, index|
zipfile.add(filename.try(:cv_file_name), filename.try(:cv).try(:path)) if filename.present?
end
end
send_file "#{Rails.root}/tmp/zipfile_name.zip", :type => 'application/zip', :filename => "Contractor_cvs.zip", :x_sendfile => true
File.delete("#{Rails.root}/tmp/zipfile_name.zip")
rescue
end
end
Related
I have following on config/application.rb
config.action_mailer.default_url_options = { :host => 'xxxxx.com' }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.mandrillapp.com",
:port => 587,
:domain => 'xxxxx.com',
:user_name => 'xxx.xxxxx#gmail.com',
:password => 'xxxxxxxxxxxxxxxx',
:authentication => 'plain',
:enable_starttls_auto => true }
And in app/mailers/welcome_mailer.rb
def welcome_email(user)
#user = user
#lang=I18n.locale
if #user.email.present?
begin
headers = {
:subject => welcome_email_subject(#user).to_s,
:from => ADMINISTRATIVE_EMAIL,
:to => user.email
}
mail(headers)
rescue Exception => e
abort
end
end
end
I have a template on /app/views/welcome_mailer/welcome_email.html.erb
I am using this mailer action for sending welcome emails along with confirmation link by using devise.For that I have done the following on /config/initializers/welcome_mailers.rb
module Devise::Models::Confirmable
def send_on_create_confirmation_instructions
if self.email
WelcomeMailer.welcome_email(self).deliver
end
end
def send_reset_password_instructions
generate_reset_password_token! if should_generate_reset_token?
WelcomeMailer.generate_password(self).deliver
end
end
Even though the development I have used same smtp configurations I am getting empty body for the mail sent on production and the same working fine in development(local).By the way my production environment is Amazon EC2. Initally 15 days before I have got the same issue and I solved by changing the smtp account.Now it is not happening in any order.Suggest with your feedback or comments.
You can overwrite the template route in the mailer config:
class WelcomeMailer < ActionMailer::Base
...
self.template_root = "#{RAILS_ROOT}/app/views/mailers" # Rails.root in 3.x
...
end
I'm trying to upload multiple images with paperclip through 1 form, but I'm getting a Unpermitted parameters error.
This is my code:
Model:
class Recentjacket < ActiveRecord::Base
has_attached_file :jacketimage, :styles => { :medium => "300x300>", :thumb => "100x100>"}, :default_url => "/images/:style/missing.png"
end
Controller:
def recent
#recentjacket = Recentjacket.all
end
def update
params['recentjacket'].keys.each do |id|
#jacket = Recentjacket.find(id)
#jacket.update_attributes(recentjacket_params)
end
redirect_to '/recent'
end
private
def recentjacket_params
params.require(:recentjacket).permit(:jacketimage)
end
Html.slim
= form_for recent_path, html: { multipart: true } do |k|
- #recentjacket.each do |j|
= fields_for "recentjacket[]", j do |jacketfields|
= jacketfields.file_field :jacketimage
= k.submit "Update"
So basically there are 12 recentjackets in the database and when something is changed, it should overwrite the image.
Does anyone know how to fix this?
I fixed the problem:
def update
if params.has_key?(:jacket)
Recentjacket.update(params[:jacket].keys, params[:jacket].values)
redirect_to '/recent'
else
redirect_to '/recent/edit',
notice: 'No Files were selected to upload!'
end
end
In my invoice system, I want a backup function to download all invoices at once in one zip file.
This system is running on heroku - so it's only possible to save the pdfs temporary.
I've the rubyzip and wicked_pdf gem installed.
My current code in the controller:
def zip_all_bills
#bill = Bill.all
if #bill.count > 0
t = Tempfile.new("bill_tmp_#{Time.now}")
Zip::ZipOutputStream.open(t.path) do |z|
#bill.each do |bill|
#bills = bill
#customer = #bills.customer
#customer_name = #customer.name_company_id
t = WickedPdf.new.pdf_from_string(
render :template => '/bills/printing.html.erb',
:disposition => "attachment",
:margin => { :bottom => 23 },
:footer => { :html => { :template => 'pdf/footer.pdf.erb' } }
)
z.puts("invoice_#{bill.id}")
z.print IO.read(t.path)
end
end
send_file t.path, :type => "application/zip",
:disposition => "attachment",
:filename => "bills_backup"
t.close
end
respond_to do |format|
format.html { redirect_to bills_url }
end
end
This ends with the message
IOError in BillsController#zip_all_bills closed stream
I think what is wrong in your code is that you have t for your zip but you also also use it for individual pdfs. So I think when you try to use the tempfile for a pdf it is a problem because you are already using it for the zip.
But I don't think you need to use tempfiles at all (and I never actually got a tempfile solution working on Heroku)
Here is a controller method that works for me--also using wickedpdf and rubyzip on heroku. Note that I'm not using a Tempfile instead I'm just doing everything with StringIO (at least I think that's the underlying tech).
def dec_zip
require 'zip'
#grab some test records
#coverages = Coverage.all.limit(10)
stringio = Zip::OutputStream.write_buffer do |zio|
#coverages.each do |coverage|
#create and add a text file for this record
zio.put_next_entry("#{coverage.id}_test.txt")
zio.write "Hello #{coverage.agency_name}!"
#create and add a pdf file for this record
dec_pdf = render_to_string :pdf => "#{coverage.id}_dec.pdf", :template => 'coverages/dec_page', :locals => {coverage: coverage}, :layout => 'print'
zio.put_next_entry("#{coverage.id}_dec.pdf")
zio << dec_pdf
end
end
# This is needed because we are at the end of the stream and
# will send zero bytes otherwise
stringio.rewind
#just using variable assignment for clarity here
binary_data = stringio.sysread
send_data(binary_data, :type => 'application/zip', :filename => "test_dec_page.zip")
end
I am able to upload image files as attachments using Attachment_fu but when I try to edit/ modify images which were already uploaded, it does not work.
In my Controller I have this:
def update
#sponsor_account = SponsorAccount.find( params[:id] )
if params[:showcase_category_image_file] &&
!params[:showcase_category_image_file].blank?
#sponsor_account.showcase_category_image =
ShowcaseCategoryImage.new(:uploaded_data =>
params[:showcase_category_image_file])
***This logs - Now the file name is: ***
Rails.logger.info("Now the file name is: #
{#sponsor_account.showcase_category_image.filename}")
end
#sponsor_account.update_attributes( params[:sponsor_account] )
if #sponsor_account.save
flash[:notice] = "Showcase category #{#sponsor_account.name} was updated!"
else
flash[:errors] = #sponsor_account.errors
end
redirect_to sponsor_accounts_path
end
ShowcaseCategoryImage is defined as follows:
has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 5.megabytes,
:thumbnails => { :large => [350, 100], :medium => [200, 90], :thumb =>
[35,35], :preview => [60,60] }
validates_as_attachment
The view has a file_field_tag as follows:
<%= file_field_tag :showcase_category_image_file%>
and my SponsorAccount model says:
has_one :showcase_category_image, :as => :owner, :dependent => :destroy
validates_presence_of :showcase_category_image, :on => :create
Almost similar code works perfectly ok in 'create' but here in 'update' action where there is already a value, this code is not working.
I am getting the below error msgs:
Completed 500 Internal Server Error in 1089ms
ActionView::Template::Error (undefined method `public_filename' for nil:NilClass):
Obviously this error is in the index action where it tries to list all records and their attachments. Since this attachment is empty after the update, this error is thrown in the redirect_to part.
I am using REE1.8.7 and rails 3.2.9
Please help!
This issue was solved when I added :multipart => true in the 'View'. I am using rails 3.2.9 and the rails api has this to say about the 'file_field_tag':
file_field_tag(name, options = {}) Link
Creates a file upload field. If you are using file uploads then you will also need to set the multipart option for the form tag:
Which version of authlogic are people using with Rails 3.1.
I have the following entry in my gemfile:
gem 'authlogic', :git => 'https://github.com/AndreasWurm/authlogic.git'
The problem I have is with a piece of code in my base ApplicationController.
def require_no_user
if current_user
store_location
flash[:notice] = "You must be logged out to access this page"
redirect_to :controller => "home", :action => "index"
return false
end
end
def store_location
session[:return_to] = request.request_uri
end
The error I am getting is with the line:
session[:return_to] = request.request_uri
I am getting an error saying:
undefined method `request_uri' for #<ActionDispatch::Request:0x7dadd4d8>
Has Request_uri been removed from ActionDispatch and if so, what is the correct alternative?
The best solution is as said Vadim, using the new methods in ActionDispatch::Request :
You just replace :
def store_location
session[:return_to] = request.request_uri
end
by :
def store_location
session[:return_to] = request.url
end
and it's done !
fullpath will give you url(but without protocol, port, domain) with params
and request.url will give you everything that fullpath skips