I asked a related question previously here. However, it may have been confusing as I was unable to get an answer. Therefore I shall ask a simpler question. For my Invitation Model, how do I submit more than 1 invitation at a time and after submission how do I access the data in resultant array/collection of hashes?
The Invitations model has a :email
string attribute and through form submission, I want to achieve something like the following(I'm assuming that'll be the best setup?):
"@invitation" => {
"@invitations" => [
{"email" => "example@hotmail.com" },
{"email" => "example@yahoo.com"},
{"email" => "example@gmail.com"}
]
}
The Following is the Code I have so far:
The Models
class Scoreboard < ActiveRecord::Base
has_many :invitations
end
class Invitation < ActiveRecord::Base
belongs_to :scoreboard
end
The Invitation#new View
<%= form_for [@scoreboard, @invitation] do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.fields_for :invitations do |invites| %> <!-- Tried Many things here with no success -->
<div>
<%= invites.label :email %>
<%= invites.text_field :email %>
</div>
<% end %>
<%= f.submit "Send Invitation", class: "btn btn-primary" %>
<% end %>
The Invitations Controller
class InvitationsController < ApplicationController
def new
@scoreboard = Scoreboard.find(params[:scoreboard_id])
@invitation= @scoreboard.invitations.build
end
def create
@scoreboard = Scoreboard.find(params[:scoreboard_id])
@invitation = @scoreboard.invitations.build(invitation_params)
if @invitation.save
flash[:success] = "Invitation sent successfully"
else
render 'new'
end
end
private
def invitation_params
params.require(:invitation).permit(:email)
end
end
Invitations#create View
<p> Display all the emails here for all the invitations <p>
In my original question, I had wanted to access the saved emails so I can mail to them, however if I can figure out how to display all the emails on this view, I can figure out the way the array/hash collections works and using that info, mail them out. Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire