Rails 通过 Javascript 提交部分错误

发布于 2024-10-28 17:30:16 字数 1327 浏览 5 评论 0原文

我的控制器中有以下代码:

文件:app/controllers/photos_controller.rb

def show_snapshot_comments
    snapshot = Snapshot.find(params[:id])
    @photo = snapshot.photo
    comments = snapshot.comments.paginate :page => params[:page]
    @snapshot_comment = snapshot.comments.new
      respond_to do |format|
        format.js
      end
   end

JavaScript (jQuery): 文件:app/views/photos/show_snapshot_comments.js.erb

$j('#info').append("<%= escape_javascript(render :partial  => "snapshot_comments",
        :locals   => {:snapshot => snapshot, :comments => comments}) %>" );

show_snapshot_copmments 调用自: app/views/photos/show.html.erb

它似乎不起作用。

我的错误:

渲染照片/show_snapshot_comments

ActionView::TemplateError (undefined local variable or method `snapshot' for #<ActionView::Base:0x2aaaaea473c8>) on line #2 of app/views/photos/show_snapshot_comments.js.erb:
1: $j('#info').append("<%= escape_javascript(render :partial  => "snapshot_comments",
2:         :locals   => {:snapshot => snapshot, :comments => comments}) %>" );

    app/views/photos/show_snapshot_comments.js.erb:2
    app/controllers/photos_controller.rb:227:in `show_snapshot_comments'

有什么想法可能会发生什么吗?

I have the following code in my controller:

File: app/controllers/photos_controller.rb

def show_snapshot_comments
    snapshot = Snapshot.find(params[:id])
    @photo = snapshot.photo
    comments = snapshot.comments.paginate :page => params[:page]
    @snapshot_comment = snapshot.comments.new
      respond_to do |format|
        format.js
      end
   end

The JavaScript (jQuery):
File: app/views/photos/show_snapshot_comments.js.erb

$j('#info').append("<%= escape_javascript(render :partial  => "snapshot_comments",
        :locals   => {:snapshot => snapshot, :comments => comments}) %>" );

show_snapshot_copmments is called from:
app/views/photos/show.html.erb

It doesn't appear to be working.

My error:

Rendering photos/show_snapshot_comments

ActionView::TemplateError (undefined local variable or method `snapshot' for #<ActionView::Base:0x2aaaaea473c8>) on line #2 of app/views/photos/show_snapshot_comments.js.erb:
1: $j('#info').append("<%= escape_javascript(render :partial  => "snapshot_comments",
2:         :locals   => {:snapshot => snapshot, :comments => comments}) %>" );

    app/views/photos/show_snapshot_comments.js.erb:2
    app/controllers/photos_controller.rb:227:in `show_snapshot_comments'

Any ideas what may be going on?

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

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

发布评论

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

评论(2

残龙傲雪 2024-11-04 17:30:16

您正在尝试将局部变量传递给部分“snapshot_comments”。但是,这两个局部变量、快照和注释未在当前作用域(视图中)中定义。

如果您想将变量从控制器传递给视图,您必须这样做以下:

def show_snapshot_comments
    @snapshot = Snapshot.find(params[:id])
    @photo = snapshot.photo
    @comments = snapshot.comments.paginate :page => params[:page]
    @snapshot_comment = snapshot.comments.new
      respond_to do |format|
        format.js
      end
   end

$j('#info').append("<%= escape_javascript(render :partial  => "snapshot_comments",
        :locals   => {:snapshot => @snapshot, :comments => @comments}) %>" );

尝试上面的代码,看看它是否有效。我只是将快照和注释更改为@snapshot和@comments。

You are trying to pass the local variables to the partial 'snapshot_comments". However this two local variables, snapshot and comments are not defined in current scope (in the view).

If you want to pass variables to views from controller you have to do the following:

def show_snapshot_comments
    @snapshot = Snapshot.find(params[:id])
    @photo = snapshot.photo
    @comments = snapshot.comments.paginate :page => params[:page]
    @snapshot_comment = snapshot.comments.new
      respond_to do |format|
        format.js
      end
   end

$j('#info').append("<%= escape_javascript(render :partial  => "snapshot_comments",
        :locals   => {:snapshot => @snapshot, :comments => @comments}) %>" );

Try the code above and see if it works. I just change the snapshot and comments to @snapshot and @comments.

忆梦 2024-11-04 17:30:16

snapshot 在控制器中设置为局部变量,因此模板看不到它。将其设置为实例变量:

@snapshot = Snapshot.find(params[:id])

然后在模板中:

:locals   => {:snapshot => @snapshot

snapshot is set as a local variable in controller, so the template doesn't see it. Set it as an instance variable:

@snapshot = Snapshot.find(params[:id])

then in the template:

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