Reputation: 14504
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?
Upvotes: 0
Views: 2919
Reputation: 6324
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 .
Upvotes: 3
Reputation: 7998
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
Upvotes: 1
Reputation: 1069
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.
Upvotes: 0