来自create.js.erb的render partial中的undefined局部变量

时间:2014-09-09 12:15:47

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

当我的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) %>
```

1 个答案:

答案 0 :(得分:1)

而不是

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

你应该:

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