Reputation: 1017
I have a form where I am using an if statement to either pull the user name from @current_user.name (adauth) if the form is new, and @complaint.user.name if the form is being edited.
The validations were working but now, if I try to submit an empty form or a form where an associated field value is nil, then I get an error of
NoMethodError in Complaints#create
Showing C:/Users/cmendla/RubymineProjects/internal_complaints/app/views/complaints/_form.html.erb where line #27 raised:
undefined method `name' for nil:NilClass
Trace of template inclusion: app/views/complaints/new.html.erb
Rails.root: C:/Users/cmendla/RubymineProjects/internal_complaints
Application Trace | Framework Trace | Full Trace
app/views/complaints/_form.html.erb:27:in `block in _app_views_complaints__form_html_erb__838826376_68516916'
app/views/complaints/_form.html.erb:1:in `_app_views_complaints__form_html_erb__838826376_68516916'
app/views/complaints/new.html.erb:3:in `_app_views_complaints_new_html_erb__529765002_56812308'
app/controllers/complaints_controller.rb:150:in `block (2 levels) in create'
app/controllers/complaints_controller.rb:135:in `create'
_form.html.erb for complaints (where i have the conditional code)
<strong>Originator:</strong><br>
<!-- Below will show the user name. If it is not a new complaint, we pull the user name from the complaint table
Otherwise, we get the name of the currently logged in user. -->
<% if action_name != "new" then %>
<%= @user.name %>
<% else %>
<%= @current_user.name %>
<%= f.hidden_field(:user_id, :value => @current_user.id) %>
<% end %>
</td>
Line 27 is <%= @user.name %>
I think that what is happening is that the reload after a failed validation is no longer an action of 'new'. How can I fix this?
validates_presence_of :status
validates_presence_of :sales_order
validates_presence_of :product_name
validates_presence_of :coater_roll_number
validates_presence_of :coater_work_order
validates_presence_of :slitter_disposition
validates_presence_of :length
validates_presence_of :measure
validates_presence_of :disposition
validates_presence_of :reason_for_complaint
validates_presence_of :customer
I think what is happening is that when the form is reloaded after failing a validation, it no longer is a controller action of 'new'.
Upvotes: 0
Views: 71
Reputation: 1017
I found that when a validation fails, the action shifts from NEW to CREATE. The code below will show the @current_user.name for NEW and CREATE actions and the @user.name from the table for edit actions.
<% if action_name != "new" && action_name != "create" then %>
<%= @user.name %>
<% else %>
<%= @current_user.name %>
<%= f.hidden_field(:user_id, :value => @current_user.id) %>
<% end %>
Upvotes: 0
Reputation: 34318
undefined method `name' for nil:NilClass
This error simply means that your @user
is nil
and that's why it's failing on <%= @user.name %>
. Make sure your @user
has the value and is not not nil
. Then, it will work.
You can also try using try like this: <%= @user.try(:name) %>
. That way, your program will not crash when @user
is nil. But, the best way is to find out why @user
is nil and fix that.
Upvotes: 1