Using Controller Set Variables In Conditional .js.erb File
I have a controller with an action that is being hit remotely, after which my controller_action.js.erb file is run. Within my controller's action, I am setting a variable @successf
Solution 1:
try this
<%- if @successful %>
alert('Blabla');
<%- else %>
alert('Blabal');
<%- end %>
Solution 2:
I speak HAML usually, so this might not be exactly correct:
if(<%= @successful %>) {
// do stuff
} else {
// do other stuff
}
In HAML, it would be:
:javascript
if(#{@successful}) {
// do stuff
} else {
// do other stuff
}
Alternatively, you could do this:
(ERB)
<%- if @successful%>// JS code if true<%-else%>// JS code if false<%-end%>(HAML)
- if@successful
:javascript
// JS code if true
- else
:javascript
// JS code if falseThe first option will always render all your JS code on the client side, the only difference being if true or false is in the if clause.
The second option will conditionally only render the JS code for the true or false case.
Post a Comment for "Using Controller Set Variables In Conditional .js.erb File"