“模板丢失” Rails 3.1 尝试渲染时出错

发布于 2024-12-13 09:44:13 字数 1780 浏览 1 评论 0原文

我正在尝试将 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) %>

现在,我有一个简单的 js.erb 文件位于 app/views/posts/edit.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 模板,但不能编辑。感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趁微风不噪 2024-12-20 09:44:13

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

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

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 %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文