Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering a partial within a loop #5

Open
lagartoflojo opened this issue Oct 30, 2011 · 4 comments
Open

Rendering a partial within a loop #5

lagartoflojo opened this issue Oct 30, 2011 · 4 comments

Comments

@lagartoflojo
Copy link

Hi,
I'm trying to render a partial multiple times (for the index action) and it's only returning the last item:

Partial _child.json.jsonify

json.name child.name

File index.json.jsonify

@children.each do |child|
  json.ingest!(render partial: child)
end

Actual result:

{"name":"Pedrito"}

Expected result:

[{"name":"Horacio"},{"name":"Pepito"},{"name":"Pedrito"}]

I also tried:

@children.each do |child|
  json << json.ingest!(render partial: child)
end

But the result was:

{"name":"Pedrito","#<Jsonify::JsonObject:0x00000002333ef0>":null,"[#<Jsonify::JsonPair:0x00000002320df0 @key=\"name\", @value=\"Pepito\">]":null,"[#<Jsonify::JsonPair:0x00000002306658 @key=\"name\", @value=\"Pedrito\">]":null}
@bsiggelkow
Copy link
Owner

I'll take a look today at your problem ... until I get that fixed, you could just skip the partial ...

@children.each do |child|
json.name child.name
end

On Oct 29, 2011, at 11:23 PM, lagartoflojo wrote:

Hi,
I'm trying to render a partial multiple times (for the index action) and it's only returning the last item:

Partial _child.json.jsonify

json.name child.name

File index.json.jsonify

@children.each do |child|
 json.ingest!(render partial: child)
end

Actual result:

{"name":"Pedrito"}

Expected result:

[{"name":"Horacio"},{"name":"Pepito"},{"name":"Pedrito"}]

I also tried:

@children.each do |child|
 json << json.ingest!(render partial: child)
end

But the result was:

{"name":"Pedrito","#<Jsonify::JsonObject:0x00000002333ef0>":null,"[#<Jsonify::JsonPair:0x00000002320df0 @key=\"name\", @value=\"Pepito\">]":null,"[#<Jsonify::JsonPair:0x00000002306658 @key=\"name\", @value=\"Pedrito\">]":null}

Reply to this email directly or view it on GitHub:
#5

@lagartoflojo
Copy link
Author

Thank you!
Yes, that's how I generated the "expected result".

@sporto
Copy link

sporto commented Aug 7, 2012

Hi
I am looking for the same thing. Any solutions for this?

@sporto
Copy link

sporto commented Aug 7, 2012

I have this working like this:

@children.each do | child |
    json << JSON.parse(render 'child', child: child)
end

Not very efficient because Jsonify is converting the partial into a string and JSON.parse is converting it back into a hash, but it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants