Rendering a partial inside a loop
up vote
-2
down vote
favorite
I'm trying to render the following partial:
<% @accepted.each do |question| %>
<div class="questions-container__content">
<div class="questions-container__accepted-content">
...
</div>
<%= render 'question_buttons', collection: @accepted %>
</div>
<% end %>
with _question_buttons.html.erb
:
<div class="links-container__button-group" id="link-buttons">
<%= link_to "View submission", coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<%= link_to "Edit", edit_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% if !question.accepted? %>
<%= link_to "Activate" , activate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% else %>
<%= link_to "Deactivate" , deactivate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% end %>
<% if current_user.admin? %>
<%= link_to (question.rejected ? "Restore" : "Reject"), reject_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% end %>
</div>
I get the following error:
undefined local variable or method `question' for #<#<Class:0x00007fece6998d08>:0x00007fed02072bb8>
What am I doing wrong here?
ruby-on-rails
add a comment |
up vote
-2
down vote
favorite
I'm trying to render the following partial:
<% @accepted.each do |question| %>
<div class="questions-container__content">
<div class="questions-container__accepted-content">
...
</div>
<%= render 'question_buttons', collection: @accepted %>
</div>
<% end %>
with _question_buttons.html.erb
:
<div class="links-container__button-group" id="link-buttons">
<%= link_to "View submission", coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<%= link_to "Edit", edit_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% if !question.accepted? %>
<%= link_to "Activate" , activate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% else %>
<%= link_to "Deactivate" , deactivate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% end %>
<% if current_user.admin? %>
<%= link_to (question.rejected ? "Restore" : "Reject"), reject_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% end %>
</div>
I get the following error:
undefined local variable or method `question' for #<#<Class:0x00007fece6998d08>:0x00007fed02072bb8>
What am I doing wrong here?
ruby-on-rails
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I'm trying to render the following partial:
<% @accepted.each do |question| %>
<div class="questions-container__content">
<div class="questions-container__accepted-content">
...
</div>
<%= render 'question_buttons', collection: @accepted %>
</div>
<% end %>
with _question_buttons.html.erb
:
<div class="links-container__button-group" id="link-buttons">
<%= link_to "View submission", coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<%= link_to "Edit", edit_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% if !question.accepted? %>
<%= link_to "Activate" , activate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% else %>
<%= link_to "Deactivate" , deactivate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% end %>
<% if current_user.admin? %>
<%= link_to (question.rejected ? "Restore" : "Reject"), reject_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% end %>
</div>
I get the following error:
undefined local variable or method `question' for #<#<Class:0x00007fece6998d08>:0x00007fed02072bb8>
What am I doing wrong here?
ruby-on-rails
I'm trying to render the following partial:
<% @accepted.each do |question| %>
<div class="questions-container__content">
<div class="questions-container__accepted-content">
...
</div>
<%= render 'question_buttons', collection: @accepted %>
</div>
<% end %>
with _question_buttons.html.erb
:
<div class="links-container__button-group" id="link-buttons">
<%= link_to "View submission", coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<%= link_to "Edit", edit_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% if !question.accepted? %>
<%= link_to "Activate" , activate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% else %>
<%= link_to "Deactivate" , deactivate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% end %>
<% if current_user.admin? %>
<%= link_to (question.rejected ? "Restore" : "Reject"), reject_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% end %>
</div>
I get the following error:
undefined local variable or method `question' for #<#<Class:0x00007fece6998d08>:0x00007fed02072bb8>
What am I doing wrong here?
ruby-on-rails
ruby-on-rails
edited Nov 9 at 8:41
sawa
128k27192296
128k27192296
asked Nov 9 at 0:05
mxvx
1028
1028
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
I believe the issue is that you need to pass the question
variable from each
loop in the parent view through to the partial using locals
which allows the partial to access it.
<%= render 'question_buttons', locals: { question: question } %>
does this work on your version of rails?<%= render 'question_buttons', question: question %>
?
– vee
Nov 9 at 0:50
i don't see the use ofcollection
variable either. why do you have it there?
– vee
Nov 9 at 0:51
add a comment |
up vote
0
down vote
First:
When rendering a collection, each item of the collection is passed to the partial as a local variable with the same name as partial itself. It means that for this call:
<%= render 'question_buttons', collection: @accepted %>
the partial question_buttons
will be called for each item of @accepted
array; that item will be available inside partial as question_buttons
.
If you want to use another name for the item, for example question
, you need to call it as:
<%= render 'question_buttons', collection: @accepted, as: :question %>
Another option -- just rename the partial to question
:
<%= render 'question', collection: @accepted %>
Second:
In your code snippet rendering collection is called on each iteration of the loop over @accepted
elements. If @accepted
has 8 elements, for example, the partial will be rendered 8 times for each of these elements, ie 8 * 8 = 64 times in total. I suspect that it's not what you want to achieve. Your code looks like question_buttons
partial needs to be rendered for each element of @accepted
only once. In this case using collection
param has no sense here. Just pass a local variable question
into the partial:
<%= render 'question_buttons', question: question %>
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
I believe the issue is that you need to pass the question
variable from each
loop in the parent view through to the partial using locals
which allows the partial to access it.
<%= render 'question_buttons', locals: { question: question } %>
does this work on your version of rails?<%= render 'question_buttons', question: question %>
?
– vee
Nov 9 at 0:50
i don't see the use ofcollection
variable either. why do you have it there?
– vee
Nov 9 at 0:51
add a comment |
up vote
1
down vote
I believe the issue is that you need to pass the question
variable from each
loop in the parent view through to the partial using locals
which allows the partial to access it.
<%= render 'question_buttons', locals: { question: question } %>
does this work on your version of rails?<%= render 'question_buttons', question: question %>
?
– vee
Nov 9 at 0:50
i don't see the use ofcollection
variable either. why do you have it there?
– vee
Nov 9 at 0:51
add a comment |
up vote
1
down vote
up vote
1
down vote
I believe the issue is that you need to pass the question
variable from each
loop in the parent view through to the partial using locals
which allows the partial to access it.
<%= render 'question_buttons', locals: { question: question } %>
I believe the issue is that you need to pass the question
variable from each
loop in the parent view through to the partial using locals
which allows the partial to access it.
<%= render 'question_buttons', locals: { question: question } %>
edited Nov 9 at 9:46
Praveen Dhawan
7718
7718
answered Nov 9 at 0:15
Katrpilar
113
113
does this work on your version of rails?<%= render 'question_buttons', question: question %>
?
– vee
Nov 9 at 0:50
i don't see the use ofcollection
variable either. why do you have it there?
– vee
Nov 9 at 0:51
add a comment |
does this work on your version of rails?<%= render 'question_buttons', question: question %>
?
– vee
Nov 9 at 0:50
i don't see the use ofcollection
variable either. why do you have it there?
– vee
Nov 9 at 0:51
does this work on your version of rails?
<%= render 'question_buttons', question: question %>
?– vee
Nov 9 at 0:50
does this work on your version of rails?
<%= render 'question_buttons', question: question %>
?– vee
Nov 9 at 0:50
i don't see the use of
collection
variable either. why do you have it there?– vee
Nov 9 at 0:51
i don't see the use of
collection
variable either. why do you have it there?– vee
Nov 9 at 0:51
add a comment |
up vote
0
down vote
First:
When rendering a collection, each item of the collection is passed to the partial as a local variable with the same name as partial itself. It means that for this call:
<%= render 'question_buttons', collection: @accepted %>
the partial question_buttons
will be called for each item of @accepted
array; that item will be available inside partial as question_buttons
.
If you want to use another name for the item, for example question
, you need to call it as:
<%= render 'question_buttons', collection: @accepted, as: :question %>
Another option -- just rename the partial to question
:
<%= render 'question', collection: @accepted %>
Second:
In your code snippet rendering collection is called on each iteration of the loop over @accepted
elements. If @accepted
has 8 elements, for example, the partial will be rendered 8 times for each of these elements, ie 8 * 8 = 64 times in total. I suspect that it's not what you want to achieve. Your code looks like question_buttons
partial needs to be rendered for each element of @accepted
only once. In this case using collection
param has no sense here. Just pass a local variable question
into the partial:
<%= render 'question_buttons', question: question %>
add a comment |
up vote
0
down vote
First:
When rendering a collection, each item of the collection is passed to the partial as a local variable with the same name as partial itself. It means that for this call:
<%= render 'question_buttons', collection: @accepted %>
the partial question_buttons
will be called for each item of @accepted
array; that item will be available inside partial as question_buttons
.
If you want to use another name for the item, for example question
, you need to call it as:
<%= render 'question_buttons', collection: @accepted, as: :question %>
Another option -- just rename the partial to question
:
<%= render 'question', collection: @accepted %>
Second:
In your code snippet rendering collection is called on each iteration of the loop over @accepted
elements. If @accepted
has 8 elements, for example, the partial will be rendered 8 times for each of these elements, ie 8 * 8 = 64 times in total. I suspect that it's not what you want to achieve. Your code looks like question_buttons
partial needs to be rendered for each element of @accepted
only once. In this case using collection
param has no sense here. Just pass a local variable question
into the partial:
<%= render 'question_buttons', question: question %>
add a comment |
up vote
0
down vote
up vote
0
down vote
First:
When rendering a collection, each item of the collection is passed to the partial as a local variable with the same name as partial itself. It means that for this call:
<%= render 'question_buttons', collection: @accepted %>
the partial question_buttons
will be called for each item of @accepted
array; that item will be available inside partial as question_buttons
.
If you want to use another name for the item, for example question
, you need to call it as:
<%= render 'question_buttons', collection: @accepted, as: :question %>
Another option -- just rename the partial to question
:
<%= render 'question', collection: @accepted %>
Second:
In your code snippet rendering collection is called on each iteration of the loop over @accepted
elements. If @accepted
has 8 elements, for example, the partial will be rendered 8 times for each of these elements, ie 8 * 8 = 64 times in total. I suspect that it's not what you want to achieve. Your code looks like question_buttons
partial needs to be rendered for each element of @accepted
only once. In this case using collection
param has no sense here. Just pass a local variable question
into the partial:
<%= render 'question_buttons', question: question %>
First:
When rendering a collection, each item of the collection is passed to the partial as a local variable with the same name as partial itself. It means that for this call:
<%= render 'question_buttons', collection: @accepted %>
the partial question_buttons
will be called for each item of @accepted
array; that item will be available inside partial as question_buttons
.
If you want to use another name for the item, for example question
, you need to call it as:
<%= render 'question_buttons', collection: @accepted, as: :question %>
Another option -- just rename the partial to question
:
<%= render 'question', collection: @accepted %>
Second:
In your code snippet rendering collection is called on each iteration of the loop over @accepted
elements. If @accepted
has 8 elements, for example, the partial will be rendered 8 times for each of these elements, ie 8 * 8 = 64 times in total. I suspect that it's not what you want to achieve. Your code looks like question_buttons
partial needs to be rendered for each element of @accepted
only once. In this case using collection
param has no sense here. Just pass a local variable question
into the partial:
<%= render 'question_buttons', question: question %>
answered Nov 9 at 5:39
Ilya Konyukhov
2,183618
2,183618
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217982%2frendering-a-partial-inside-a-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown