I'm handling the error messages using the gem toastr and a function in javascript that works with the gem.
One of the parameters is resource
and in the partial form _comments it does not have it, and is defined as follows:
<%= form_for [*commentable, Comment.new], local: true do |f| %>
<%= render 'shared/devisemes' %> //Esto genera error
<div class="form-group">
<%= f.text_area :body, autofocus: true, :rows => 2, style: 'width:100%;', placeholder: "Add a comment", class: "form-control" %><br/>
</div>
<%= f.submit "Commentate", class: "btn btn-primary pull-right" %>
<% end %>
Logically, how the function uses resources
generates an error because it does not recognize resource
:
**undefined local variable or method 'resource' for**
Is there a way to redefine the following line:
<%= form_for [*commentable, Comment.new], local: true do |f| %>
in this way to make it work?
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
This is the code that is invoked in views:
_devisemes.html.erb
<% unless resource.errors.empty? %>
<script type="text/javascript">
<% resource.errors.full_messages.each do |value| %>
toastr.error('<%= value %>')
<% end %>
</script>
<% end %>
<% unless resource.errors.empty? %>
<script type="text/javascript">
<% resource.errors.full_messages.each do |value| %>
toastr.error('<%= value %>')
<% end %>
</script>
<% end %>
Example of invocation from a form:
<div class="row">
<div class="col-md-4 col-md-offset-4">
<h2 class="text-center">Sign up</h2>
<br/>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render 'shared/devisemes' %> //Acá se invoca
<div class="form-group">
<%= f.text_field :fullname, autofocus: true, placeholder: "Full Name", class: "form-control" %>
</div>
<div class="form-group">
<%= f.email_field :email, autofocus: true, placeholder: "Email", class: "form-control" %>
</div>
<div class="form-group">
<%= f.password_field :password, autocomplete: "off", placeholder: "Password", class: "form-control" %>
</div>
<div class="actions">
<%= f.submit "Sign up", class: "btn btn-primary" %>
</div>
<% end %>