I've created a new project with Rails 5.0.0 and Ruby 2.5 on my local mac with macOs 10.12. Now I'm trying to add paperclip to this project but still no luck.
My model class looks like this:
class Photo < ApplicationRecord
has_attached_file :file, styles: { big: '1280x1024>', small: '640x480>' }
validates_attachment :file, content_type: { content_type: /\Aimage/ }, file_name: { matches: [/png\Z/i, /jpe?g\Z/i, /gif\Z/i] }, size: { less_than: 15.megabytes }
end
Whatever version of paperclip I try I don't have a paperclip generator in my project and I'm geting errors like this when I try to call a model or it's methods:
"NoMethodError (undefined method `has_attached_file' for Photo (call 'Photo.connection' to establish a connection):Class)"
When I add "include Paperclip::Glue" like suggested here https://github.com/thoughtbot/paperclip/issues/705 error changes to
NameError (uninitialized constant Photo::Paperclip)
Is there any way for me to bypass this mess ? ><
Link to paperclip issues
https://github.com/thoughtbot/paperclip/issues/2555
Link provided by hamdi in the first comment is an answer.
If you've got problems with Devise or with Paperclip like that, don't try to add "include Paperclip::Glue" or in case of Devise "extend Devise::Models". The only right way to fix this is to rollback ALL of your migrations, terminate console, start it again, migrate, terminate console one more time and thats it! Sounds stupid but it's working >< Pictures upload is working and paperclip generator is on the list.
For all of you who came from Rails 4:
2.5.0 :001 > Photo
=> Photo (call 'Photo.connection' to establish a connection)
is totally ok behavior for console in Rails 5. Before accessing model you've got to perform 'Photo.connection' from now on. If you don't like it you can always fix it by adding
console do
ActiveRecord::Base.connection
end
to your config/application.rb
Related
I am using ActionText to edit a paragraph and it works perfectly locally but when I deploy it to Heroku the page which has the form with rich_text_area it throws an error saying undefined method rich_text_area_tag even though I followed the rails guide. I thought I needed to configure Active Storage on production but that's not the case.
Here is what I am getting in Heroku's logs:
ActionView::Template::Error (undefined method 'rich_text_area_tag' for #<#<Class> Did you mean? rich_text_area)
<%= f.label :something, class:'label' %>
<%= f.rich_text_area :something %>
I found this online, and it was helpful for me:
https://github.com/JoeWoodward/spree_helper_issue
I am not sure if this is the correct way to do it, but it's a temporary workaround.
SOLUTION:
In application_controller.rb enter the following:
require 'action_text'
class ApplicationController < ActionController::Base
helper ActionText::Engine.helpers
...
end
Docs for helper: https://apidock.com/rails/AbstractController/Helpers/ClassMethods/helper
First of all make sure that you have this line in config/application.rb:
require 'rails/all'
If it's a rails 6 app action_text/engine should be loaded.
If it doesn't it's possible taht zeitwerk is not loaded. This happens when you have a rails 5.2 app that was updated to rails 6.
In the same file (config/application.rb) you have to change config.load_defaults to 6.0:
config.load_defaults 6.0
If you want to know what happens in the background take a look at this link on line 125.
To avoid cluttering the ApplicationController code, you can use an initializer:
i.e. add a new file config/initializers/action_text.rb:
ActiveSupport.on_load(:action_view) do
include ActionText::ContentHelper
include ActionText::TagHelper
end
Inspired by p8's comment in a closed Rails issue.
I'm having issues uploading images to my Rails app. I've downloaded ImageMagick and installed the Paperclip gem, but I keep getting the following error:
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
I have the following line in my development.rb file:
Paperclip.options[:command_path] = 'C:\Program Files (x86)\GnuWin32\bin'
And here's what my model looks like:
class Movie < ApplicationRecord
belongs_to :user
has_attached_file :image, styles: { medium: "400x600" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
I'm running Rails version 5.0.0 and on Windows 10. I've reinstalled ImageMagick several times, but still no luck. And I've tried solutions I found on this site and others, but I still keep getting the error. Can someone tell me what I'm doing wrong?
Rails 3.2.19, Ruby 1.9.3p547.
After adding a few gems to development (pry and its various dependencies), any validation in the form validates :some_field, some_default_rails_validator: true fails. Prior to bundling, all of these validations worked perfectly. Rolling back the Gemfile and re-bundling has no effect.
Other folks seem to have had issues with custom (or misspelled) validators, but the failures are all for Rails default validators. For example, running rails c gives me:
Users/MY_USER/.rvm/gems/ruby-1.9.3-p547#MY_APP/gems/activemodel-3.2.19/lib/active_model/validations/validates.rb:96 in 'rescue in block in validates': Unknown validator: 'PresenceValidator' (Argument Error)
which traces down to
from /Users/MY_USER/MY_APP/app/models/document.rb:4 in '<class:SomeModel>'
In the model file, I have:
validates :title, presence: true
PresenceValidator is a default Rails validator, and somehow a gem seems to have borked that. I'm not really sure what to do (besides nuking the app from space and reinitializing my development environment).
Can you try this? It worked for me.
validates :title, :presence=>true
I attempted to install the gmaps4rails gem.
I added gem 'gmaps4rails' to my Gemfile, and ran 'bundle install. It said that my bundle installed successfully. I can find "Using gmaps4rails (0.8.8)" with 'gem list'. I added the specified columns to my users table with rake db:migrate and added acts_as_gmappable and the gmaps4rails_address method to my User model.
Visiting pages that involve the user model gives me "undefined local variable or method 'acts_as_gmappable'"error.
Is there something that I am missing?
For greater context, the code I am using is from the Rails 3 Tutorial.
OS X 10.6.6
ruby 1.8.7
rails 3.0.7
passenger 3.0.7
Restarting the server should resolve the problem :)
I had the same issue : undefined local variable or method 'acts_as_gmappable'
I just restarted the server and it error went away.
I was using Mongoid and had the same trouble.
In which case, make sure your model includes:
include Gmaps4rails::ActsAsGmappable
acts_as_gmappable :position => :location
field :gmaps, :type => Boolean
field :location, :type => Array
def gmaps4rails_address
#describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki
"#{self.address}, #{self.city}, #{self.country}"
end
I think this may be helpful (similar issue):
rvm + rails3 + gmaps4rails - acts_as_gmappable
I'm trying to use oauth-plugin on a Rails application I'm developing, but I keep running into problems.
To make sure I'm not making any mistake, I started an application from scratch (using Rails 3.0.3). Here are the steps I followed:
Create da new rails application (rails.test)
Edited its Gemfile to include:
gem "oauth-plugin", ">=0.4.0.pre1"
gem "oauth", "0.4.4"
Generated oauth-consumer, by running script/rails g oauth_consumer
Edited oauth_consumers.rb to include my keys for Google integration:
:google=>{
:key=>"anonymous",
:secret=>"anonymous",
:scope=>"https://www.google.com/calendar/feeds/",
:options => {
:site => "http://www.google.com",
:request_token_path => "/accounts/OAuthGetRequestToken",
:access_token_path => "/accounts/OAuthGetAccessToken",
:authorize_path=> "/accounts/OAuthAuthorizeToken"
},
}
Edited routes.rb to add the route for oauth_consumer:
resources :oauth_consumers
Edited application_controller.rb to implement the logged_in? method as follows:
def logged_in?
true
end
Now when I access http://localhost:3000/oauth_consumers/google I get the following error:
uninitialized constant GoogleToken
Does anyone know what causes this error and how can I fix it? GoogleToken is a class that should have been auto generated by oauth-plugin, so I can't tell why I'm getting this uninitialized constant error.
The GoogleToken class doesn't get auto-generated unless you pass "google" to the generator like so:
script/rails g oauth_consumer google
or for rails 3:
rails g oauth_consumer google
Also check to ensure the relationship is set up in the user model like so:
has_one :google, :class_name => "GoogleToken", :dependent => :destroy
Did you remember to run bundle install from terminal after editing your Gemfile? Sounds like your Rails app doesn't know about these gems yet.
I have the same problem, i think a solution could be in this:
https://github.com/pelle/oauth-plugin/blob/master/lib/generators/oauth_consumer/USAGE
You need some sort of authentication like restful-authentication plugin, if you uncomment line 27..29 in your oauth_consumers_controller.rb file, you'll jump to next step!