In short, I don't understand ajax with rails. I did this tutorial a little while ago and I can get it to work. No problems. The problems seem arise when I have more than one model.
For example, if I have a model "cats", I use the tutorial to implement some ajax, works nicely - each time I add a new cat to the list, it appears at the top without reloading the page. Then I add a "dogs" model, and I render the _dog.html.erb file in the cats index and call on it with something like @dogs.each do |dog| etc.
Now I need to implement the ajax for the dogs, but do I put the ajax files into create.js.erb in the dogs folder or in the cats create.js.html folder? I've tried both, no success. The first model, in this example cats, will continue to work, but the others don't. I know that its half working because the page doesn't reload when I hit enter, but nothing is generated either. I've checked the id's, all correct.
Is there anything else I should be looking for? My logic says that if i'm in the cats index and rendering something from _dog.html.erb, I should put all the create ajax in the cats create.js.html folder….
Help is more than appreciated.
Also, just as a small second question, I read another tutorial and it advised me to delete everything in the public/javascripts folder except "application.js", which in my case is blank. I haven't because ajax is working, just not for the other models. Should I take the tutorials advise?
Thanks again.
Update
This is the code from "show" in the cats controller.
@dog = Dog.new
respond_to do |format|
format.html
end
Update 2
Ok, so "Cats" is really "Games", and "Dogs" is really "Features". I used the cats and dogs examples because I thought in theory it would be easier, sorry if it made it more confusing :).
The code - the ajax for creating games in the games index is working, but the ajax for creating features in the games show is not. The games show is where I want to create new features using ajax. the features are created but only viewable after a page reload.
class GamesController < ApplicationController
def index
@games = Game.paginate(:page => params[:page], :per_page => 10)
@game = Game.new
respond_to do |format|
format.html
end
end
def show
@user = User.find(params[:id])
@gameid = Game.find(params[:id])
@features = Feature.paginate(:page => params[:page], :per_page => 8, :conditions => {:game_id => @gameid})
@feature = Feature.new
respond_to do |format|
format.html
end
end
def create
@game = Game.new(params[:game])
respond_to do |format|
if @game.save
format.html { redirect_to(games_url,
:notice => 'game was successfully created.') }
format.js
else
format.html { redirect_to(games_url) }
end
end
end
def destroy
@game = Game.find(params[:id])
@game.destroy
respond_to do |format|
format.html { redirect_to(games_url) }
format.js
end
end
end
class FeaturesController < ApplicationController
def index
@features = Feature.all
@feature = Feature.new
respond_to do |format|
format.html
end
end
def show
end
def create
@feature = Feature.new(params[:feature])
respond_to do |format|
if @feature.save
format.html { redirect_to(features_url,
:notice => 'feature was successfully created.') }
format.js
else
format.html { redirect_to(features_url) }
end
end
end
def destroy
@feature = Feature.find(params[:id])
@feature.destroy
respond_to do |format|
format.html { redirect_to(features_url) }
format.js
end
end
end
(features/create.js.erb)
$('#features').prepend('<%= escape_javascript(render(@feature)) %>');
$('#features > li:first').effect('highlight', {}, 3000);
$('#features_form > form')[0].reset();
(games/create.js.erb)
$('#games').prepend('<%= escape_javascript(render(@game)) %>');
$('#games > li:first').effect('highlight', {}, 3000);
$('#game_form > form')[0].reset();
(games/index)
<h1>Games</h1>
<table>
<tr>
<td class="main">
<ul id="games">
<% @games.each do |game| %>
<%= render 'game', :game => game %>
<% end %>
</ul>
<% if signed_in? %>
<div id="game_form">
<%= render 'form' %>
</div>
<% else %>
<% end %>
</td>
<td class="sidebar">
</td>
</tr>
</table>
(games/show)
<h1>Features</h1>
<table>
<tr>
<td class="main">
<h2>Features</h2>
<div id="features_form">
<%= render 'features/form' %>
</div>
<ul id="features" style="margin:0;padding:0;">
<% @features.each do |feature| %>
<%= render 'features/feature', :feature => feature %>
<% end %>
<%= will_paginate @features, :class => 'pagination' %>
</ul>
</td>
<td class="sidebar">
</td>
</tr>
</table>