1

我正在尝试将 Ajax 与我的 CRUD 一起使用。我正在关注本教程

我看到这个错误:

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"

当我尝试单击编辑链接时。看起来像:

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

现在,我在 app/views/posts/edit.js.erb 中有一个简单的 js.erb 文件。此文件不用于响应。查看上面的错误信息,:formats 键的值是只包含 :html 的数组。如果我创建了一个 edit.html.js,这可以正常工作,但不是 edit.js.erb 文件。

这篇文章建议删除旧的 rails.js 文件,但我很确定我从未将它包含在这个简单的应用程序中(或者如果我这样做了在哪里可以找到它)。

这是我的控制器类的样子:

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

我不明白为什么我的创建和销毁操作使用 js.erb 模板成功响应,但没有编辑。谢谢你的帮助!

4

1 回答 1

1

我相信这个remote选项应该是一个选项link_to,而不是edit_post_path。尝试将其移到括号外:

<%= link_to 'Edit', edit_post_path(post), remote: true %>
于 2011-11-03T16:02:48.483 回答