errors
method is accessible on ActiveRecord::Base
objects i.e., in your case on instances of your model(Screen) which extend ActiveRecord::Base
.
Currently you receive error as NoMethodError undefined method errors for "@screens":String
because you are not invoking errors
method on a String
("@screens") instead of an instance of your model. "@screens"
is a String formed by concatenating "@" + params[:controller]
, it is NOT an instance variable @screens
.
So, you would need to pass an instance of your model class to your error_message_for
method and then call errors
on it.
For example:
def error_message_for(object, field, options = {:prepend_text => "#{'<i class="fa fa-bullhorn"></i> '}"}})
error_message = object.errors[field][0]
if error_message
raw "#{options[:prepend_text]} #{error_message}"
end
end
Call it as below:
<%= form_for(@screen) do |f| %>
<%= error_message_for(@screen,:field_name) %>
<% end %>