dimanche 7 juin 2015

Unable to run LOOPS within Javascript Ruby on Rails View

Executive Summary

  1. Question 1: What is the value of providing local variables to a render, when they can access the instance objects in the controller?

  2. Issue: I can NOT run a ruby loop within my _error.js.erb file. The goal of this loop is to provide DIRECT output to a $("#error_explanation") id regarding all of the

  3. I was able to run the various other jquery commands as illustrated below.

  4. I am able to run straight JQUERY commands leveraging ruby.

Dear Friends,

In my create controller, within it is the following:

format.js { render "_error.js.erb", locals: {post: @post, errors: @post.errors.messages}  }

In my VIEW _error.js.erb, i have 3 JQUERY queries, two of them work, but the third does not work.

This Works. $("#error_explanation").append('<%= j pluralize(post.errors.count, "error") %> prohibited this secret from being saved: <p>');

This Works. $("#error_explanation").append('<%= post.errors.full_messages_for(:title) %>');


This Does NOT Work. $("#error_explanation").append('
    <% post.errors.messages.each do |error| %>
     <%= j error %>
    <% end %>

I can only assume that it is because of my loop? Can I not perform loops in my javascript (js.erb) code?

Resulted in this Error

ActionView::Template::Error (undefined method `gsub' for ["Title can't be blank", "Subject can't be blank"]:Array):
     5: <% end %>
     6: 
     7: <% if post.errors.full_messages[1] %>
     8: $("#error_explanation").append('<%= j(post.errors.full_messages.each {|n| n}) %>');
     9: <% end %>
    10: $("#error_explanation").css("color", "red");

Aucun commentaire:

Enregistrer un commentaire