I'm trying to use simple_form to gather one or multiple email address, then pass those email addresses as an array so that ActionMailer can send out invitation emails to those addresses. Unsure about how to get all the input fields into one array that is passed to the controller and mailer. Here is what I have so far.
Input form:
<div class="user-group-partial">
<%= simple_form_for :user_emails, :url => "/user_groups/sent_emails/", :method => :post do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :email, :maxlength => 25 %>
<%= f.input :email, :maxlength => 25 %>
<%= f.input :email, :maxlength => 25 %>
<br>
<div class="form-actions">
<%= f.button :submit, :class => "btn btn-primary", :value => "Invite Bros" %>
</div>
</div>
<% end %>
<br>
Controller Method:
def send_invite_to_members
token = current_user.user_group.token
email = params[:user_emails][:email]
UserGroupMailer.group_invitation_email(email, token).deliver
redirect_to '/user_groups', :notice => "Your invitations have been sent"
end
ActionMailer Method:
def group_invitation_email(email_address, token)
#token = token.to_s
mail to: email_address, subject: "You've been invited!"
end
Thanks!
One simple way to solve this is to use a text field with a helper label that instructs the user to type email addresses separated by spaces or commas.
Then in your controller you can split them up and send out each email with something like:
def send_invite_to_members
token = current_user.user_group.token
emails = params[:user_emails][:emails].split(",") # or split(" ") if you want them separated by a space
emails.each do |e|
UserGroupMailer.group_invitation_email(e, token).deliver
end
redirect_to '/user_groups', :notice => "Your invitations have been sent"
end
Related
I need to send the same message to three different e-mails.
How can I do this?
Something like using a for each?
This is working, but if I just add a field in my view, like:
<div class="form-group">
<%= label_tag(:to, "Recipient's email address") %>
<%= email_field_tag :to, "", class: "form-control" %>
</div>
I get the error:
400 bad request
because of the parameters.
My HTML is:
<h1>Send a message with Mailgun</h1>
<% flash.each do |key, value| %>
<div class="alert alert-success"><%= value %></div>
<% end %>
<%= form_tag(:action => 'create')%>
<div class="form-group">
<%= label_tag(:to, "Recipient's email address") %>
<%= email_field_tag :to, "", class: "form-control" %>
</div>
<div class="form-group">
<%= label_tag(:body, "Message") %>
<%= text_area_tag :body, "", class: "form-control" %>
</div>
<%= submit_tag("Send", :class => "btn btn-primary") %>
And my model:
class HomeController < ApplicationController
def index
end
def create
# Get message value from form
#message = params
# First, instantiate the Mailgun Client with your API key
mg_client = Mailgun::Client.new ('7a9c4084f-b0aacd0-a2b65624')
# Define your message parameters
html_output = render_to_string template: "notifier/send_email"
message_params = {:from => 'postmaster#sandbox671e4d0f627c.mailgun.org',
:to => #message[:to],
:subject => 'Mailgun message via API',
:text => #message[:body],
:html => html_output.to_str,
"o:tag" => 'test'}
# Send your message through the client
mg_client.send_message ('sandboxe6fb741ddb25.mailgun.org'), message_params
# Redirect on success
redirect_to root_path, notice: 'Message was sent.'
end
end
I want to send multiple invitations at a time using devise-invitable gem.
new.html.erb
<h2>
<%= t "devise.invitations.new.header" %>
</h2>
<%= form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => {:method => :post} do |f| %>
<%= devise_error_messages! %>
<div>
<% resource.class.invite_key_fields.each do |field| -%>
<p><%= f.label field %><br />
<%= f.email_field field, placeholder: "Invitation email" %></p>
<% end -%>
<%= f.collection_select(:role_id, Role.all, :id, :role_name, :prompt => true) %>
</div>
<p>
<%= f.submit t("devise.invitations.new.submit_button") %>
</p>
<% end %>
my controller:-
class Users::InvitationsController < Devise::InvitationsController
def create
exit
if params[:user][:email]== "" || params[:user][:role_id] == ""
flash[:alert]="Please enter email first and Select any role for invitees"
redirect_to new_user_invitation_path
else
if User.invite!(:email => params[:user][:email], :company_id => current_user.id, :type => 'Employee', :role_id => params[:user][:role_id])
flash[:notice]="Invitation is send successfully."
redirect_to new_user_invitation_path
else
flash[:alert]="Invitation is not send."
redirect_to new_user_invitation_path
end
end
end
end
I think one solution is to pass comma separated emails in invite method but how can I pass it? I really don't know how.
If you have any other solution then please tell me.
Thanks.
i trouble with this problem but finally i got solution to send multiple invitation email at a time.
below i explain my code that how i become possible this.
here is my html view.
new.html.erb
<h2>
<%= t "devise.invitations.new.header" %>
</h2>
<%= form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => {:method => :post} do |f| %>
<%= devise_error_messages! %>
<div>
<% resource.class.invite_key_fields.each do |field| -%>
<%= f.label field %><br />
<%= f.email_field field, name: "user[email][]", placeholder: "Invitation email", required: true %></p>
<div id="createNewTextbox" >
</div>
<a id="btnLinkCreate" href="#" onClick="create_new();">
+ INVITE MORE
</a>
<% end -%>
</div>
<p>
<%= f.submit t("devise.invitations.new.submit_button") %>
</p>
<% end %>
in this i take one text-box by-default with name : "user[email][]" (it is important because using this rails automatically create email array and send in params whene you submit form )
i also generate dynamic text-box using JavaScript and it will create below div when click on invite more link button :
<div id="createNewTextbox" >
</div>
<a id="btnLinkCreate" href="#" onClick="create_new();">
+ INVITE MORE
</a>
here is my dynamic text-box code of JavaScript.
$('#createNewTextbox').append('<input type="email" id="email'+i+'" name="user[email][]" placeholder="Invitation email" required/>');
now you can see that i give same name to dynamic text-box (name="user[email][]"), so rails automatically create hash array like this:
"user" => { "email" => { "email-1","email-2","email-3"... } }
now this hash array is pass in create method in which we fetch every email from params and give it to invite method to send the invitation.
my controller :-
class Users::InvitationsController < Devise::InvitationsController
def create
params[:user][:email].each do |email|
User.invite!(:email => email)
end
end
end
thats it...
if still you have any query then tell comment me.
I have added Stripe payment to my app to handle registration payments for events, the form is on the event show page so the method is in the events controller but I have created a registration model. My issue is trying to get the price of the event into the save_with_payment method inside the registration model(total_price).
Any ideas? I have tried attr_accessible :price but with no luck.
Event Controller
def show
#event = Event.where(:slug => params[:slug]).first
#registration = Registration.new
end
def payment
#registration = Registration.new(params[:registration])
if #registration.save_with_payment
RegistrationMailer.admin(#registration, #event).deliver
RegistrationMailer.delegate(#registration, #event).deliver
redirect_to root_path
flash[:success] = "Thank you for registering for <% #event.title %>"
else
redirect_to root_path
flash[:error] = "We're sorry, something went wrong"
end
end
Registration Model
def save_with_payment
if valid?
charge = Stripe::Charge.create({
amount: total_price, ##I can declare a integer here instead of a method and it works
currency: "gbp",
card: stripe_card_token,
description: email })
save
end
rescue Stripe::CardError => e
end
def total_price
#event.price = price
price
end
Routes
match 'registration' => 'events#show', :via => :get
match 'registration' => 'events#payment', :via => :post
View
<%= simple_form_for [#registration], :method => :post, :url => registration_path do |f| %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email %>
<%= f.input :job_title %>
<%= f.input :company %>
<%= f.input :stripe_card_token, as: :hidden %>
<div class="input card_number">
<label for="card_number">Card number</label>
<%= text_field_tag :card_number %>
</div>
<div class="input card_cvc">
<label for="card_code">Security code (CVC)</label>
<%= text_field_tag :card_cvc %>
</div>
<div class="input card_dates">
<label>Card expiration date</label>
<%= select_month nil, { add_month_number: true }, { id: "card_month"} %>
<%= select_year nil, { add_year: Date.today.year, end_year: Date.today.year + 15 }, { id: "card_year"} %>
</div>
<%= f.button :submit, "Register", class: "button" %>
If I got everything right, here:
def show
#event = Event.where(:slug => params[:slug]).first
#registration = Registration.new
end
is controller action with view, containing form.
Registration should belong to event, I suppose, through :has_one
Then your registration model needs event_id integer column. As you declared #event in show, append you registration form with
<%= f.input :event_id, as: :hidden, value: #event.id %>
After that you can find #event in payment method by:
#event=Event.find(params[:registration][params[:event_id]].to_i)
This is basic example, I don't actually know, what is event in your app, may be there is better attribute which you can pass.
After that you can pass #event.price as argument to model method:
def save_with_payment(total_price)
if valid?
charge = Stripe::Charge.create({
amount: total_price,
#other code
I am new to Rails and I am using the salesforce gem, databasedotcom so there are is no model where I would normally validate the email address format, simple_form is validating presence but this error when an invalid email is entered:
Databasedotcom::SalesForceError at /leads
Email: invalid email address:def create
Here is the controller:
def create
#lead = Lead.new(params[:lead])
#lead['OwnerId'] = '005b0000000WxqE'
if #lead.save
redirect_to #event
else
render "new"
end
end
Here is the form:
<%= simple_form_for #lead, url: leads_path(#lead, event_id: params[:event_id]), method: :post do |f| %>
<%= f.input :FirstName, :label => "First Name" %>
<%= f.input :LastName, :label => "Last Name" %>
<%= f.input :Email %>
<%= f.input :Company %>
<%= f.button :submit, value: "Submit" %>
<% end %>
I need to add something like 'if #lead.valid?' but im not sure what steps to take to make this work because that on its own doesn't work.
This seems to be an options for you, client_side_validations-simple_form gem has the simple_form with client_side_validation.
hope this helps
I am a user model and in my view I have:
#user = current_user
User model have an attribute with name "email".
and I want send one e-mail to multiple email address with a subject.
I have a form like:
<%= form_for (Email.new), :method => :post, :remote => true, :url => { :controller => "users", :action => "invite_friends" } do |f| %>
<%= f.text_field :address1 %>
<%= f.text_field :address2 %>
<%= f.text_field :address3 %>
<%= f.text_field :address4 %>
<%= f.text_area :subject_email %>
<% end %>
Have I that create a "email model" with attributes address and subject_email?
Check section 2.3.3 Sending Email To Multiple Recipients from the Rails Guide