undefined method `original_filename' for #<CarrierWave::Storage::AWSFile: - ruby

Can't seem to identify why I would be getting a undefined method original_filename.
Maybe because when upgrading ruby 3.1.2,with ruby 2.7.5 can still use this method.
Uploader
class DocumentUploader < CarrierWave::Uploader::Base
CarrierWave::SanitizedFile.sanitize_regexp = Settings.regex.upload_filename
def store_dir
"uploads/event_documents/#{mounted_as}/#{model.id}"
end
end
Model
class EventDocumentFile < ActiveRecord::Base
mount_uploader :document_file, DocumentUploader
validate :document_file_name
private
def document_file_name
return if !document_file.file.present? || document_file.file.original_filename.length <= 70
errors.add :document_file, I18n.t("event_documents.invalid_file_name")
end
end
Error detail
undefined method `original_filename' for #<CarrierWave::Storage::AWSFile:0x00007fe60dfc38e8 #uploader=#<DocumentUploader:0x00007fe60dfbdc18 #model=#<EventDocumentFile
return if !document_file.file.present? || document_file.file.original_filename.length <= 70
^^^^^^^^^^^^^^^^^^
Anyone know what's wrong with my code? thanks!

Related

undefined method '<=' for nil:NilClass Ruby

I have a Ruby model Book.rb that has the following:
class Book < Library
field :name
field :checked_out, default: false
field :checked_in, default: true
def checked_out?
checked_out && !checked_in
end
def checked_in?
checked_in && !checked_out
end
Then, in my index.haml file, have the following:
-#books.each do |book|
haml :"books/list_all" locals: {book: book}
Finally, in my list_all.haml file, I have:
.h4
#{book.name}
- if "#{book.checked_in?}"
Book is checked in
- else
Book is checked out
Update: book_routes.rb
get '/' do
haml :'books/index'
end
However, I get a undefined method <= for nil:NilClass error. How can I fix this?

Rails ArgumentError on Job - wrong number of arguments - how to debug?

I’m trying to create a Job within my specs with the following, but keep getting an ArgumentError:-
MyJob.new.perform_now(user_id: #current_user.id, building_ids: [building.id])
module ActionPlan
class MyJob < ApplicationJob
queue_as :low
def perform(user_id:, building_ids:, **_args)
#user = User.find(user_id)
#buildings = ActionPlan::Reminder.where(user_id: user_id)
#buildings.map(&method(:create_alerts))
end
I keep getting wrong number of arguments (given 1, expected 0) (ArgumentError). Where am I going wrong? The perform method takes 2 arguments, right? How would I debug this?
Try this:
module ActionPlan
class MyJob < ApplicationJob
queue_as :low
def initialize(user_id:, building_ids:, **_args)
#buildings = ActionPlan::Reminder.where(user_id: user_id)
end
def perform
buildings.map(&method(:create_alerts))
end
private
attr_accessor :buildings
end
end
That should allow you to call:
MyJob.new(user_id: #current_user.id, building_ids: [building.id]).perform

Rails 4 strong parameters ActiveModel::ForbiddenAttributesError

For some reason in my current controller I am getting ActiveModel::ForbiddenAttributesError even though I believe I am using strong parameters just fine. Albeit I am using permit! for the time being to permit all model attributes. See code below, what am I missing
class HeuristicsController < ApplicationController
def index
#heuristics = Heuristic.order(:name).page params[:page]
#heuristic = Heuristic.new
end
def create
#heuristic = Heuristic.new(params[:heuristic])
if #heuristic.save
redirect_to action: 'index', :flash => {:success => "New heuristic created!" }
else
render 'new'
end
end
def new
#title = "Heuristic"
#heuristic = Heuristic.new
end
private
def heuristic_params
params.require(:heuristic).permit!
end
end
i think you did not fully understand the way that strong-params work...
you have a method
def heuristic_params
params.require(:heuristic).permit!
end
and you are not using it
Heuristic.new(params[:heuristic])

rails 3.1.0 belongs_to ActiveResource no longer working

I am upgrading from rails 3.0.7 to 3.1 and am having trouble getting my tests to pass. The problem occurs when I try to use a stubbed active resource object in a factory.
#employee.rb
class Employee < ActiveResource::Base; end
#task.rb
class Task < ActiveRecord::Base
belongs_to :employee
end
#factories.rb
Factory.define :employee do |e|
e.name "name"
end
Factory.define :task do |t|
t.employee { Factory.stub(:employee) }
end
On the console and in the spec stubbing an employee works. Referencing the stubbed employee object in a new task gives the following error.
Factory.create( :task, :employee => Factory.stub(:employee) )
NoMethodError:
undefined method `[]' for #<Employee:0x007fc06b1c7798>
EDIT
This is not a factory girl issue. I get the same error if I do the following in the console.
Task.new( :employee => Employee.first )
It must be related to how belongs_to maps the id column.
I didn't like the monkey patch so I created a module that I will include at initialization to extend ActiveRecord
module BelongsToActiveResource
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def ar_belongs_to( name, options = {} )
class_eval %(
def #{name}
##{name} ||= #{options[:class_name] || name.to_s.classify }.find( #{options[:foreign_key] || name.to_s + "_id" } )
end
def #{name}=(obj)
##{name} ||= obj
self.#{ options[:foreign_key] || name.to_s + "_id" } = ##{name}.#{ options[:primary_key ] || 'id' }
end
)
end
end
end
ActiveRecord::Base.class_eval { include BelongsToActiveResource }
Then in each ActiveRecord model would look like:
#task.rb
class Task < ActiveRecord::Base
ar_belongs_to :employee
end
Hope this helps someone

How to extend DataMapper::Resource with custom method

I have following code:
module DataMapper
module Resource
##page_size = 25
attr_accessor :current_page
attr_accessor :next_page
attr_accessor :prev_page
def first_page?
#prev_page
end
def last_page?
#next_page
end
def self.paginate(page)
if(page && page.to_i > 0)
#current_page = page.to_i - 1
else
#current_page = 0
end
entites = self.all(:offset => #current_page * ##page_size, :limit => ##page_size + 1)
if #current_page > 0
#prev_page = #current_page
end
if entites.size == ##page_size + 1
entites.pop
#next_page = (#current_page || 1) + 2
end
entites
end
end
end
Then I have call of #paginate:
#photos = Photo.paginate(params[:page])
And getting following error:
application error
NoMethodError at /dashboard/photos/
undefined method `paginate' for Photo:Class
In Active record this concept works fine for me... I'am using JRuby for notice. What I'am doing wrong?
Andrew,
You can think of DataMapper::Resource as the instance (a row) and of DataMapper::Model as the class (a table). Now to alter the default capabilities at either the resource or the model level, you can either append inclusions or extensions to your model.
First you will need to wrap your #paginate method in a module. I've also added a probably useless #page method to show how to append to a resource in case you ever need to.
module Pagination
module ClassMethods
def paginate(page)
# ...
end
end
module InstanceMethods
def page
# ...
end
end
end
In your case, you want #paginate to be available on the model, so you would do:
DataMapper::Model.append_extensions(Pagination::ClassMethods)
If you find yourself in need of adding default capabilities to every resource, do:
DataMapper::Model.append_inclusions(Pagination::InstanceMethods)
Hope that helps!

Resources