0

I am a newbie to Rails. I have a program which does basic AJAX functionality in RAILS. The link TEST when clicked, would execute the test path in the controller, which executes the test.js.erb. But I see that rendered in server logs, but dont see the alert message though.

index.html.erb:

<%= link_to 'TEST', test_path, :remote => true %> <br>

test action:

  def test
    respond_to do |format|
      format.js 
    end
  end

test js.erb:

"alert('Hello Rails');"

Server log:

Started GET "/test" for x.x.x.x at 2013-06-30 11:34:37 -0700 Processing by DefaultController#test as JS
Rendered Default/test.js.erb (0.3ms) Completed 200 OK in 2ms (Views: 2.1ms | ActiveRecord: 0.0ms)

If I change the code to render the js directly from the controller action, like shown below, , I see the alert message.

  def test
     render :js "alert('Hello Rails');"
  end

But I want the js to be executed in a separate js file. Can someone help me with what I am doing wrong, and how to have it executed as a part of test.js.erb ?

1
  • 1
    just remove double quotes from alert Commented Jun 30, 2013 at 19:33

1 Answer 1

1

Joe Half Face is right,

"alert('Hello Rails');"

should be:

alert('Hello Rails');

keeping this in quotes will take it as a string and so there is no javascript code to run, everything in the js file imagine it as beeing inside of javascript tags:

<script type="text/javascript">
  content of js.erb file
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

That doesn't need an answer, I suppose.
agree, the question itself could be removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.