Search code examples
ruby-on-railsruby-on-rails-3rjs

Rails can't see .rjs file


I'm trying to learn how to use AJAX in my rails apps so i've decided to start with something simple. I have a blog app on which user can vote on any blog post. Here is my code for 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

and here is a link code for my posts#view:

view.html.erb

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

And now, if i'll click my Vote link, posts#vote action works and vote is casted, however i'm getting an error:

ActionView::MissingTemplate (Missing template posts/vote, application/vote with {:handlers=>[:haml, :coffee, :erb, :builder], :locale=>[:en], :formats=>[:js, :html]}.

I have (empty) vote.rjs file in my views/posts folder but for some reason rails can't see it. According to error, the only file extensions that rails is searchng for are .haml, .coffee, .erb and .builder. Shouldn't there be also a .rjs extension on that list? Thanks in advance.


Solution

  • Your file should be called vote.js.erb. Rails doesn't use a .rjs extension.