English 中文(简体)
铁路通过 Java路部分提交
原标题:Rails Error submitting partial via Javascript

我的管制人员有以下守则:

文件:控制器/控制器/控制器/控制器。

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

似乎没有工作。

我的错误:

发送照片/how_snapshot_com

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 

可能出现的任何想法?

最佳回答

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).

如果你想通过变量向控制者提出意见,你必须做以下工作:

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。

问题回答

缩略语被定为控制器的一个当地变量,因此模板没有看到。 视其为案例变量:

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

然后在模板中:

:locals   => {:snapshot => @snapshot




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签