send email to multiple email address rails 3 - ruby-on-rails

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

Related

Rails Forms - Edit User Form

I am trying to create an Edit User Form to amend the user details stored in my database but keep getting error. I have changed the form URL and tried changing it to a method: :get without much success.
Edit User Form
<%= form_with url: #user, :class => "user_sign_in_form" do |form| %>
<%= form.text_field "user[email]", placeholder: "email", :class => "form_box" %>
<%= form.text_field "user[username]", placeholder: "username", :class => "form_box"%>
<%= form.text_field "user[password]", placeholder: "password", :class => "form_box" %>
<%= form.submit "SUBMIT" %>
<% end %>
Edit Action
def edit
#user = User.find(params[:id])
end
Link to Edit User Form
<%= link_to "Edit", edit_user_path(#user) %>
Error Message
No route matches [POST] "/users/58"
You need to use model: #user not url: #user.
<%= form_with model: #user, :class => "user_sign_in_form" do |form| %>
<%= form.text_field :email, placeholder: "email", :class => "form_box" %>
<%= form.text_field :username, placeholder: "username", :class => "form_box"%>
<%= form.password_field :password, placeholder: "password", :class => "form_box" %>
<%= form.submit "SUBMIT" %>
<% end %>
When you pass a model form_with will set both the action (/users or /users/:id) and method (POST or PATCH) depending on if the record has been saved. See Resource Routing: the Rails Default.
The url: option should really only be used if you have to override the conventional routes or if you have a form that does not wrap a model instance.
Also use form.text_field :email instead which will "bind" the input to the model attribute. Nobody likes having to refill an entire form from scratch because they missed some minor detail.
And on a side note do not use placeholders instead of labels - its an aweful practice from both a useability and accessibility standpoint.

Rails Form Object Returns Object ID in mail

I have the below form which works absolute fine but when submitted the :event field returns an ID in the mailer, any ideas how to prevent this?
Form
<%= simple_form_for #sponsorship_inquiry, :method => :post do |f| %>
<%= f.input :spam, as: :hidden %>
<%= f.input :name %>
<%= f.input :phone %>
<%= f.input :email %>
<%= f.input :job_title %>
<%= f.input :company %>
<%= f.input :event, :collection => Event.where(:end_date.gt => Date.today, :is_live => 'true') %>
<%= f.input :message, as: :text, :input_html => { :cols => 5, :rows => 6 } %>
<%= f.button :submit %>
<% end %>
Mailer
Name: <%= #sponsorship_inquiry.name %>
Phone: <%= #sponsorship_inquiry.name %>
E-Mail: <%= #sponsorship_inquiry.email %>
Job Title: <%= #sponsorship_inquiry.job_title %>
Company: <%= #sponsorship_inquiry.company %>
Event: <%= #sponsorship_inquiry.event %>
Message: <%= #sponsorship_inquiry.message %>
Controller
def new
#sponsorship_inquiry = SponsorshipInquiry.new
end
def create
# Hidden field for bots/spiders
redirect_to new_inquiry_path and return if params[:spam].present?
#sponsorship_inquiry = SponsorshipInquiry.new(params[:sponsorship_inquiry])
if #sponsorship_inquiry.valid?
SponsorshipInquiryMailer.admin(#sponsorship_inquiry).deliver
redirect_to sponsorship_inquiries_path
else
render :new
end
end
Need your SponsorshipInquiry model.
If you have
class SponsorshipInquiry < ActiveRecord::Base
belongs_to :event
end
try send <%= #sponsorship_inquiry.event.name %> or whatever )
Or you need to parse needed value from the form if "event" is only field not associated with Event model.
IMHO
If your question is "can I modify the form so that I automatically (magically?) get an object as a param?", the answer is definetly no.
What you have to do is to search the event object in the database based on the received id.

Adding inputs from simple_form into array

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

Converting form tag to simple form

How would I go about converting this form tag below into a form_for?
<%= form_tag(contact_email_path, :method => 'post') do %>
<%= label_tag "Your email" %>
<%= text_field_tag "sender", #sender, :autofocus => true %>
<%= label_tag "Subject" %>
<%= text_field_tag "subject", #subject %>
<%= label_tag "Message" %>
<%= text_area_tag "message", #message %>
<%= submit_tag "Send Email" %>
<% end %>
form_for is a helper for creating forms which create or edit a resource.
If you have a resource here that you would like to create in your database, you would use this method. What it looks like you're doing here is not creating a resource, but sending an email. If that is the case, then a form_tag is probably a better option.
If you are, however, trying to create a new resource in the database (i.e. an new instance of ContactEmail or some other class), then you could do it like this:
<%= form_for #contact_email do |f| %>
<%= f.label :sender, "Your email" %>
<%= f.text_field :sender, :autofocus => true %>
<%= f.label :subject %>
<%= f.text_field :subject %>
<%= f.label :message %>
<%= f.text_area :message %>
<%= f.submit "Send Email" %>
<% end %>
This assumes that #contact_email is an object that has the methods sender, subject and message and that you have resources :contact_email in your routes file.

Rails: dynamically changing form content

I am building message app in rails where user can send message from templates. I have a database of templates, and the user can select templates as per category.
In my message model, I want to render the template dynamically based on category selected. I looked for examples on google, but was not able to find a relevant solution.
This is my message form:
<%= form_for #message, :html => {:multipart => true} do |m| %>
<%= m.select :biz_case, options_for_select(Message::Bcase), :prompt => "Select business case" %>
<%= m.text_field :subject, :class => "message-text", :placeholder => "Subject" %>
<div class="message-body">
<%= m.text_area :message, :class => "message-body", :class => "redactor", :placeholder => "Your content" %>
</div>
<%= m.select :user_type, options_for_select(Customer::CType), :prompt => "Customer segment" %>
<%= m.submit %>
<% end %>
In the above form, I am looking to display the subject and body based on the selected business case. Something like:
if biz_case == "promote"
subject = #template.subject where ("biz_case = ?", "promote")
message = #template.content where ("biz_case = ?", "promote")
end
The subject and message would be displayed in input text fields.
Can anyone tell me how to do this?
in your method:
#subject = #template.subject where ("biz_case = ?", "promote")
in view:
<%= m.text_field :subject, :value => #subject, :class => "message-text", :placeholder => "Subject" %>

Resources