How to make multiple file uploading via CarrierWave and Sinatra? - ruby

I'm not first who is asking and maybe not last. How to implement multiple uploading with CarrierWave in Sinatra? I'm using this code in action:
post '/create' do
params.delete 'submit'
d = Dcmnt.new(
:published => params[:published],
:name => params[:name],
:description => params[:description],
:created_at => Time.now
)
d.attachment = params[:attachments]
d.save
redirect '/success'
end
With this model:
class Dcmnt
include Mongoid::Document
store_in collection: 'dcmnts'
field :published, type: Boolean
field :name, type: String
field :description, type: String
field :created_at, type: Date
mount_uploader :attachment, Uploader, type: String
end
And this form:
%form{:method => "post", :action => "/create", :enctype => "multipart/form-data"}
%input{:type => "checkbox", :name => "published", :value => "true"} Published?
%input{:name => "name"}
%textarea{:name => "description", :rows => "5"}
%div.form-group
%label Attachments
%input{:type => "file", :name => "attachments[]"}
%input{:type => "file", :name => "attachments[]"}
%input{:type => "file", :name => "attachments[]"}
%input{:type => "file", :name => "attachments[]"}
%input{:type => "file", :name => "attachments[]"}
Also CW's configuration:
class Uploader < CarrierWave::Uploader::Base
storage :file
def store_dir
'attachments/' + model.id
end
end
Looks correct, but doesn't work. When I'm trying to upload couple of files either single file, Pow returns me no implicit conversion of nil into String on d.attachment = params[:attachments] line. And I can't figure out why. Can you help me with this?

Related

Fedex Ruby Gem: Customs Value is required - how to add?

I'm working with the Ruby gem 'FedEx', https://github.com/jazminschroeder/fedex.
I've set up my code for a development mode and I'm testing making a shipment.
However, I get stuck with the following error:
C:/Ruby22/lib/ruby/gems/2.2.0/gems/fedex-.10.1/lib/fedex/request/shipment.rb:134:in 'failure_response': Customs Value is required. (Fedex:: RateError) from C: /Ruby22/lib/ruby/gems/2.2.0/gems/fedex-.10.1/lib/fedex/request/shipment.rb:32:in 'process_request' from C: /Ruby22/lib/ruby/gems/2.2.0/gems/fedex-3.10.1/lib/fedex/shipment.rb:57:in 'ship' from C: /Ruby22/bin/css_fedex_v1.rb:92:in ''
It seems that I need to parse a 'Customs Value', probably as part of my 'packages' hash. However, I'm unable to find the relevant field for me to enter this in. Anyone who's experienced this and found a solution?
My code is as below:
require 'fedex'
fedex = Fedex::Shipment.new(:key => '***',
:password => '***',
:account_number => '***',
:meter => '***',
:mode => 'development')
shipper = { :name => "***",
:company => "***",
:phone_number => "***",
:address => "***",
:city => "***",
:postal_code => "***",
:country_code => "DK" }
recipient = { :name => "***",
:company => "***",
:phone_number => "***",
:address => "***",
:city => "***",
:postal_code => "***",
:country_code => "GB",
:residential => "false" }
packages = []
packages << {:weight => {:units => "LB", :value => 1}}
shipping_options = {:packaging_type => "YOUR_PACKAGING",
:drop_off_type => "REGULAR_PICKUP"}
rate = fedex.rate(:shipper=>shipper,
:recipient => recipient,
:packages => packages,
:shipping_options => shipping_options)
ship = fedex.ship(:shipper=>shipper,
:recipient => recipient,
:packages => packages,
:service_type => "INTERNATIONAL_PRIORITY",
:shipping_options => shipping_options)
puts ship[:completed_shipment_detail][:operational_detail][:transit_time]
Customs value is declared in their docs:
https://github.com/jazminschroeder/fedex/commit/9f1d4c67b829aaa4eeba9090c1a45d3bd507aab3#diff-4f122efb7c0d98120d8b7f0cd00998e4R106
customs_value = { :currency => "USD",
:amount => "200" }
As I understand you can pass it into the commodities hash or keep it separate.

Ruby/Highrise API - Not saving phone number?

So I've been using this same block of code for about 9 months and suddenly it has stopped saving a phone number into Highrise when a new Lead is generated... Any idea where things are going sideways? I never throws an error and happily saves the contact with everything except for the telephone number.
# create a contact Highrise from supplied information
#person = Highrise::Person.new(first_name: #lead.first_name.humanize,
last_name: #lead.last_name.humanize,
background: "automatically created by LSAL app",
contact_data: { email_addresses: [ { address: #lead.email, location: "Home" } ],
phones: [ { number: #lead.phone, location: "Mobile" } ] } )
#person.save # save contact
#person = Highrise::Person.create(:company_name => account.company, :first_name => account.first_name, :last_name => account.last_name, :contact_data => { :email_addresses => [{ :email_address => {:address => account.email, } }], :addresses => [{:address => {:city => account.city, :location =>"Work", :state => account.state, :country => account.country,:street => "#{account.address1} #{account.address2}" , :zip => account.zip} }], :phone_numbers => [{ :phone_number => { :number => number, :location =>"Work"}}] } )
Hope its helpful

How do I Work Around " Encoding::UndefinedConversionError: ""\x9D"" from Windows-1252 to UTF-8"?

Here's the code that generates the error:
issues.each
{
|issue|
response = RestClient::Request.new
(
:method => :get,
:url => "http://wls-eng1:8080/rest/api/2/issue/{issue['key']}/comment",
:user => username,
:password => password,
:headers => { :accept => :json, :content_type => :json },
:ssl_ca_file => :temp
).execute
}

Active_admin and :filter

The issue lies in the following: filter :contact, :as => :string works successfully if I type the id for the contact. But that's not practical when you have 2000+ contacts. How can I successfully filter :contact, as => :string but have it search for :name instead of :id.
I have tried the following with no success:
filter :contact, :as => :string, :collection => proc {Contact.where(:name => 'Paul' )}
filter :contact, :as => :string, :collection => proc { (Contact.order.all).resources{|c| [c.name]}}
Note: my repository can be found here.
Model: order.rb
belongs_to :contact
Migration:
def change
create_table :orders do |t|
t.string :tag
t.text :description
t.string :technician_id
t.string :status
t.string :type
t.string :contact_id
t.string :business_id
t.timestamps
end
end
admin/orders/ - orders.rb
filter :business
filter :contact, :as => :string, :collection => proc { (Contact.order.all).resources{|c| [c.name]}}
filter :tag
filter :description, :label => "Status"
filter :created_at
index do
column :business
column :contact
column :tag
column :status
column :description, :sortable => false do |order|
truncate(order.description, :length => 30)
end
Activeadmin uses meta_search gem, so try this:
filter :contact_name, :as => :string

Short-form for validates_exclusion_of

If
validates_presence_of :login,
:message => 'How do you expect to login?'
has a short-form variant
validates :login, :presence => { :message => 'How do you expect to login?' },
what is the short-form version of
validates_exclusion_of :login, :in => ['admin'],
:message => "\"{{value}}\" is reserved."
so I can also have a custom message?
validates :login, :exclusion => { :in => ['admin'], :message => "%{value} is reserved" }

Resources