Pasar una variable a un parcial, rails 3?
Tengo un bucle como tal:
<% @posts.each do |post| %>
<% render middle %>
<% end %>
Luego, en mi parcial central, ¿cómo puedo acceder a la publicación actual?
139
Author: Toon Krijthe, 2011-01-15
4 answers
Prueba esto:
<% @posts.each do |post| %>
<%= render 'middle', :post => post %>
<% end %>
Así tendrás una variable local post
disponible dentro del parcial.
236
Author: polarblau,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-26 17:05:59
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-26 17:05:59
Darle a la parcial como una variable local
<%= render :partial => 'middle', :locals => { :post => post } %>
Por supuesto, rails también tiene un atajo para renderizar colecciones:
<%= render :partial => 'post', :collection => @posts %>
En este caso llamará al post parcial para cada post con una variable local'post'
Incluso puedes renderizar una plantilla espaciadora entre cada post:
<%= render :partial => 'post', :collection => @posts, :spacer_template => 'post_divider' %>
124
Author: Stefaan Colman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-16 21:46:14
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-16 21:46:14
<% @posts.each do |post| %>
<% render middle, :post => post %>
<% end %>
Ahora puede acceder a post como la variable local post
en el parcial
14
Author: Felix Andersen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-15 16:14:55
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-15 16:14:55
Sustitúyase <%= render middle %>
por <%= render middle, :post => post %>
. Luego, en su parcial middle
, puede acceder a la variable post
.
10
Author: sevenseacat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-10 04:26:33
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-10 04:26:33