Hello, I have the following:
My Controller:
def new
.
.
@teammembers.each do |teammember|
request = @request_thread.requests.build(:recipient_id =>
teammember.user_id, :full_name => 'Billy Bob T')
end
My View:
.
.
<%= f.fields_for :requests do |builder| %>
<div class="field">
<%= builder.label :full_name %>
<%= builder.check_box :recipient_id, :checked => false %>
</div>
<% end %>
.
.
The nested form for requests only holds the user_id, not the
user.name... Problem is in the nested form, I need to show the
user.name next to the check_box. So I tried adding a virtual attribute
in the model (attr_accessor :full_name), so I could use full_name but
I can't see to access that in the nested form (inside builder).
Also, the checkbox doesn't have the recipient_id as the value being
set? I want to use the checkbox to tell Rails whether or not to create
the record or not on submit using something like this in the model
":reject_if => lambda { |a| a[:recipient_id].blank? }"
Anyone with fields_for experience?
Any tips or suggestions from the pros?
Thanks
Follow-up, in the nested form if I have:
<div class="field">
<%= item.label :recipient_id %>
<%= item.text_field :recipient_id %>
<%= item.check_box :recipient_id %>
</div>
I get an output of
<div class="field">
<label
for="request_thread_requests_attributes_1_recipient_id">Recipient</
<input id="request_thread_requests_attributes_1_recipient_id"
name="request_thread[requests_attributes][1][recipient_id]" size="30"
type="text" value="61">
<input name="request_thread[requests_attributes][1][recipient_id]"
type="hidden" value="0"><input checked="checked"
id="request_thread_requests_attributes_1_recipient_id"
name="request_thread[requests_attributes][1][recipient_id]"
type="checkbox" value="1">
</div>
So the ID I want is working for text_field but not check_box
Shouldn't this work: "<%= item.check_box :recipient_id, :value
=> :recipient_id %>"
That outputs:
<input checked="checked"
id="request_thread_requests_attributes_1_recipient_id"
name="request_thread[requests_attributes][1][recipient_id]"
type="checkbox" value="1">
the value is wrong. All the checkboxes show value 1. Yet, this "<%=
item.text_field :recipient_id %>" gets the right value?