AnthonyGalli.com AnthonyGalli.com - 4 months ago 7
Ruby Question

How to Show Nested Attribute for Corresponding User?

How can I only show the

:accept
checkmark for the
dueler
who is the
current_user
?

Right now there is a
check_mark
for each dueler, which means a user who is not the dueler can accept on behalf of the other user.

Dueler.last
id: 20,
user_id: 78,
challenge_id: 178,
duel_id: 13,
accept: nil>


duels/show.html.erb

<% @duel.duelers.each do |dueler| %>
<% if current_user.id == dueler.user_id %>
<%= form_for @duel do |f| %>
<%= f.fields_for :duelers do |accept_fields| %> # Only Want to Show One Checkmark for current_user.id == dueler.user_id
Accept? : <%= accept_fields.check_box :accept %>
<% end %>
<%= f.submit %>
<% end %>
<% end %>
<% end %>

Answer

Try to retrieve just the dueler for the current_user (with an SQL query)

in the action

@duel = Dueler.find_by(user_id: current_user.id)

in the view

<%= form_for @duel do |f| %>
  Accept?  : <%= f.check_box :accept %>
  <%= f.submit %>
<% end %>
Comments