Tôi đang sử dụng fields_for
trong hình thức của tôi như vậyfields_for gửi mảng thay vì Hash (Rails 3)
<%= form_for @user %>
...
<%= f.fields_for :photos do |f2| %>
<%= f2.radio_button :public, 'true' %>
<% end %>
...
<% end %>
Dưới đây là các nút radio nó tạo ra:
<input id="user_photos_attributes_0_public_true" name="user[photos_attributes][0][public]" type="radio" value="true" />
<input id="user_photos_attributes_0_id" name="user[photos_attributes][0][id]" type="hidden" value="1" />
<input id="user_photos_attributes_1_public_true" name="user[photos_attributes][1][public]" type="radio" value="true" />
<input id="user_photos_attributes_1_id" name="user[photos_attributes][1][id]" type="hidden" value="4" />
<input id="user_photos_attributes_2_public_true" name="user[photos_attributes][2][public]" type="radio" value="true" />
<input id="user_photos_attributes_2_id" name="user[photos_attributes][2][id]" type="hidden" value="5" />
...
Tôi có điều này trong sử dụng .rb
has_many :photos
accepts_nested_attributes_for :photos
Khi gửi biểu mẫu tôi nhận được lỗi này:
Error during failsafe response: ActionView::Template::Error
TypeError (expected Hash (got Array) for param `photos_attributes'):
Có ai biết tại sao điều này xảy ra không?
Btw, tôi đang sử dụng Rails 3.0.0.rc2
Lưu xảy ra với tiêu chuẩn '@ user.update_attributes (params [: user])'. Thật không may tôi không thể xem băm params bởi vì nó chết trước khi in đó vào giao diện điều khiển. Tôi giả sử băm params có mảng như bạn đã viết nó mặc dù - vì vậy vẫn không chắc chắn lý do tại sao nó phàn nàn về điều đó. Cảm ơn vì sự trả lời! –
@Brian, bạn có thể thực hiện 'logger.debug (params.inspect)' để kiểm tra. –
Xin chào Andrew, cảm ơn bạn đã phản hồi. Vì vậy, thực sự tôi sẽ đặt dòng đó ở đâu? Nó chết trước khi nhập vào hành động điều khiển của tôi, hoặc application_controller. Đó là một số loại failsafe cho đường ray chết trong khi đầu tiên xử lý yêu cầu để nó không bao giờ thậm chí đi vào bất kỳ mã của tôi. Hoặc là có nơi nào khác để thêm nó? –