ruby on rails - undefined method `[]' for nil:NilClass -
def creation
(1..params[:book_detail][:no_of_copies].to_i).each |i| logger.info "nnnnnnnnnnn#{i}" @book_details= bookdetail.new(params[:book_detail]) @book_details.save end
and error undefined method []' nil:nilclass app/controllers/book_details_controller.rb:16:in
creation'
is can tell problem?
error getting because params[:book_detail]
nil
, calling [:no_of_copies]
on i.e. nil.so giving following error
undefined method []' nil:nilclass
so need check first if params[:book_detail]
present or not following
(1..params[:book_detail][:no_of_copies].to_i).each |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]
Comments
Post a Comment