我無法找到有關如何遍歷數組和創建對象的任何信息。遍歷數組創建rails對象
我的表單創建一個可選擇的用戶列表,當選中時,將user_ids作爲數組對象傳遞。
邀請\ new.html.rb
<%= bootstrap_form_for Invitation.new do |f| %>
<br>
<ul>
<%= f.hidden_field :attended_event_id, :value => @event_selected.id %>
<li>
<%= check_box_tag 'attendee_id[]', user.id %>
<%= h user.name %>
</li>
<% end %>
</ul>
<br>
<%= f.submit "Invite Selected Users" %>
<% end %>
我想再由attended_event_id與所有attendee_id數組中的對象的結合創造新的邀請對象。
經過一些麻煩,我得到了我的控制器工作的基礎知識,但只通過傳遞user_id作爲文本條目。以下是我的邀請控制器。由於我一直無法找到一個好例子,所以不知道從哪裏開始。
invitations_controller.rb
def create
@invitation = Invitation.new(invite_params)
if @invitation.save!
flash.now[:success] = "Invited!"
redirect_to root_path
else
flash.now[:error] = "Failure!"
redirect_to root_path
end
end
private
def invite_params
params.require(:invitation).permit(:attended_event_id, :attendee_id)
end
end
Thanks !!我正在經歷這個,但仍然遇到:「NoMethodError(未定義的方法'映射爲零:NilClass):」錯誤。我做了所有其他verbatum(除了將attendie_id更改爲attendee_id,我猜這只是一個錯字。 – Prkl8r
感謝讓我開始!我不得不做一些更改,但基本上做到了這一點。 ) – Prkl8r