I have been using vanilla javascript and im just rendering a simple alert message but it renders the JS code as plane string in view. Here is my code
def index
render :js => "alert('Hello Rails');"
end
It renders the same string “alert(‘Hello Rails’);” for me in view instead of a alert message.
Im in Rails 3 and as Rails guide the code looks ok.
Any answers would be appreciated. Thanks
Compare two following snippets:
First one generates alert message, second just shows the the string. Browsers do not execute JS code unless it is wrapped with
<script>tag. jQuery ajax calls in the @Erez answer also wrap response with<script>internally.So if you access your action directly try to wrap it with
<script>, or, better, use javascript_tag helper (http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#method-i-javascript_tag)