0

In my index.js.erb I have:

alert('asdasd'); 

In my controller I have:

def index @konkurrencer = Konkurrencer.find(:all).paginate(:page => params[:page], :per_page => 2) respond_to do |format| format.html format.js{ render :text => "alert('hello')" } end end 

I have included jquery and jquery_ujs. Why can´t I see any alert message?

3 Answers 3

3

If you are trying to access the index method through the browser it won't work because it's not an ajax request . If you want to try and learn AJAX make a link to another method that has the remote=true attribute , that will send an AJAX request . Here is how your controller should look like

def index end def show end 

And the views:

index.html.erb

<%= link_to "Click here" , "/controller/show" , remote: true %> 

show.js.erb

alert("Hello ,AJAX!");

Then start playing with more complex stuff .

Sign up to request clarification or add additional context in comments.

Comments

1

You don't want to include render :text => "alert('hello')" if you want the view to render the index.js.erb file. Rails can only render one thing at a time, so its either or.

Plus, I'd have to see the AJAX request that is hitting the index action to tel why it doesn't seem to work.

If you visit index.js in your browser, and you remove the render code as I said above, you should see the alert box.

Joe

7 Comments

Joe it is only a test to escape some javascript. What is then the purpose of it, when it is not possible to render, some JS. I have removed the render, I can still not see any error message.
Rails Beginner, I've been trying to help you all day, but your questions just aren't that specific.
It is a simple thing I want to do. Sorry if I don´t are specific enough. I want to render the js that is in the index.js.erb. the alert message. I want to be able to keep my javascript separate from html code. But is should not be static js. I am just trying to get the js rendered.
Are you making AJAX requests? Are you using jquery_ujs?
I am using jquery_ujs. I will just try to reinstall it. Think it is the problem.
|
0

If you want to render JS from controller, you need to surround it with tag otherwise browser will render it as a string.

 render :text => "<script>alert('hello')</script>" 

But generally speaking it's not recommended, you should use js template for this sort of renders.

1 Comment

I want the javascript in the index.js.erb to get rendered

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.