【问题标题】:undefined local variable in render partial from create.js.erb来自 create.js.erb 的部分渲染中未定义的局部变量
【发布时间】:2014-09-09 12:15:46
【问题描述】:

执行 create.js.erb 时,我的 firebug 响应出现错误。请看下文。

create.js.erb

<% if @document.new_record? %>
  alert("Failed to upload document: <%= j @document.errors.full_messages.join(', ').html_safe %>");
<% else %>
  $('#documents').append("<%= j render(:partial => "home/document", :document => @document) %>");
<% end %>

错误

NameError at /documents
=======================

> undefined local variable or method `document' for #<#<Class:0x08d11700f0>:0x007f8dcb9bfc20>

app/views/home/_document.html.erb, line 2
-----------------------------------------

``` ruby
    1   <tr class="document_details file">
>   2       <td class="file_name"><%= link_to document.name, download_url(document) %></td>
    3       <td class="file_size"><%= number_to_human_size(document.file_size, :precision => 2) %></td>
    4       <td class="file_last_updated"><%= document.file_updated_at %></td>
    5       <td class="actions">
    6           <div class="download">
    7               <%= link_to "Download", download_url(document) %>
```

【问题讨论】:

    标签: jquery ruby-on-rails ajax ruby-on-rails-4


    【解决方案1】:

    代替

    render(:partial => "home/document", :document => @document)
    

    你应该有:

    render(:partial => "home/document", :locals => {:document => @document})
    

    【讨论】:

    • render("home/document", :document =&gt; @document)
    • 我注意到我的帖子收到了反对票,我不想有没有用的帖子。如何改进我的帖子?
    • 仍在试图理解为什么这是一个糟糕的问题并被否决。如果有人能解释一下,我将不胜感激。
    猜你喜欢
    • 1970-01-01
    • 2016-11-21
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 2011-05-14
    • 1970-01-01
    • 2012-03-04
    相关资源
    最近更新 更多