In my rails application I have this three models User, Job and Job_application the association between the 3 is below
class Job < ActiveRecord::Base
has_many :job_applications
has_many :applicants, :through => :job_applications, :source => :user
accepts_nested_attributes_for :job_applications
class JobApplication < ActiveRecord::Base
belongs_to :user
belongs_to :job
class User < ActiveRecord::Base
has_many :job_applications
has_many :applied_jobs, :through => :job_applications, :source => :job
In my Job controller I have this method so that user can apply to job
def apply
@job = Job.find_by_slug(params[:id])
@job_application = JobApplication.new
@job_application.user_id = current_user.id
@job_application.job_id = @job.id
if @job_application.save
JobMailer.somebody_applied(@job).deliver
redirect_to job_url(@job)
else
render :action => "show"
end
end
and my Job view page I have this code for the apply method
<button type="button" id="control_gen_5" data-toggle="modal" data-target="#myModal">
<span>Envoyer un candidature</span>
</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close close-this" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Apply to <%= @job.job_title %></h4>
</div>
<p><%= current_user.full_name%></p>
<%= form_for @job, :url => apply_job_path(@job), :method => :post, :html => { :multipart => true } do |f| %>
<%= f.fields_for :job_applications, @job.job_applications.build do |s| %>
<%= s.hidden_field :user_id %>
<%= s.hidden_field :job_id %>
<div class="modal-body">
<p>
<div class="form-inputs">
<%= s.text_area :cover_letter, :input_html => { :rows => 5, :class => 'span12', :value => cookies[:cover_letter] }, :label => false %>
</div>
</p>
</div>
<% end %>
<div class="modal-footer">
<%= f.submit 'Postuler', :class => 'btn btn-info', :name => nil %>
</div>
<% end %>
</div>
</div>
</div>
I have also this code so I can view who has applied for a job and their cover letter
<ul>
<% @job.applicants.each do |appl| %>
<li>
<%= appl.full_name %>
<%= appl.cover_letter %>
</li>
<% end %>
</ul>
But I'm getting this error
undefined method `cover_letter' for #
Aucun commentaire:
Enregistrer un commentaire