简体   繁体   中英

Rails 4 missing local variables for partials

Per Rails Guides if you render partial using a collection: , the partial will have rendered record accessible with name of the partial. However my partials are throwing error:

Here is the Template: = render "todo", collection: @ntodos, as: 'todo' || "none" = render "todo", collection: @ntodos, as: 'todo' || "none"

here is _todo.html.haml partial:

%div 
    %h3= todo.title
    %div

tldr; you need to render partial: 'todo', collection: ... .


That isn't how you render a collection partial, that's how you render a partial and pass locals to it.

Rendering partial has two different an incompatible syntaxes.

The first way is render(options_hash) . It looks like this:

render partial: 'template', locals: { var1: value1 }

The second way (which you've used) cannot render collections. It uses a render(template_name, locals_hash) signature, something like this:

render 'template', var1: value1

Your invocation, which uses the second form, rendered todo and passed two locals named collection and as to it.

If you want to render a collection, you need to explicitly use the first form, with render partial: 'todo', collection: @ntodos, as: 'todo'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM