'Template is missing' error in Rails 3.1 when trying to render

1.4k views Asked by At

I'm trying to use Ajax with my CRUD. I'm following this tutorial.

I see this error:

Missing template posts/edit, application/edit with {:handlers=>[:erb, :builder, :coffee], :formats=>[:html], :locale=>[:en, :en]}. Searched in: * "/Users/wangstabill/Code/rails/ajax_on_rails/app/views"

when I try to click the edit link. It looks like:

<%= link_to 'Edit', edit_post_path(post, remote: true) %>

Now, I have a simple js.erb file located in app/views/posts/edit.js.erb. This file is not used for a response. Looking at the above error message, the :formats key's value is the array that only contains :html. If I create a edit.html.js, this works fine but not the edit.js.erb file.

This post recommends removing the old rails.js file, but I'm quite sure I never included it in this simple app (or where to find it if I did).

Here's what my controller class looks like:

class PostsController < ApplicationController
    before_filter :load, :except => [:destroy, :create]

    def load
        @posts = Post.all
    end

    def index
        @post  = Post.new
    end

    def edit
        @post = Post.find(params[:id])
    end

    def create
        @post = Post.new(params[:post])
        if @post.save
            flash[:notice] = 'Post was successfully created.'
            load
        end
    end

    def update
        @post = Post.find(params[:id])
        if @post.update_attributes(params[:post])
            flash[:notice] = 'Post was successfully updated.'
        end
    end

    def destroy
        @post = Post.find(params[:id])
        @post.destroy
        flash[:notice] = 'Successfully destroyed post.'
        load
    end
end

I don't understand why my create and destroy actions are successfully responding with js.erb templates, but not edit. Thanks for any help!

1

There are 1 answers

2
Dylan Markow On BEST ANSWER

I believe the remote option should be an option for link_to, not for edit_post_path. Try moving it outside the parentheses:

<%= link_to 'Edit', edit_post_path(post), remote: true %>