Rails 通过 Javascript 提交部分错误
我的控制器中有以下代码:
文件: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试将局部变量传递给部分“snapshot_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:
Try the code above and see if it works. I just change the snapshot and comments to @snapshot and @comments.
snapshot 在控制器中设置为局部变量,因此模板看不到它。将其设置为实例变量:
然后在模板中:
snapshot is set as a local variable in controller, so the template doesn't see it. Set it as an instance variable:
then in the template: