Reputation: 899
I'm working on a Rails API that creates shared links for users. The input accepts a JSON hash that looks like this:
{
"shared_with":{
"1": "[email protected]",
"2": "[email protected]"
},
"expiry": "30 June 2017"
}
When it hits the controller it iterates through the emails that were submitted, creates the DB entry, and triggers a mailer for each. This is the controller action:
def create
@user = current_user
@recipients = share_params
# save and mail in one step
# first iterate over the email addresses sent
@recipients[:shared_with].each do |recipient|
# assign a token
@token = Share.generate_token
# save record, token, expiry
@user.shares.create!(
token: @token,
shared_with: **???**,
expiry: @recipients[:expiry]
)
# trigger mailer for email
ShareMailer
.share_dealsheet(recipient, @user, @token)
.deliver_now
end
json_response(:created)
end
Everything is saving correctly and the mailers are being generated, but I can't figure out what to put in shared_with:
to get the current email in the loop.
Upvotes: 0
Views: 44
Reputation: 1638
There is error in your json (comma is missing).
{
"shared_with":{
"1": "[email protected]",
"2": "[email protected]"
},
"expiry": "30 June 2017"
}
Now change your code to :
@recipients[:shared_with].each do |key,recipient|
# assign a token
@token = Share.generate_token
# save record, token, expiry
@user.shares.create!(
token: @token,
shared_with: recipient,
expiry: @recipients[:expiry]
)
# trigger mailer for email
ShareMailer
.share_dealsheet(recipient, @user, @token)
.deliver_now
end
Upvotes: 1