遍历数组以创建Rails对象(Iterate through an array to create rails objects)

4 IT屋

I am having trouble finding any information on how to iterate through an array and create objects.

My form creates a selectable list of users that when checked, pass the user_ids as an array object.

invitations\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 %>

I would like to then create new Invitations objects by combining the attended_event_id with all of the objects in the attendee_id array.

After a bit of trouble I got the basics of my controller working but only by passing in the user_id as a text entry. Below is my Invitations controller. Not really sure where to start on this one as I haven't been able to find a good example.

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
解决方案

Do you mean something like this?

<%= bootstrap_form_for Invitation.new do |f| %>
  <br>
    <ul>
      <%= f.hidden_field :attended_event_id, :value => @event_selected.id %>
      <% users.each do |user| %>
        <li>
          <%= check_box_tag 'invitation[attendee_id][]', user.id %>
          <%= h user.name %>
        </li>
      <% end %>
    </ul>
  <br>
  <%= f.submit "Invite Selected Users" %>
<% end %>


def create
@invitations = invite_params[:attendee_id].map do |attendee_id|
  Invitation.new(
    attended_event_id: invite_params[:attended_event_id],
    attendee_id: attendee_id
  )
end

if @invitations.any?(&:invalid?)
  flash.now[:error] = "Failure!"
  redirect_to root_path
else
  @invitations.each(&:save!)
  flash.now[:success] = "Invited!"
  redirect_to root_path
end
end

private
def invite_params
  params.require(:invitation).permit(:attended_event_id, {:attendee_id => []})
end

我很难找到有关如何遍历数组和创建对象的任何信息。



我的表单创建了一个可选择的用户列表,该列表在选中后将user_ids作为数组对象传递。



invitations\new.html.rb



  <% = bootstrap_form_for Invitation.new做| 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。提交"邀请选定的用户"%>
<%end%>


然后我想通过将与会者的事件ID与与会者ID中的所有对象结合起来来创建新的邀请对象数组。



在遇到一些麻烦之后,我掌握了控制器的基本功能,但仅通过将user_id作为文本输入传入即可。以下是我的邀请函控制器。由于我一直找不到很好的例子,因此不太确定从哪里开始。



invitations_controller.rb



  def创建
@invitation = Invitation.new(invite_params)

如果@ invitation.save!
flash.now [:success] ="邀请!"
redirect_to root_path
else
flash.now [:error] ="失败!"
redirect_to root_path
结束
结束

私人
def Invitation_params

params.require(:invitation).permit(: Attendant_event_id,:attendee_id)
结束
结束

解决方案

您的意思是这样吗?



 <%= bootstrap_form_for Invitation.new do | f | %> 
< br>
< ul>
<%= f.hidden_​​field:attended_event_id,:value => @ event_selected.id%>
<%users.each do | user | %>
< li>
<%= check_box_tag"邀请[attendee_id] []",user.id%>
<%= h user.name%>
< / li>
<%end%>
< / ul>
< br>
<%= f。提交"邀请选定的用户"%>
<%end%>


def创建
@invitations = Invitation_params [:attendee_id] .map do | attendee_id |
Invitation.new(
included_event_id:Invitation_params [:attended_event_id],
与会者e_id:与会者ID

结尾

如果@ invitations.any ?(& ;:无效?)
flash.now [:error] ="失败!"
redirect_to root_path
其他
@ invitations.each(&:save!)
flash.now [:success] ="已邀请!"
redirect_to root_path
结束
结束

私人
def Invitation_params
params.require(:invitation).permit(:attended_event_id,{:与会者[id] => []})
结束

本文地址:IT屋 » 遍历数组以创建Rails对象