Rails无法看到.rjs文件

时间:2012-06-20 19:35:37

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

我正在尝试学习如何在我的rails应用程序中使用AJAX,所以我决定从简单的东西开始。我有一个博客应用程序,用户可以在任何博客文章投票。这是我的posts#vote代码:

posts_controller.rb

class PostsController < ApplicationController
  (...)
  def vote
    post = Post.find(params[:id])

    if current_user.voted_on?(post)
      current_user.unvote_for(post)
    else
      current_user.vote_for(post)
    end

    respond_to do |format|
      format.html { redirect_to post_path(post) }
      format.js
    end
  end
end

这是我的posts#view的链接代码:

view.html.erb

<%= link_to "Vote", vote_post_path(post.id), :remote => true %>

现在,如果我点击我的Vote链接,posts#vote操作有效并投票,但是我收到了错误:

  

ActionView :: MissingTemplate(缺少模板帖子/投票,   申请/投票{:handlers =&gt; [:haml,:coffee,:erb,:builder],   :locale =&gt; [:en] ,: formats =&gt; [:js,:html]}。

我的vote.rjs文件夹中有(空)views/posts文件,但由于某些原因,rails无法看到它。根据错误,rails搜索的唯一文件扩展名为.haml.coffee.erb.builder。该列表中是否还应该有.rjs个扩展名?提前谢谢。

2 个答案:

答案 0 :(得分:2)

您的文件应该被称为vote.js.erb。 Rails不使用.rjs扩展名。

答案 1 :(得分:1)

.rjs扩展程序最初用于RailsPrototype JS库。

使用Rails 3.1,默认库切换到JQuery。

如果你想在你的Rails项目中使用.rjs,那就意味着使用Prototype而不是JQuery。宝石就是prototype-rails