Rails3:如何进行ajax-y更新?比如,更改页面内容而不必刷新页面?

时间:2011-01-27 18:08:17

标签: jquery ruby-on-rails ruby-on-rails-3

<li><%= link_to "Yes", { :controller => 'application', :action => 'start_game', :human_is_first => true  }, :remote => true  %></li>

在我的控制器中:

  def start_game
    respond_to do |format|
      format.js
    end
  end
在start_game.js.erb中

$('.choose_options').fadeOut(1000);

我的erb,调用start_game:

<div class="welcome">
  Hello!

  <div class="choose_turn">
    Would you like to go first?

    <ul class="choose_options">
      <li><%= link_to "Yes", { :controller => 'application', :action => 'start_game', :human_is_first => true  }, :remote => true  %></li>
      <li><%= link_to "No", { :controller => 'application', :action => 'start_game', :human_is_first => true  }, :remote => false  %></li>
    </ul>
  </div>
</div>

但是当我点击链接时,页面会重定向

Template is missing

Missing template application/start_game with {:locale=>[:en, :en], :handlers=>[:builder, :rjs, :rhtml, :rxml, :erb], :formats=>[:html]} in view paths "~/Documents/Aptana Studio 3 Workspace/tic-tac-toe-ai/tic-tac-toe-ai/app/views" 

非常感谢任何帮助。

(我正在使用jQuery 1.4.3)

1 个答案:

答案 0 :(得分:1)

您可以尝试使用request.xhr而不是使用respond_to吗?像这样:

if request.xhr?
  render 'start_game.js.erb', :layout => false
end

科迪