weixin_33705053 2011-06-17 00:51 采纳率: 0%
浏览 24

Rails 3和Ajax的困惑

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>
  • 写回答

1条回答 默认 最新

  • weixin_33694620 2011-06-18 05:50
    关注

    Ok, I got it to work. All that was missing was format.js under the format.html in show in the games controller. Like this:

     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
               format.js
        end
      end
    

    I guess the strange thing is that games didn't need this to execute ajax...

    Cheers guys for the help

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料