user3743457
user3743457

Reputation: 161

Pass variable to partial(rails 4)

I have a partial _album.html.erb and it has a variable "album" I have a collection @albums . For each in the collection I want to render the partial. But I don't use the option "collection:" because I want to put each one in different position. Here is my code:

<% @albums.each do | f | %>
     ..........
    <td>
           <%= render partial: "album", locals: {"album" f} %>
      .........
<% end %>

This code returns a SyntaxError. I don't know why this happens.

Upvotes: 1

Views: 188

Answers (3)

brcebn
brcebn

Reputation: 1722

I had this problem and I tried with locals: {..} but it didn't work.

You can try this that worked for me:

 <%= render partial: "form", zone: @zone %>

Upvotes: 1

Richard Peck
Richard Peck

Reputation: 76774

Locals

Further to zishe's correct answer, you'll need to look up on how to set the syntax for partial local variables:

<%= render partial: "form", locals: {zone: @zone} %>

Rails basically sends a hash of key: value pairs, which means the syntax has to be like this:

locals: {key: value, "key" => value}

You have to remember Rails is basically just a collection of files & classes, which means anything you do has to conform to the ways in which Ruby allows you to assign & pass data, a hash being one of the main ways

Upvotes: 1

Alexander Kireyev
Alexander Kireyev

Reputation: 10825

You haven't any divider between "album" and f, so try locals: {album: f} or :album => f

Upvotes: 5

Related Questions