Where can I look to see why Paperclip is failing silently in Rails 3? - paperclip

I have followed the simple example here.
I have performed the generation, run the migration, added the code to my model and view, and restarted the application.
This is on a company edit screen, where the user can upload a logo.
Running Rails 3.0.3 in dev mode. The only thing even close to Paperclip that I see in the log is:
Started GET "/logos/original/missing.png" for 127.0.0.1 at Tue Dec 14 15:27:42 -0500 2010
ActionController::RoutingError (No route matches "/logos/original/missing.png"):
I was under the impression that Paperclip was pretty easy to use, but I can't seem to even locate an error message. Can anyone help?

Please set your "default_url" path to a image which is displayed if there is no image.
For example,
has_attached_file :image,
:default_url => '/images/nopicture.jpeg',
:styles => {
:large => "300x300>",
:thumb => "160x120>"
}
Where "nopicture.jpeg" which is available in your "/images" folder under public is the default picture to be displayed if none is available.
This should solve your problem.

Fixed! The power of Google. Or Bing rather. My first problem was, I did not have my form_helper include:
:html => { :multipart => true }
That at least got the call to Paperclip going. But it was hanging.
I am using Passenger to serve up Rails. And it turns out that Passenger did not know where ImageMagick was installed on my machine. So I added an initialization file to config/initializers called "paperclip.rb" with one line:
Paperclip.options[:image_magick_path] = "/opt/local/bin"
Problem solved.

Related

Add support for rendering views to a rails api only application

I built an api only rails app, but since i'm using a 3rd party email service, I need to be able to render the email template into a string to pass it to the mailing service wrapper gem. So far any attempt to render templates into a string returns an empty string, and i suspect it is because the application is configured to be API only. Ho do I add support to rendering templates into a string?
If this is not the case, please let me know. I'm using postmark mail service. postmark-rails gem, which integrates into standard rails mailers didn't work at all, and plain postmark gem (which uses postmark API instead of postmark SMTP server) works fine, but now my problem is producing the proper html for the email.
this is what I'm trying:
html = render_to_string(
partial: "transfer_mailer/transfer.html.erb",
locals: {
:#body => #body,
:#campaign => #campaign,
:#chat => #chat
}
)
but it returns empty string.
My setup involves Rails 5.0.1, ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux], Sidekiq 4.2.7, and in production im using nginx/1.10.2 and Phusion Passenger 5.1.1. Production environment is deployed in an azure virtual machine, and I added both inbound and outbound rules for allowing traffic through ports 25, 465, 2525, and 587.
Any help would be really appreciated.
Looking at the docs for render_to_string, it looks like the path doesn't need to include .html.erb, so maybe removing that will fix it.
You can see here for some example code:
In your case, it would be:
render_to_string(
partial: '/transfer_mailer/transfer',
locals: {
:#body => #body,
:#campaign => #campaign,
:#chat => #chat
},
layout: false
)
Also make sure the partial filename starts with an underscore, even though you don't include it with render_to_string or render calls.
There is the other way to use render_to_string for resolving your issue. Please see the code below.
#body = XXX
#campaign = YYY
#chat = ZZZ
html = render_to_string(
file: 'transfer_mailer/transfer.html.erb'
)
Hope it helps.

EncryptedCookie gem causes TypeError in Sinatra

I'm trying to make my Sinatra app's sessions more secure, and to do that I'd like to use the EncryptedCookie gem. Unfortunately, when I visit any page in my app, I get this error:
TypeError at /
no _dump_data is defined for class UnboundMethod
file: encrypted_cookie.rb location: dump line: 68
Here's my code:
configure do
use Rack::Session::EncryptedCookie,
:key => 'myapp.session',
:domain => 'myapp.com',
:path => '/',
:expire_after => 1200,
:secret => 'bigcrazysecretstringhere'
end
I tried using the EncryptedCookie gem with the same settings as shown above in a simple Sinatra app I made to test the gem, and it worked fine. There must be some other setting in my app that's interfering with the app, but I can't figure out what it might be. Has anyone out there experienced a similar issue?
(I've also tried starting the app with 'thin start', 'rackup config.ru', and 'ruby myapp.rb'- none of these made a difference.)
After banging my head against the wall for well over a week, I discovered the contractor who worked on the project before me had put
use Rack::Session::Pool
200 lines below the original
use Rack::Session::Cookie
I removed that and Encrypted Cookie worked fine. Lesson learned: Immediately abstract away everything you can when handed a 500 line Sinatra app. Then things can't hide from you.

Bad Request/Bad URI on production (Heroku) but not locally

Let me preface this by saying most of this application is a giant hack put together in a short window of time under pressure so I may have deeper issues. This question will likely have some bad code in it.
I've built a Sinatra application to handle some tasks with purchase and sales orders in-house. Part of that is to send a few parameters to one of the routes in the app which will then push those off to an API that does useful things with them.
Right now, I'm generating the links with paramaters from within a pretty ugly loop in HAML:
%td
- opts = JSON.generate({ "key" => d[d.keys.first]["key"], "sa_id" => d.keys.first, "site" => d[d.keys.first]["site"], "name" => d[d.keys.first]["name"], "recipient" => d[d.keys.first]["email"], "items" => d[d.keys.first]["descriptions"], "date" => d[d.keys.first]["ship_date"]})
-if (d[d.keys.first]["email"]) && (d[d.keys.first]["site"] != "")
%a{:href => "/notify?options=#{opts}", :title => "Deliver"} Deliver
-else
Deliver
%a{:href => "/destroy?key=#{d.keys.first}", :title => "Destroy"} Destroy
When clicking hte "deliver" link (%a{:href => "/notify?options=#{opts}", :title => "Deliver"} Deliver) locally, everything behaves as expected. My /notify route is called, it hands the parameters off to the desired API, and everything is rainbows and unicorns. When I click that same link on Heroku, it throws a "Bad Request" stating "Bad URI". The only difference between the two URLs generated is the hostname (localhost:3000 vs. myapp.herokuapp.com) and vimdiff confirms this.
Everything else being even, why would Heroku (using Webrick) kick back my URI when my local instance (Thin) doesn't seem to care?
Switching from Webrick to Thin on Heroku resolved this issue, though I'm not sure about the specifics as to why.
We've had some issues where development and production environments that were seemingly identical behaved differently once deployed on heroku. A quick sanity check I like to do is to diff the local gemfile.lock to the one on heroku after the deploy process is complete. I know - we should lock our gem versions down more tightly and we will do, so don't consider this a best practice - but it unearthed some hard to track down issues, some of which were related to the fact that we have some devs on Windows and some on Mac and the gemfile.lock often had Windows specific gems which causes issues on heroku. Just something to consider.

Undefined method 'session' using Sinatra for Facebook Canvas Application

I am using sessions in my Facebook Canvas Application using Sinatra and Rack.
The error that occurs in Facebook Canvas is:
NoMethodError - undefined method `session' for #<Hash:0xa3ed0a0>:
/home/apoorv/.rvm/gems/ruby-1.9.2-p320/gems/sinatra-1.3.2/lib/sinatra/base.rb:170:in `session'
The problem is surely with Rack because when I run my application as follows:
ruby application.rb -p 3000
it does not display any error. I have tried installing rack version: 1.3.6 and 1.4.1, but the error persists.
I have also tried using the following code instead of enable :sessions
use Rack::Session::Cookie, :key => 'rack.session',
:domain => 'static.ak.facebook.com',
:path => '/',
:secret => 'change_me'
Do I need to upgrade/degrade to lower version of Rack or add some piece of Code to make this thing work?
edited
Also before coming across this issue I had an issue integrating my application in Facebook Canvas which was resolved by adding this line in config.ru:
set :protection, :except => [:remote_token, :frame_options]
Using env['rack.session'] instead of session[] in the POST request recieved from Facebook solved this issue. Hope this helps to solve such problems
Though it has not fully resolved complications because, now adding a redirect to code in the same POST callback displays: undefined method secure? error.
I have not been able to solve this issue, though what I did was to avoid the redirect thing and instead add a functionality to the controller to handle different requests.

ActionController::RoutingError (uninitialized constant User::UsersController) in heroku (but everything works in local)

I am trying to run my app in heroku but I get this error when trying to signup or even access devise's login page:
ActionController::RoutingError (uninitialized constant User::UsersController)
Is this a devise bug or a server setting i missed in heroku?
I am running a rails3.1 app in a cedar stack by the way and loading the index page is good, but if I try to login or sign up, it blows.
The signup form DOES show, but when I submit, that's when it blows. I checked the logs and it did POST to the controller but GETting the resulting page(when redirected I guess) blows it up.
Any help?
EDIT
here are my routes:
root :to => "home#index"
devise_for :users
namespace :user do
root :to => "users#welcome"
end
resources :users, :only => :show
A heroku support person also asked about my routes but why does it happen in production only? Also I don't think there's any problem with the routes...is there?
I found the you do not need to remove the default root for when a user logs in. So replace the namespace call and use the following:
match 'users' => 'users#welcome', :as => 'user_root'
This way you can still have two "home" pages. It worked for me.
https://github.com/plataformatec/devise/wiki/How-To%3a-Redirect-to-a-specific-page-on-successful-sign-in
This is your problem:
namespace :user do
root :to => "users#welcome"
end
Can you remove this?
I got the same error. Error was only reproducible in Heroku, not locally. What I realized is that while I had added the resource to routes and pushed that, all the generated scaffold was still lying locally. Once I added all the generated stuff to git and pushed, worked fine on Heroku.

Resources