mardi 28 février 2017

Different ways of rendering collection of records rails

I'm trying to render a collection of elements using partials in rails. I found 3 approaches that work, but not all for the same html document. These are the approaches that I found working, but I don t understand why some work in some files and some dont.

<%= j render @event_messages %>

<%= render @event_messages%>

<%= render partial: 'event_message', collection: @event_messages%>

Let's say I have the html file index.html.erb and the partial _event_message.html.erb. Now I'll go over the 3 approaches:

<%= render partial: 'event_message', collection: @event_messages%> this line works perfectly in this case and the partial gets rendered as many times as required.

<%= render @event_messages%> throws the missing template error, but in other .html.erb files I can use it to render partials with no problems.

<%= j render @event_messages %> works in a .js.erb file, but what does the 'j' stand for? I'm using this approach with an ajax callback and for some cases it works, but for others I get the missing template error.

Missing partial event_messages/_event_message with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in:

I tried my best to describe the issue. If you need more information, just let me know.

Aucun commentaire:

Enregistrer un commentaire