I have 2 models: Applicants and Comments. Applicants has_many
Comments and Comments belongs_to Applicant.
If I add validation to my Applicants model (for example,
"validates_presence_of :first_name") and edit the applicants page, I
get a Nil Object error if I remove the first_name before saving. In
this case I would expect to see the "there was 1 error that
prevented..." error message. I do get this message during Applicant
creation.
The strange part is that the error I do get points to my comments
display. Everything works as expected. Why would it behave this way?
##### ERROR MESSAGE: Action Controller: Exception Caught
#################
Showing app/views/applicants/_comments.rhtml where line #42 raised:
You have a nil object when you didn''t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Extracted source (around line #42):
39: <th>Comment</th>
40: <th>File</th>
41: </tr>
42: <% for comment in @comments %>
43: <tr>
44: <td> <%= comment.created_at.to_s(:long) %> </td>
45: <td> <%=h comment.comment_type %> </td>
#### CONTROLLER: applicants_controller.rb ##########################
def edit
@applicant = Applicant.find(params[:id])
@comments = @applicant.applicant_comments.find(:all)
end
def update
@applicant = Applicant.find(params[:id])
if @applicant.update_attributes(params[:applicant])
flash[:notice] = ''Applicant was successfully updated.''
redirect_to :action => ''list''
else
render :action => ''edit''
end
end
def comment
Applicant.find(params[:id]).applicant_comments.create(params[:applicant_comment])
flash[:notice] = ''comment was successfully created.''
redirect_to :action => ''edit'', :id => params[:id]
end
#### MODEL: ApplicantComment.rb ##############
class ApplicantComment < ActiveRecord::Base
belongs_to :applicant
has_attachment :storage => :file_system
end
#### MODEL: Applicant.rb ######################
class Applicant < ActiveRecord::Base
has_one :contract
has_many :applicant_comments
validates_presence_of :first_name, :last_name, :email, :phone_1, :address_1,
:city, :state, :zip, :resume
def full_name
[last_name, first_name].join('', '')
end
def full_name=(name)
split = name.split('' '', 2)
self.first_name = split.last
self.last_name = split.first
end
end
#### VIEW: applicants/edit.rhtml #########################
<h1>Edit applicant</h1>
<% form_tag :action => ''update'', :id => @applicant do
%>
<%= render :partial => ''form'' %>
<div class="button">
<%= image_submit_tag("save.png") %>
</div>
<% end %>
<h1>Post Comments</h1>
<table>
<tr>
<th>Comment</th>
<th>Created</th>
</tr>
<% for comment in @comments %>
<tr>
<td><%= comment.body %></td>
<td><%= comment.created_at %></td>
</tr>
<% end %>
</table>
<% form_for(
:applicant_comment,
:url => {:controller => ''applicants'' , :action =>
''comment'', :id =>
@applicant},
:html => { :multipart => true }
) do |form| %>
<%= form.hidden_field("comment_type", :value => "Hiring
Process") %>
<p><label
for="comment_comment">Comment:</label><br />
<%= form.text_area :body, :rows => 3, :cols => 40 %></p>
<div id="file_link">
<%= link_to_function(("Attach File"),
"Element.remove(''file_link'');Element.show(''file'')")
%>
</div>
<div id="file" style="display:none;">
<label for="uploaded_data">Upload a file:</label><br
/>
<%= form.file_field :uploaded_data %>
</div>
<div class="button">
<%= image_submit_tag("save.png") %>
</div>
<% end %>
<%= link_to ''Back'', :action => ''list''
%>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---