Rake task NoMethodError: undefined method `task' for main:Object - ruby-on-rails

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

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)

Permission Denied error when uploading to local filesystem via paperclip gem

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).

Handling OmniAuth::Strategies::OAuth2::CallbackError in OmniAuth

How can I handle the error:
OmniAuth::Strategies::OAuth2::CallbackError
...generated by OmniAuth when a user goes through to the Facebook login, but decides to cancel?
I have seen a few threads about this, but none of the solutions worked for me.
I tried placing this:
OmniAuth.config.on_failure = UsersController.action(:oauth_failure)
...into the omniauth.rb initializer file with no success.
I am using the omniauth-facebook gem with Rails 4.0.2.
Any help greatly appreciated.
Many thanks!
Michael.
My gemfile.lock file reveals the following gems related to the omniauth-facebook gem:
oauth2 (0.8.1)
faraday (~> 0.8)
httpauth (~> 0.1)
jwt (~> 0.1.4)
multi_json (~> 1.0)
rack (~> 1.2)
omniauth (1.1.4)
hashie (>= 1.2, < 3)
rack
omniauth-facebook (1.5.1)
omniauth-oauth2 (~> 1.1.0)
omniauth-oauth2 (1.1.1)
oauth2 (~> 0.8.0)
omniauth (~> 1.0)
I upgraded the omniauth-facebook gem to version 1.0.6 and now it is working as expected.
For anyone else coming across this issue and would like to capture this error, here's what you need:
/config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, 'xxx', 'xxx', scope: "email,publish_stream,user_location,user_birthday"
end
/config/initializers/omniauth_failure_callback.rb
OmniAuth.config.on_failure = Proc.new do |env|
UsersController.action(:omniauth_failure).call(env)
end
/app/controllers/users_controller.rb
def omniauth_failure
flash[:danger] = "Unable to connect with Facebook at this time."
redirect_to root_url
end

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