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
Related
Please I am new to Ruby on Rails and also new to stackoverflow. I am using the simple_form gem to create a Contact form where visitors will sign up for newsletter.
I have a Contact model and new.html.erb view file. The problem is I get the following error mesage when I navigate to the Contact link
"undefined local variable or method `simple' for #<#:0xb44fae84> " Please what am I doing wrong?
Note: I am using Rails 4.2.6 and ruby 2.2.1
Thank you.
The Contact Model is in app/models/contact.rb as shown below.
class Contact < ActiveRecord::Base
has_no_table
column :name, :string
column :email, :string
column :content, :string
validates_presence_of :name
validates_presence_of :email
validates_presence_of :content
validates_format_of :email,
:with => /\A[-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i
validates_length_of :content, :maximum => 500
end
and the view file in app/views/contacts/new.html.erb is as shown below:
<% content_for :title do %>Contact<% end %>
<h3>Contact</h3>
<div class="form">
<%= simple_form_for #contact do |form| %>
<%= simple.error_notification %>
<%= form.input :name, autofocus: true %>
<%= form.input :email %>
<%= form.input :content, as: :text %>
<%= form.button :submit, 'Submit', class: 'submit' %>
<% end %>
</div>
simple isn't a defined method, like the error says.
Use Simple Form's error notification method in your form:
= f.error_notification
Or, for better customization, add your own helper partial to render errors (I'm using the slim templating engine in this example):
_form_errors.html.slim:
- if resource.errors.any?
#error_explanation
.alert.alert-error
h2.error-header Please fix the following #{resource.errors.count} errors
ul
- resource.errors.full_messages.each do |msg|
li= "* #{msg}"
In your form
= render "partials/form_error", resource: #contact
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.
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
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
I'm working on an Ruby on Rails application (2.3.x) and i want to make a form that lets the user login or register. I want to do this in the same form. I have a JS function that replaces the form elements like this:
Login form:
<% form_for #user do |f| %>
<div id="form">
<%= f.label :email, "E-mail" %>
<%= f.text_field :email %>
<%= f.label :password, "Password" %>
<%= f.password_field :password %>
<%= link_to "I don't have an account, "#", :id => "changeForm"%>
<%= f.submit "Login" %>
</div>
<% end %>
The id "changeForm" triggers a JS function that changes the form elements. So if you press the url the html looks like this:
<% form_for #user do |f| %>
<div id="form">
<%= f.label :name, "Name" %>
<%= f.text_field :name %>
<%= f.label :email, "E-mail" %>
<%= f.text_field :email %>
<%= f.label :password, "Password" %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Password confirmation" %>
<%= f.password_field :password_confirmation %>
<%= link_to "I do have an account, "#", :id => "changeForm"%>
<%= f.submit "Register" %>
</div>
<% end %>
I added the neccesary validations to my user model:
class User < ActiveRecord::Base
attr_reader :password
validates_presence_of :name, :email, :password
validates_format_of :email, :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
validates_confirmation_of :password
But what happens when you fill in the email / password you get the errors that the name is missing and that the password fields aren't confirmed. So i could do some nasty programming in my user model like this:
#if password_conf or the name are present the user has tried to register...
if params[:user][:password_confirmation].present? || params[:user][:name].present?
#so we'll try to save the user
if #user.save
#if the user is saved authenticate the user
current_session.user = User.authenticate(params[:user])
#if the user is logged in?
if current_session.user.present?
flash[:notice] = "succesvully logged
redirect_to some_routes_path
else
#not logged in...
flash[:notice] = "Not logged in"
render :action => "new"
end
else
#user not saved
render :action => "new"
end
else
#So if the params[:user][:password_confirmation] or [:user][:name] weren't present we geuss the user wants to login...
current_session.user = User.authenticate(params[:user])
#are we logged_in?
if current_session.user.present?
flash[:notice] = "Succesvully logged in"
redirect_to some_routes_path
else
#errors toevoegen
#user.errors.add(:email, "The combination of email/password isn't valid")
#user.errors.add(:password," ")
render :action => "new"
end
end
end
Without validations this (imho dirty code and should not be in the controller) works. But i want to use the validates_presence_of methods and i don't want to slap the "conventions over configurations" in the face.
So another thing i have tried is adding a hidden field to the form:
#login form
<%= f.hidden_field :login, :value => true %>
# and ofcourse set it to false if we want to register.
And then i wanted to use the method: before_validation
before_validation_on_create do |user|
if params[:user].login == true #throws an error i know...
validates_presence_of :email, :password
validates_format_of :email, :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
else
validates_presence_of :name, :email, :password
validates_format_of :email, :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
validates_confirmation_of :password
end
end
But this doesn't work because i can't access the params. And login isn't a attribute for the user object. But i thought that in this way i could validate the email and password params if the user wants to login. And all the other attrs if the user want to register.
So all i could think of doesn't work how i want it to work. So my main goal is this:
1 form for login/register with the use of the validation methods in the user model. So if we want to login but don't fill in any information => give validation errors. And if the user wants to login but the email/password combination doens't match give the "#user.errors.add(:email, "the combination wasn't found in the db...")". And the same goes for user register...
Thanks in advance!
You should point each form to the appropriate controller action:
# Registration form
<% form_for #user, :url => users_path do |f| %>
...
<% end %>
# Login form, non-RESTful action
<% form_for #user, :url => login_users_path do |f| %>
...
<% end %>
This assumes you have a login action in your UsersController, but if you'd like to have a dedicated controller manage your sessions:
# Use SessionsController (Sessions#Create) to create a new user session
<% form_for #user, :url => sessions_path do |f| %>
...
<% end %>