I am a newbie in ruby on rails it’s also my first ruby application. The
problem is database relation I am trying to make relation within two
tables and the same procedure are working properly in another relation
but here it’s showing an error.
The error is:
'undefined method `create_applied_jobs' for nil:NilClass'
Here are my codes:
applied_job.rb
class AppliedJob < ActiveRecord::Base
belongs_to :jobseekers
end
jobseeker.rb
class Jobseeker < ActiveRecord::Base
has_one :applied_job
end
view_details_controller.rb
def create
params.permit!
if @applied_job =
@current_user.create_applied_jobs(params[:applied_job])
flash[:notice] = “You have applied successfully”
render “viewDetails”
else
render “viewDetails”
flash[:warning] = “Please try agian”
end
end
sessions_helper.rb
def current_user
@current_user ||= Jobseeker.find_by(jobseeker_id:
session[:user_id])
end
viewDetails.html.erb
<%= form_for :applied_job, url: viewDetails_path(@applied_job),
action: :create, method: :post do |f| %>
- Preferred Joining Date
<%= f.text_field :preffered_joining_date, class:
‘field-long’, id: ‘datepicker’, placeholder: ‘Preferred Joining Date’%>
-
Expected Salary
<%= f.text_field :expected_salary, class: ‘form-control’,
placeholder: ‘Expected Salary’%>
-
<% end %>
On 23 January 2016 at 12:39, Mukto M. [email protected] wrote:
I am a newbie in ruby on rails it’s also my first ruby application. The
problem is database relation I am trying to make relation within two
tables and the same procedure are working properly in another relation
but here it’s showing an error.
The error is:
'undefined method `create_applied_jobs' for nil:NilClass'
If you look more carefully at the stack trace (in the server window or
in log/development.log) you should be able to see which line of your
code caused the problem.
jobseeker.rb
@current_user.create_applied_jobs(params[:applied_job])
I am assuming that it this line that has generated the error. The
error means that you have called create_applied_jobs on a variable
that is nil, so @current_user is nil. Perhaps you have not called
current_user, I don’t see where you are calling current_user to set it
up. In fact I am not sure why you don’t just have
current_user.create_applied_jobs(params[:applied_job])
though I have not looked in great detail at your code so there may be a
reason.
Colin
On Sat, Jan 23, 2016 at 7:39 AM, Mukto M. [email protected]
wrote:
'undefined method `create_applied_jobs' for nil:NilClass'
…
class Jobseeker < ActiveRecord::Base
has_one :applied_job
end
In addition to what Colin has said about setting up @current_user, you
may need to change the method call.
Since this is a has_one relationship rather than has_many, it might
need “create_applied_job” (i.e., singular) instead. (Though it seems
to me you probably want to change the has_one to has_many instead, to
allow seekers to apply to multiple jobs. Possibly even :through some
kind of JobApplication model, where you can store the date, status,
etc.)
-Dave
–
Dave A., consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.