rails method undefined job_apps - ruby-on-rails

I get and error undefined method 'job_apps' (tried with single form 'job_app' too)
def accept
#job_apps = job_apps.find(params[:id])
#job_apps.update_attribute(status: :accept)
end
def refuse
#job_apps = job_apps.find(params[:id])
#job_apps.update_attribute(:status,2)
end
routes:
get 'accept' => 'job_apps#accept'
post 'accept' => 'job_apps#accept'
Tried:
def accept
#job_apps = #user.job_app
#job_apps.update_attribute(status: :accept)
end
I had similar trouble for calling the job_app in the same view (to see job_app.status as it is table joined with users on user_id) but on stack overflow someone helped me with this (view file):
<th><%= user.job_app.status %></th>

undefined method 'job_apps'
This #job_apps = job_apps.find(params[:id]) is wrong. You need to use the classname when writing a ActiveRecord query. Th below should work
#job_apps = JobApp.find(params[:id])

Related

NoMethodError (undefined method `each' for nil:NilClass):

class Api::SurveyAnswersController < ApplicationController
def create
# #survey_answer = SurveyAnswer.new(survey_answer_params)
survey_answers = []
survey_id = params[:survey_id]
params[:questions].each do |q|
answer = {survey_id: survey_id, option_ids: [], question_id: q[:id],
title: q[:answer]}
if q[:options].present?
selected_options = q[:answer].split(',')
selected_options.each do |selected_option|
q[:options].each do |option|
if option[:title]== selected_option
answer[:option_ids] << option[:id]
#<todo add break when in this condition
end
end
end
survey_answers << answer
end
end
puts survey_answers
# #survey_answers = SurveyAnswer.create(survey_answers)
if SurveyAnswer.create(survey_answers)
render json: survey_answers
end
end
end
I have a survey model which has some questions. Each question contains answers. When I try to hit post request through postman to insert answers, it gives 505 internal server error with message "undefined method each for nil:nilclass". Can anybody tell what the problem is?
You are trying to run the .each loop an empty object.
Make sure that both
params[:questions]
and
q[:options]
are not empty (not equal to nil).
NoMethodError sometimes sounds very unrepresentative, especially if you're just starting off with Ruby.
Try to browse Stackoverflow next time, because this has been answered here.

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

Undefined new_record? Ruby Rails

I am trying to check if the domain host url record exist in the domain table before creating it again, but I am getting this error:
undefined method `new_record?' for #<Domain::ActiveRecord_Relation:0x007f320ed8af80>
The class GetMeta is a service object that is getting initialize when a user enter a URL in a form and click submit. I am taking the URL from the form and calling MetaInspector with it to get more meta informations.
The first part (if) new_record method is working perfectly fine, but is creating duplicate values in the domain table. I tried to create a conditional logic, but I am having this bug that I don't know how to fixed.
class GetMeta
include ActiveModel::Model
def initialize(url)
#url = url
end
def new_record
page = MetaInspector.new(#url)
#domain = Domain.where(:host => page.host)
if new_record?
Domain.create! do |url|
url.root_url = page.root_url
url.scheme = page.scheme
url.host = page.host
url.links.build(url: page.url, title: page.best_title, description: page.description)
end
else
Link.create! do |link|
link.url = page.url
link.title = page.best_title
link.description = page.description
end
end
end
private
def new_record?
#domain.new_record?
end
end
The problem is described by an error. Let's see:
undefined method `new_record?' for
#<Domain::ActiveRecord_Relation:0x007f320ed8af80>
The problem is the line
#domain = Domain.where(:host => page.host)
This returns an ActiveRecord relation instead of single record.
You should take a .first or .last.
#domain = Domain.where(:host => page.host).last
That's the fix, but let's see how can we improve the code.
We can use method exists? which is defined in ActiveRecord Relation (docs: http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-exists-3F)
if Domain.exists?(host: page.host)
Link.create! do |link|
link.url = page.url
link.title = page.best_title
link.description = page.description
end
else
Domain.create! do |url|
url.root_url = page.root_url
url.scheme = page.scheme
url.host = page.host
url.links.build(url: page.url, title: page.best_title, description: page.description)
end
end
This way we don't need instance variable #domain and helper method new_record?
You are calling an instance method from within an instance method. So you need to specify which instance you are referring to. You need to use 'self'. So instead of just calling 'new_record?', try calling self.new_record?

rails link_to controller method undefined

I want to create link_to that updates a value status (accept - 1 ,refuse - 2 cause 0 is in progress) so i had an idea to call methods in job_app
def accept
#job_apps = job_apps.find(params[:id])
#job_apps.update_attribute(:status,1)
end
def refuse
#job_apps = job_apps.find(params[:id])
#job_apps.update_attribute(:status,2)
end
routes:
get 'accept' => 'job_apps#accept'
post 'accept' => 'job_apps#accept'
It claims error that job_apps are undefined (treid job_app also).
Tried:
def accept
#job_apps = #user.job_app
#job_apps.update_attribute(:status,1)
end
I had similar trouble for calling the job_app in the same view (to see job_app.status as it is table joined with users on user_id) but on stack overflow someone helped me with this (view file):
<th><%= user.job_app.status %></th>
I completely dont have idea how use it to link_to (if it is possible ofc)
Please see this topic.
it seems that you are using an enum on the status column. You can't use raw values (the integer part of the enum value) with enums unless you skip object instantiation (using update_all, or update_columns, for example).If you instantiate the object, you must use the enum value (value is :accept, while raw value is 1). In accept, you need to update the object as such
def accept
#job_apps = job_apps.find(params[:id])
#job_apps.update_attribute(status: :accept)
end
The solution is:
#job_apps = JobApp.find(params[:id])
Full post here

Kaminari error when collection is smaller than per(x)

I am running the Kaminari gem for my pagination.
Controller
def dashboard
#projects = Project.find_by_user_id(current_user)
if #projects.size > 10
#projects.page(params[:page]).per(10)
end
end
Dashboard view
= paginate #projects, :theme => 'twitter-bootstrap-3', :remote => true
In my case, the #projects is sometimes only 1 record or even zero records. When it is nil, I get an error on the params[:page] being nil.
So this works
def dashboard
#projects = Project.page(params[:page]).per(10)
end
This gets error undefined method 'page' for #<Project:0x007f8cac5f14b0>
def dashboard
#projects = Project.find_by_user_id(current_user).page(params[:page]).per(10)
end
I think it is because the #projects is only a couple of records which is less than the 10 specified in .per
I tried adding a #projects.count or #projects.size but I get the error undefined method 'size' for #<Project:0x007f8c996865f0>
def dashboard
#projects = Project.find_by_user_id(current_user)
if #projects.size > 10
#projects.page(params[:page]).per(10)
end
end
What the hell am I doing wrong!? haha
I am guessing I can fix this in the first instance instead of trying to fix the second or third options. Any help would be greatly appreciated.
The issue is Project.find_by_user_id(current_user) returns an Array, not an ActiveRecord::Relation
You should do something like:
current_user.projects.page(params[:page]).per(10)
If your relationships are correctly setup.
Or:
Project.where(user_id: current_user.id).page(params[:page]).per(10)

Resources