Unable to run LOOPS within Javascript Ruby on Rails View -


executive summary

  1. question 1: value of providing local variables render, when can access instance objects in controller?

  2. issue: can not run ruby loop within _error.js.erb file. goal of loop provide direct output $("#error_explanation") id regarding of

  3. i able run various other jquery commands illustrated below.

  4. i able run straight jquery commands leveraging ruby.

dear friends,

in create controller, within following:

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

in view _error.js.erb, have 3 jquery queries, 2 of them work, third not work.

this works. $("#error_explanation").append('<%= j pluralize(post.errors.count, "error") %> prohibited secret being saved: <p>');  works. $("#error_explanation").append('<%= post.errors.full_messages_for(:title) %>');   not work. $("#error_explanation").append('     <% post.errors.messages.each |error| %>      <%= j error %>     <% end %> 

i can assume because of loop? can not perform loops in javascript (js.erb) code?

resulted in error

actionview::template::error (undefined method `gsub' ["title can't blank", "subject can't 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"); 

try with,

$("#error_explanation").append("<%= j(post.errors.full_messages.each{|msg| msg }) %>") 

or

 <%- str = "" %>  <% if post.errors %>     <% post.errors.full_messages.each |error| %>       <% str+= error %>     <% end %>  <% end %>    $("#error_explanation").append("<%= j str %>"); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -