Permission Denied error when uploading to local filesystem via paperclip gem - ruby-on-rails

I've used paperclip in the past with AWS S3 storage, but with this project I'm trying to reduce external dependencies. I'd like to save uploaded images to the public/assets directory, for immediate use (skirting asset pipeline). Here's the error I'm getting when it tries to save:
Errno::EPERM in PostsController#update
Operation not permitted - /usr/local/src/project_name/public/assets/
...
app/controllers/posts_controller.rb:43:in `update'
I've chmod'ed the directories to 755, as suggested in an existing Stack Overflow solution. That shouldn't even matter though, because thin (my web server) is running as root (via sudo) so that I can bind to 443 (HTTPS). I don't understand why would there even be a permission error for root. What am I not getting? Is the file being uploaded by the user under an anonymous public account, or is the application server saving it as root?
Additional info:
Environment: Development
Operating System: OS X 10.9.3
Ruby version:
MRI ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]
Gem versions:
Gems included by the bundle:
* actionmailer (4.0.3)
* actionpack (4.0.3)
* activemodel (4.0.3)
* activerecord (4.0.3)
* activerecord-deprecated_finders (1.0.3)
* activesupport (4.0.3)
* acts-as-taggable-on (3.2.6)
* arel (4.0.2)
* atomic (1.1.15)
* bcrypt (3.1.7)
* bcrypt-ruby (3.1.5)
* builder (3.1.4)
* bundler (1.5.3)
* climate_control (0.0.3)
* cocaine (0.5.3)
* coffee-rails (4.0.1)
* coffee-script (2.2.0)
* coffee-script-source (1.7.0)
* daemons (1.1.9)
* epiceditor (0.2.2.1)
* erubis (2.7.0)
* eventmachine (1.0.3)
* execjs (2.0.2)
* faraday (0.9.0)
* friendly_id (5.0.4)
* haml (4.0.5)
* haml-rails (0.5.3)
* hike (1.2.3)
* i18n (0.6.9)
* jquery-rails (3.1.0)
* json (1.8.1)
* jwt (1.0.0)
* mail (2.5.4)
* mime-types (1.25.1)
* minitest (4.7.5)
* multi_json (1.8.4)
* multi_xml (0.5.5)
* multipart-post (2.0.0)
* oauth (0.4.7)
* oauth2 (0.9.4)
* paperclip (4.1.1)
* polyglot (0.3.4)
* rack (1.5.2)
* rack-test (0.6.2)
* rails (4.0.3)
* railties (4.0.3)
* rake (10.1.1)
* rdiscount (2.1.7.1)
* sass (3.2.14)
* sass-rails (4.0.1)
* sorcery (0.8.5)
* sprockets (2.11.0)
* sprockets-rails (2.0.1)
* sqlite3 (1.3.9)
* thin (1.6.2)
* thor (0.18.1)
* thread_safe (0.2.0)
* tilt (1.4.1)
* treetop (1.4.15)
* tzinfo (0.3.38)
* uglifier (2.4.0)
File permissions (via ls -la public):
drwxr-xr-x 9 proto admin 306 Jul 6 15:16 .
drwxr-xr-x 23 proto admin 782 Mar 28 06:24 assets
Server Daemon (via ps aux | grep [t]hin):
root 2650 0.0 0.9 2513704 79120 ?? S 3:43PM 0:07.81 thin server (0.0.0.0:443)
Post model:
class Post < ActiveRecord::Base
# Relationships
extend FriendlyId
friendly_id :title, use: [:slugged, :history]
has_attached_file :photo,
path: ':rails_root/public/assets/',
url: ':basename.:extension'
belongs_to :author
# Validations
validates_presence_of :title, :body, :author_id, :slug
validates_uniqueness_of :slug
validates_attachment_content_type :photo, content_type: /\Aimage\/.*\Z/
# Overriding this friendly method to update slug when title changes
def should_generate_new_friendly_id?
slug.blank? || title_changed?
end
end
Update action:
def update
#data = {
title: params[:post][:title],
body: params[:post][:body],
photo: params[:post][:photo]
}
if #post.update! #data
redirect_to #post
else
render :edit, layout: 'layouts/admin'
end
end
Form partial:
= form_for post, html: { multipart: true } do |f|
= f.text_field :title, placeholder: 'Title'
%br
= f.text_area :body, class: 'hidden'
#epiceditor
%br
= f.submit 'Save', class: 'button'
- unless post.new_record?
= link_to 'Delete', post, method: 'delete', class: 'button'
= f.file_field :photo, class: 'upload'
- unless post.photo.nil?
= link_to post.photo.url, post.photo.url

This is too long to write as a comment, but you could use other port than 443, which the root will redirect to from port 443, using, let's say, iptables.
$ sudo iptables -t nat -I PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8443
Then you can start your server normally, as rails user, on port 8443
This is of course if you want to do so.
Regarding the comment/question I've asked to your question, I'm not familiar with thin server, but I would consider the case where the application is running as rails user (proton?) and the frontend web server as root. In that case, root owned assets folder will probably not writeable for rails user....

Change the permissions on public folder to 775.
Update
You might find your solution here: Errno::EPERM (Operation not permitted FILE_PATH) when uploading image with Rails, Carrierwave, Amazon EC2
Looks like you might need to change the owner of public/assets to root (or whichever user the application is running as).

Related

Rails 5 upgrade and font awesome relative path not found in s3

Post rails upgrade from rails 4 too rails 5 font awesome loading path is not correct for more understanding
My Application.css path(correct path)
https://s3_domain/s3_bucket_name/project_name/assets/admin-fingerprint.css
Request Method: GET
Rendered 403 font awesome
https://s3_domain/assets/fontawesome-webfont-fingerprint.woff2
Request Method: GET
Ideally correct path should be->
https://s3_domain/s3_bucket_name/project_name/assets/fontawesome-webfont-fingerprint.woff2
Request Method: GET
related gems in my project
* autoprefixer-rails (10.4.7.0)
* font-awesome-rails (4.7.0.8)
* jquery-fileupload-rails (1.0.0)
* jquery-minicolors-rails (2.2.6.2)
* jquery-rails (4.5.0)
* minitest-rails (3.0.0)
* pry-rails (0.3.9)
* rails (5.1.0)
* sass-rails (6.0.0)
* sassc (2.4.0)
* sassc-rails (2.1.2)
* slim-rails (3.5.1)
* sprockets-rails (3.2.2)

rails asset pipeline images and css not displaying

I am new to rails and am following the examples in Michael Hartl. I am having an issue in chapter 5 (https://www.railstutorial.org/book/filling_in_the_layout). I have followed the michaels instructions but I am not seeing images and my css is not displaying.
I am using ruby Rails 5.0.1 with ruby 2.3.3p222 (2016-11-21 revision 56859) [i386-mingw32]
I added michaels html/code to my home.html.erb
<% provide(:title, "Home") %>
<div class="center jumbotron">
<h1>Welcome to the Sample App</h1>
<h2>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</h2>
<%= link_to "Sign up now!", '#', class: "btn btn-lg btn-primary" %>
</div>
<%= link_to image_tag("rails.png", alt: "Rails logo"),
'http://rubyonrails.org/' %>
<br/><br/>
<%= link_to image_tag("bunratty.png", alt: "Bunratty"),
'http://rubyonrails.org/' %>
<%= link_to image_tag("kitten.png", alt: "Kitten"),
'http://rubyonrails.org/' %>
<%= image_tag image_url('rails.png') %>
<%= image_tag image_path('rails.png') %>
Display using html
<img src="/public/rails.png" alt="rails logo"/>
I copied the rails.png image into app/assets/images/ directory but when I run the application the image is not displayed only the alt tag is displayed.
I also added bootstrap as per the instructions and it isnt working either.
gemfile extract
source 'https://rubygems.org'
gem 'rails', '5.0.1'
gem 'bootstrap-sass', '3.3.6'
I ran bundle install and can see it is installed using a bundle show
$ bundle show
Gems included by the bundle:
* actioncable (5.0.1)
* actionmailer (5.0.1)
* actionpack (5.0.1)
* actionview (5.0.1)
* activejob (5.0.1)
* activemodel (5.0.1)
* activerecord (5.0.1)
* activesupport (5.0.1)
* ansi (1.5.0)
* arel (7.1.4)
* autoprefixer-rails (6.7.5)
* bootstrap-sass (3.3.6)
* builder (3.2.3)
* bundler (1.13.7)
* coderay (1.1.1)
* coffee-rails (4.2.1)
* coffee-script (2.4.1)
* coffee-script-source (1.12.2)
* concurrent-ruby (1.0.4)
* debug_inspector (0.0.2)
* erubis (2.7.0)
* execjs (2.7.0)
* ffi (1.9.17)
* formatador (0.2.5)
* globalid (0.3.7)
* guard (2.13.0)
* guard-compat (1.2.1)
* guard-minitest (2.4.4)
* i18n (0.8.0)
* jbuilder (2.4.1)
* jquery-rails (4.1.1)
* listen (3.0.8)
* loofah (2.0.3)
* lumberjack (1.0.11)
* mail (2.6.4)
* method_source (0.8.2)
* mime-types (3.1)
* mime-types-data (3.2016.0521)
* mini_portile2 (2.1.0)
* minitest (5.10.1)
* minitest-reporters (1.1.9)
* multi_json (1.12.1)
* nenv (0.3.0)
* nio4r (1.2.1)
* nokogiri (1.7.0.1)
* notiffany (0.1.1)
* pg (0.18.4)
* pry (0.10.4)
* puma (3.4.0)
* rack (2.0.1)
* rack-test (0.6.3)
* rails (5.0.1)
* rails-controller-testing (0.1.1)
* rails-dom-testing (2.0.2)
* rails-html-sanitizer (1.0.3)
* railties (5.0.1)
* rake (12.0.0)
* rb-fsevent (0.9.8)
* rb-inotify (0.9.8)
* ruby-progressbar (1.8.1)
* sass (3.4.23)
* sass-rails (5.0.6)
* shellany (0.0.1)
* slop (3.6.0)
* spring (1.7.2)
* spring-watcher-listen (2.0.0)
* sprockets (3.7.1)
* sprockets-rails (3.2.0)
* sqlite3 (1.3.12)
* thor (0.19.4)
* thread_safe (0.3.5)
* tilt (2.0.6)
* turbolinks (5.0.1)
* turbolinks-source (5.0.0)
* tzinfo (1.2.2)
* tzinfo-data (1.2016.10)
* uglifier (3.0.0)
* web-console (3.1.1)
* websocket-driver (0.6.5)
* websocket-extensions (0.1.2)
I added a custom.scss file to app\assets\stylesheets
#import "bootstrap-sprockets";
#import "bootstrap";
/* universal */
body {
padding-top: 60px;
background-color:lime;
}
section {
overflow: auto;
}
textarea {
resize: vertical;
}
.center {
text-align: center;
}
.center h1 {
margin-bottom: 10px;
}
Is this a problem with the assets pipeline since neither the css or images are displaying.
I have tried putting the image in the public directory, the public/images directory, the app/assets directory but it isn't display in any of these.
I have tried using a different image incase the image is corrupted
I have tried using creating a new project
Any help would be greatly appreciated.
Thank your for coming back to me. Some extra information as requested:
I don't have an application.scss file, just an application.css file with the following content:
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
* files in this directory. Styles in this file should be added after the last require_* statement.
* It is generally better to create a new file per style scope.
*
*= require_tree .
*= require_self
*/
The html generated is as follows:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" media="all" href="/stylesheets/default.css" data-turbolinks-track="reload" />
<script src="/javascripts/default.js" data-turbolinks-track="reload"></script>
<title>Home | Ruby on Rails Tutorial</title>
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="SogDGi5kvIl13JKu57VqH0phQ/1pSw+tKuVAmvTVuQITclDhVvKLJNwHJJzcOa0qUlOhJ8cmRsBx9rUCINA+TQ==" />
</head>
<body>
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<a id="logo" href="#">sample app</a>
<nav>
<ul class="nav navbar-nav navbar-right">
<li>Home</li>
<li>Help</li>
<li>About Us</li>
</ul>
</nav>
</div>
</header>
<div class="container">
<div class="center jumbotron">
<h1>Welcome to the Sample App</h1>
<h2>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</h2>
<a class="btn btn-lg btn-primary" href="#">Sign up now!</a>
</div>
<img alt="Rails logo" src="/images/rails.png" />
<br/><br/>
<img alt="Bunratty" src="/images/bunratty.png" />
<img alt="Kitten" src="/images/kitten.png" />
<img src="http://localhost:3000/images/rails.png" alt="Rails" />
<img src="/images/rails.png" alt="Rails" />
Display using html
<img src="/public/rails.png" alt="rails logo"/>
<img src="/public/rails.png" alt="rails logo"/>
<img src="http://localhost:3000/public/rails.png" alt="rails logo"/>
<br/>
http://localhost:3000/images/rails.png
</div>
</body>
</html>
Accessing localhost:3000/assets/rails.png in a browser gives the following error.
No route matches [GET] "/assets/rails.png"
Rails.root: c:/rails/static_app
Assuming your action that generates the following views has a controller calling the layout app/views/layouts/application.html.erb as shown on chapter 5, then you need to require your custom.scss on your application.scss
your controller
class ApplicationController < ActionController::Base
layout 'application'
def index
end
end
your application.scss
/*
= require_self
*/
#import "custom.scss";
The index action is just an example, if you named your view home.html.erb I can imagine you are pointing to a home action inside your controller

Rake task NoMethodError: undefined method `task' for main:Object

I have this code which works fine (!!! without the first line "task..."!!!) in console. It creates events in DB. However no luck when running the rake (rake fetch_ttt):
task :fetch_ttt => :environment do
require 'nokogiri'
require 'open-uri'
url = "http://www.example.com"
doc = Nokogiri::HTML(open(url))
doc.css("#eventrow").each do |item|
unless Event.find_by_name(item.at_css("a").text).present?
Event.create(
:start_time => item.at_css("#eventdate").text,
:name => item.at_css("a").text,
:url => item.at_css("a")[:href]
)
end
end
end
This is the trace (not much in dry run neither):
** Invoke fetch_ttt (first_time)
** Invoke environment (first_time)
** Execute (dry run) environment
** Execute (dry run) fetch_ttt
It has been fine couple hours ago. Since then I did a "bundle update", made some DB migrations, edited associations. I tried rolling back migrations and removed model association changes with no luck. I suspect the gems.
Below the 3 gems I rolled back to the previous versions to see if they are responsible but not. And the full diff.
old ones:
gem 'rake', '10.5.0'
gem 'http', '0.9.8'
gem 'ipaddress', '0.8.2'
full diff:
- bcrypt (3.1.10)
+ bcrypt (3.1.11)
- carrierwave (0.10.0)
+ carrierwave (0.11.2)
+ mimemagic (>= 0.3.0)
- concurrent-ruby (1.0.0)
+ concurrent-ruby (1.0.2)
- devise (3.5.6)
+ devise (4.1.1)
- railties (>= 3.2.6, < 5)
+ railties (>= 4.1.0, < 5.1)
- thread_safe (~> 0.1)
- domain_name (0.5.20160128)
+ domain_name (0.5.20160310)
- excon (0.45.4)
+ excon (0.49.0)
- excon (~> 0.45)
+ excon (~> 0.49)
****lots of stuff related to "fog" gem
- http (0.9.8)
+ http (0.9.9)
- ipaddress (0.8.2)
+ ipaddress (0.8.3)
- mime-types (2.99)
- mini_magick (4.4.0)
+ mime-types (2.99.1)
+ mimemagic (0.3.1)
+ mini_magick (4.5.1)
mini_portile2 (2.0.0)
- minitest (5.8.4)
- multi_json (1.11.2)
+ minitest (5.9.0)
+ multi_json (1.12.0)
- rails_stdout_logging (0.0.4)
+ rails_stdout_logging (0.0.5)
- responders (2.1.1)
+ responders (2.2.0)
- sprockets (3.5.2)
+ sprockets (3.6.0)
- sprockets-rails (3.0.1)
+ sprockets-rails (3.0.4)
- tilt (2.0.2)
+ tilt (2.0.4)
Rails 4.2.5, Ruby 2.1.4, I am on C9 IDE. But doesn't work on heroku neither.
UPDATE
reverted back to rake 10.5.0 (and did grep rake Gemfile.lock) now in the console I only get:
<Rake::Task fetch_ttt => [environment]>
scraping doesn't run at all :(
I don't think there's enough info here to give a definitive answer, but I would start by investigating the root cause for why it isn't working on heroku.
For example, does the rake task run at all? How do you know?
If so, do heroku run rails c and try doing the sequence of code yourself. Inspect the database records before and after you expect a chance. What happened?
If it didn't run, what type of output did you get? heroku logs can also be helpful.
One other little tip, it's possible to use byebug with heroku logs -t. Although I only recommend this in your staging environment and only if you're willing to clean up the git history afterwards.
In short, my answer is I would solve it by debugging more directly. Hope this helps.
outcommented
#require 'nokogiri'
#require 'open-uri'
and now it works. Even with rake 11.1.2

unable to Set up Gravatar on my app

Trying to add Gravatar to my app
but get "could not find generator gravtastic" on the command line
added gem 'gravtastic'
ran bundle install
Using warden (1.2.3)
Using devise (3.1.2)
Using foreigner (1.6.1)
Installing gravtastic (3.2.6)
Using hike (1.2.3)
Using jbuilder (1.0.2)
Using jquery-rails (3.0.4)
Using subexec (0.2.3)
restarted server
rails g gravtastic:install
Could not find generator gravtastic
Do you need to run a generator?
I think you just have to do:
class User < ActiveRecord::Base
include Gravtastic
gravtastic
end
The documentation doesn't mention the gravtastic generator.

Transferring users from Devise 1.1.7 to 2.2.3, for new application

I develop a new application and I want to transfer users from old one. I'd like to let them to use their own old password in new app.
OLD_APP:
config.encryptor = :authlogic_sha512
config.pepper = "xxx"
devise (1.1.7)
bcrypt-ruby (~> 2.1.2)
warden (~> 1.0.2)
NEW_APP:
devise (2.2.3)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.1)
railties (~> 3.1)
warden (~> 1.2.1)
BCrypt
without pepper (config.pepper line left commented)
I used some solutions such as:
Converting existing password hash to Devise
but unfortunately it doesn't works.
My questions is how I could transform SHA512 hash (with salt) to BCrypt (without salt) ?
Whether anybody came across on this issue ?
Thanks.

Resources