sgp667
sgp667

Reputation: 1875

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"

here is _todo.html.haml partial:

%div 
    %h3= todo.title
    %div

Upvotes: 1

Views: 313

Answers (1)

user229044
user229044

Reputation: 239311

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'

Upvotes: 2

Related Questions