Sinatra + Paperclip; uploaded images have no extension - sinatra

When uploading images Paperclip is saving files wo/ extensions. I'm using ActiveRecord
photo.rb (model):
require "sinatra"
class Photo < ActiveRecord::Base
include Paperclip::Glue
attr_accessible :image
has_attached_file :image,
:styles => {
:small => "100x100#",
:medium => "500x300"
},
:path => "#{settings.root}/public/system/:class/:attachment/:attachment/:id_partition/:style/:filename"
end
photos.rb (controller):
post "/photos/?" do
params[:photo][:image] = params[:photo][:image][:tempfile] if params[:photo][:image]
#photo = Photo.new(params[:photo])
if #photo.save
{ :status => "OK"}.to_json
else
{ :status => "NOK"}.to_json
end
end

Related

Promoted Object Is Missing when creating a Creative on Facebook Marketing API

I'm trying to create a new ad creative with the following HTTP request
response = HTTParty.post(
"https://graph.facebook.com/v2.9/act_#{account_id}/adcreatives",
:query => {
:ad_format => "DESKTOP_FEED_STANDARD",
:access_token => #ads_access_token,
:creative => {
:object_story_spec => {
:object_id => page_id,
:link_data => {
:call_to_action => {
:type => ad_cta,
:value => {
:link => ad_url
}
},
:link => ad_url,
:message => ad_text,
:description => ad_description,
:name => ad_headline,
:picture => ad_image_url
},
:page_id => "233558376717908"
}
}
}
)
But unfortunately I keep on getting the following error:
{"error"=>
{"message"=>"Invalid parameter",
"type"=>"OAuthException",
"code"=>100,
"error_subcode"=>1487930,
"is_transient"=>false,
"error_user_title"=>"Promoted Object Is Missing",
"error_user_msg"=>
"Your campaign must include an ad set with a selected object to promote related to your objective (ex: Page, URL, event). Please update your ad set to continue..",
"fbtrace_id"=>"DpnhEGhLaSj"}}
I did set up a promoted object for the adset, but I don't understand what it has to do with the adcreative.
Thanks!

Don't show 's3.amazon.com' when posting images to LinkedIn API

I am posting images to LinkedIn through the public api with my app that pulls the images from our S3 bucket. When the update appears on LinkedIn it shows 's3.amazon.com' next to the image. Is there any way that I can keep this from happening?
Ruby Code from app:
def post(message_body, attachment, post)
options = { :visibility => { :code => 'anyone' } }
if attachment.present?
options.merge!(:content => { :title => 'titles',
:description => 'descriptions',
:submitted_url => 'http://s3.amazonaws.com/...'
}, :comment => message_body)
else
options.merge!(:comment => message_body)
end
response = client.add_share(options)
post.posted!
JSON.parse(response.body)['updateUrl']
rescue LinkedIn::Errors::UnauthorizedError => e
raise SocialProfile::UnauthorizedError, e.message
rescue LinkedIn::Errors::AccessDeniedError => e
raise SocialProfile::UnauthorizedError, e.message
## Heading ##end

How to use PayPal for checkout in rails4

I am trying to add PayPal e-paiement to my RoR application. so I proceed as follow:
My panier.rb:
class Panier < ActiveRecord::Base
belongs_to :user
def paypal_url(return_path)
values = {
:business => 'r.karoui-buyer#loganddrive.com',
:cmd => '_cart',
:upload => 1,
:return => return_path,
:invoice => id
}
line_items.each_with_index do |item, index|
values.merge!({
"amount_#{index+1}" => item.price,
"item_name_#{index+1}" => item.book.title,
"item_number_#{index+1}" => item.id,
"quantity_#{index+1}" => 1
})
end
"#{Rails.application.secrets.paypal_host}/cgi-bin/webscr?" + values.to_query
end
end
my details.html.erb: where i call the PayPal service
<%= #panier.book_id %> | <%= #panier.price %> | <%= link_to 'acheter' , #panier.paypal_url(paniers_path) %>
but I got this error:
undefined local variable or method `line_items' for #
i just follow this railsCast tutorial :
http://railscasts.com/episodes/141-paypal-basics
what should I put in place of line_items.each_with_index do |item, index| if it is here the errors if not please help me to find out where is the problem
Try
def paypal_url(return_path)
values = {
:business => 'r.karoui-buyer#loganddrive.com',
:cmd => '_cart',
:upload => 1,
:return => return_path,
:invoice => id
}
values.merge!({
"amount_#{index+1}" => item.price,
"item_name_#{index+1}" => item.book.title,
"item_number_#{index+1}" => item.id,
"quantity_#{index+1}" => 1
})
"#{Rails.application.secrets.paypal_host}/cgi-bin/webscr?" + values.to_query
end

OR operator in Mongo 'update' $criteria

I want to check for an existing record by matching either title or url fields. If either one matches, update that record. Otherwise, insert.
How do write the following properly (using Mongoid in Ruby):
articles.update(
{ **:title => story.title OR :url => story.url** },
{ :title => story.title, :url => story.url, :source => story.source, :last_updated => Time.now },
{ :upsert => true } )
Thanks!
You need do the request and update it like :
'''
articles.any_of({:title => xxx}, {:url => yyyy}).update( :foo => 'bar')
'''

iphone can't post to my rails app

So I have been trying out several different ways to connect my iPhone app to my ROR restful backend and all seems to work ok pulling down data (json) with 'get' requests, but when posting I am unable to do so. I have tried ObjectiveResource and two or three others and they all have the same issue. I am thinking its something set wrong in my ROR app? I did notice all the example iPhone projects use https for production apps, does a production app need to https for an iPhone to establish a session and be able to post?
HERE is what I get for an error if I use http://localhost:3000/posts
Processing PostsController#create (for 127.0.0.1 at 2011-04-05 20:49:42) [POST]
Parameters: {"post"=>{"budget"=>"222"}}
User interests hash: false
NoMethodError (undefined method posts' for false:FalseClass):
app/controllers/posts_controller.rb:113:increate'
If I use http://localhost:3000
Processing PostsController#index (for 127.0.0.1 at 2011-04-05 20:49:42) [POST]
Parameters: {"post"=>{"budget"=>"222"}}
Here is my Create method:
def create
##post = Post.new(params[:post])
#post = current_user.posts.build(params[:post])
respond_to do |format|
if #post.save
flash[:notice] = 'Post was successfully created.'
format.html { redirect_to(#post) }
format.xml { render :xml => #post, :status => :created, :location => #post }
format.json { render :json => #post, :status => :created, :location => #post }
else
format.html { render :action => "new" }
format.xml { render :xml => #post.errors, :status => :unprocessable_entity }
format.json { render :json => #post.errors, :status => :unprocessable_entity }
end
end
end
Here is the method:
# POST /posts
# POST /posts.xml
def create
##post = Post.new(params[:post])
#post = current_user.posts.build(params[:post])
respond_to do |format|
if #post.save
flash[:notice] = 'Post was successfully created.'
format.html { redirect_to(#post) }
format.xml { render :xml => #post, :status => :created, :location => #post }
format.json { render :json => #post, :status => :created, :location => #post }
else
format.html { render :action => "new" }
format.xml { render :xml => #post.errors, :status => :unprocessable_entity }
format.json { render :json => #post.errors, :status => :unprocessable_entity }
end
end
end
Iphone call:
// CRUD methods using Resource.h
- (void)createRemote {
NSString *url =
[NSString stringWithFormat:#"%#/posts", siteURL];
[Resource post:[self params] to:url];
}
%#/posts is equal to http://localhost:3000/posts
Do you have protect_from_forgery set on your ApplicationController?
This essentially kills POSTing information from anywhere except the forms on your website. Try commenting it out and see if things start working.
As peterjb said, check for forgery protection. If you're on rails 3 then look for the csrf_meta_tag in your application.html.erb.
https -- is not must, it is how your server configures for the security needs. You can have http and https both no issues.
I suggest you to have look at the log in ROR app end too know what you get HTTP POST data -- if you are not able to get any data in ROR app end post some code which does POST request from your iPhone, people can analyse it better.