undefined method `[]' for nil:NilClass - ruby-on-rails

def creation
(1..params[:book_detail][:no_of_copies].to_i).each do |i|
logger.info "nnnnnnnnnnn#{i}"
#book_details= BookDetail.new(params[:book_detail])
#book_details.save
end
And the Error is
undefined method []' for nil:NilClass
app/controllers/book_details_controller.rb:16:increation'
Is anybody can tell what is the problem?

Error you are getting is because params[:book_detail] is nil and you are calling [:no_of_copies] on it i.e. nil.So it is giving following error
undefined method []' for nil:NilClass
So you need to check first if params[:book_detail] is present or not like following
(1..params[:book_detail][:no_of_copies].to_i).each do |i|
logger.info "nnnnnnnnnnn#{i}"
#book_details= BookDetail.new(params[:book_detail])
#book_details.save
end if params[:book_detail] && params[:book_detail][:no_of_copies]

In addition is Salil's answer, you can use fetch
params.fetch(:book_detail, {})[:no_of_copies]
which will return nil if params[:book_detail] is nil. (1..0).to_a returns an empty array so you can rewrite your code using the following
copies = (params.fetch(:book_detail, {})[:no_of_copies] || 0).to_i
(1..copies).each do |i|
logger.info "nnnnnnnnnnn#{i}"
#book_details= BookDetail.new(params[:book_detail])
#book_details.save
end

Related

NoMethodError (undefined method `last' for true:TrueClass):

I got NoMethodError (undefined method `last' for true:TrueClass):
from app/controllers/posts_controller.rb:71:in `uploads'
Here is the script
def uploads
#post = current_user.posts.friendly.find(params[:id])
a = #post.images.attach(params[:file])
render json: {url: url_for(a.last)}
end
Not sure what am I doing wrong. Any advice?
I imagine the 'attach' method is returning 'true' or 'false' as to whether it was successful or not rather than assigning the file to the variable.
Personally I'd remove the variable 'a', then just use:
url_for(#post.images.last)

Rails/Grape Rspec undefined method `shopping_element_relations` for nil:NilClass

I want to test a method that update promo_code to used if shopping_process is finalized. Additional it should create a pdf using ShoppingProcessDocumentCreatorFetcher
Here is my method which I want to test
def finalize_shopping_process(form)
if finalize_process == true
shopping_process.campaign_code.update(state: 'used')
document_creator_class = ShoppingProcessDocumentCreatorFetcher.new(shopping_process).call
document_creator_class.new(shopping_process).call
end
Success(form)
end
and the specs:
describe 'campain code' do
subject(:process_update) do
described_class.new(
shopping_process: shopping_process,
form: form,
finalize_process: true,
).call
end
it 'updates state of assigned campain code' do
updated_shopping_process = process_update.value!
expect(updated_shopping_process.campaign_code.state).to eq('used')
end
end
At the end I've got an error
NoMethodError:
undefined method `shopping_element_relations' for nil:NilClass
The weird thing is that if I remove this lines from the tested method:
document_creator_class = ShoppingProcessDocumentCreatorFetcher.new(shopping_process).call
document_creator_class.new(shopping_process).call
Specs will pas. I've no clue where I'm wrong.
Edit:
all error below:
ShoppingProcesses::Update.call campain code updates state of assigned campain code
Failure/Error: return false unless parent_group.shopping_element_relations.hiding.any?
NoMethodError:
undefined method `shopping_element_relations' for nil:NilClass
def finalize_shopping_process(form)
if finalize_process == true
shopping_process.campaign_code.update(state: 'used')
document_creator_class = ShoppingProcessDocumentCreatorFetcher.new(shopping_process).call
document_creator_class.new(shopping_process).call
end
Success(form)
end
In the above function document_creator_class this is initialized to the call method and not the class as intended.
Replace the line
document_creator_class = ShoppingProcessDocumentCreatorFetcher.new(shopping_process).call
with
document_creator_class = ShoppingProcessDocumentCreatorFetcher
and then try.
I hope this resolves your problem.

undefined method `>' for nil:NilClass Ruby on Rails (NoMethodError)

So for some odd reason I keep getting this error:
undefined method `>' for nil:NilClass
Rails.root: C:/Sites/booking_saloniser - Calendar
Application Trace | Framework Trace | Full Trace
app/models/user.rb:11:in `block in upcoming_appointments'
app/models/user.rb:11:in `upcoming_appointments'
app/controllers/appointments_controller.rb:8:in `index'
appointment controller
def index
#upcoming_appointments = current_user.upcoming_appointments
end
user model
def upcoming_appointments
appointments.order(appointment_time: :desc).select { |a| a.appointment_time > (DateTime.now) }
end
Any chance someone could help me get around this error?
It appears one or more of your appointment records has a nil appointment_time.
If this is not expected, you should add a validation on the model that it's not null, and fix your existing data.
Otherwise (and this works as quick fix), you can chain a query to not include records with a nil appointment time:
def upcoming_appointments
appointments
.order(appointment_time: :desc)
.where.not(appointment_time: nil)
.select { |a| a.appointment_time > (DateTime.now) }
end
If where.not isn't available (if you're using an older version of rails), you can also use where("appointment_time != null")

undefined method `+' for nil:NilClass - Values are not null

I am trying to create a method that loops through some objects and if a certain attribute is true, adds to the cost of the lesson to the money received, but keep getting the error undefined method `+' for nil:NilClass.
Here is the piece of code (the line producing the error start #activity.update_attribute):
def show
#activity = Activity.find(params[:id])
#participants = Participant.all.where(:activity_id => #activity.id).map(&:user_id).uniq
#all_participants = Participant.all.where(:activity_id => #activity.id)
#all_participants.each do |a_participant|
if a_participant.paid
#activity.update_attribute(:money_received, #activity.money_received + #activity.cost)
end
end
#users = Array.new
#participants.each do |participant|
#users.push(User.find(participant))
end
end
And here is a screenshot from my database to show that neither is a null value:
Here is the error message that I get when running the application:
Any help would be greatly appreciated
Many thanks

NoMethodError - undefined method for nil:NilClass:

I am Rails noob and I am trying to unsderstand this simple JSON parsing code from a tutorial. Why do I get the nil:NilClass Error? What is a NilClass?
Thanks!
app.put '/users/update' do
params = JSON.parse(request.body.read)
reqUserID = params[:id]
requestUser = Models::Persistence::User.find_by_id(reqUserID)
content_type "application/json"
puts "Hello"
puts requestUser.username
if (requestUser)
status 401
return
end
Null, in Ruby is called nil and like everything else, nil is also an object. An object of NilClass.
You get this error if you try to call a method on a nil object.
So in this case, requestUser is probably nil

Resources