email not getting sent in rails 4 using sidekiq - ruby

I am trying to send an email in rails 4 using sidekiq.
In this the values are not getting saved in the database instead only the form values are taken and the email is sent.
I have follwed this link https://www.codefellows.org/blog/how-to-set-up-a-rails-4-2-mailer-with-sidekiq
But the problem is that the email is not getting sent.
visitor_controller.rb
class VisitorsController < ApplicationController
def index
end
def contact
h = JSON.generate({ 'name' => params[:name],
'email' => params[:email],
'message' => params[:message] })
PostmanWorker.perform_async(h, 5)
# if instead of sidekiq I was just sending email from rails
#VisitorMailer.contact_email(#name, #email, #message).deliver
redirect_to :root
end
end
visitor_mailer.rb
class VisitorMailer < ApplicationMailer
def contact_email(name, email, message)
#name = name
#email = email
#message = message
mail(from: #email,
to: 'javier#badaboom.com',
subject: 'New Visitor\'s Email')
end
end
contact_email.html.erb
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1><%= #name %> (<%= #email %>)</h1>
<p>
<%= #message %>
</p>
</body>
</html>
index.html.erb
<h1>Email</h1>
<p>
<%= label_tag(:name, "My name is:") %>
<%= text_field_tag(:name) %>
</p>
<p>
<%= label_tag(:email, "My email address is:") %>
<%= text_field_tag(:email) %>
</p>
<p>
<%= label_tag(:message, "My Message is:") %>
<%= text_area_tag(:message) %>
</p>
<p>
<%= submit_tag("Send Email") %>
</p>
I want to as to why the email is not getting sent...
Thanks in advance!!!!

Related

Display data according to text field value using Rails 3

I have a issue.I want to fetch data from data base according to text field value.This text field will take two types of value.First one is simple number(e.g-123456789) and second one is like this(e.g-123456789/1).The simple number is present in DB for one table.In the second one number after "/" (i.e-1) is another table's id which is associated with first table.Then my aim is when user will give the input "123456789" the data will fetch according to this number by searching and when user will give the input "123456789/1" first it will split the number and values will be fetched according to both number and id (i.e-123456789 and 1) from the both table.
Here i am explaining some of my code below.
homes/hcsy_html.erb
<% if current_admin %>
<div class="header">
<div class="navbar-header">Swargadwar, Puri Municipality,govt of odisha</div>
<div class="nav navbar-top-links navbar-right">
<div class="image"></div>
</div>
<div class="name-div">
</div>
</div>
<div class="menu-div">
<div id="leftsidebtn">
<ul>
<li>Create User</li>
<li>Scan Report</li>
<li>View and Payment Report
<ul>
<li>HCSY</li>
</ul>
</li>
<li>Payment Validate</li>
<li>Log Out</li>
</ul>
</div>
</div>
<div class="content-div">
Logged in as:<%= current_admin.email %>
<center><h1>HARICHANDRA SAHAYATA YOJANA SLIP</h1></center>
<%= form_for :hcsy,:url => {:action =>'scan_hcsy' } do |f| %>
<%= f.text_field :reciept,placeholder:"Get your scan code",:onchange => 'this.form.submit();' %>
<% end %>
<% if params[:id] %>
<center><h1>HARICHANDRA SAHAYATA YOJANA SLIP</h1></center>
Receipt No :<%= #hcsys.Receipt_No %>
<div class="left-content">
<p>Deceased Name :</p> <%= #hcsys.Deceased_Name %>
<p>Beneficary name :</p> <%= #hcsys.Beneficiary_Name %>
<p>Relation with Deceased :</p> <%= #hcsys.Beneficiary_Rel_With_Decease %>
<p>Address :</p> <%= #hcsys.Address %>
<p>Police station :</p> <%= #hcsys.PoliceStation %>
<p>Mobile No :</p> <%= #hcsys.Mobile_No %>
<p>Occupation :</p> <%= #hcsys.Occupation %>
<p>Brahmin :</p> <%= #hcsys.Brahmin %>
<p>Amount Required :</p> <%= #hcsys.Amount_Required %>
<p>Has He/She recieved any assistance erlier from this fund :</p> <%= #hcsys.Recieved_Fund_Earlier %>
</div>
<div class="right-content">
<p>BPL :</p> <%= #hcsys.BPL %>
<p>Govt. Service :</p> <%= #hcsys.Govt_Service %>
<p>Business :</p> <%= #hcsys.Business %>
<p>Land of property :</p> <%= #hcsys.Land_Property %>
<p>Other :</p> <%= #hcsys.Others %>
</div>
<% end %>
</div>
<% end %>
controller/homes_controller.rb
class HomesController < ApplicationController
def index
end
def registration
#user=User.new
end
def usersave
#admin=Admin.find(params[:id])
#user=User.new(params[:user])
#user.admin_id=#admin.id
if #user.save
flash[:notice]="User has created successfully"
flash[:color]="valid"
redirect_to :action => "index"
else
flash[:alert]="User could not created"
flash[:color]="invalid"
render 'registration'
end
end
def hcsy_reg
#hcsy=THcsy.new
end
def create_reg
#hcsy=THcsy.new(params[:hcsy])
if #hcsy.save
flash[:notice]="Data has saved successfully"
flash[:color]="valid"
redirect_to :action => "hcsy_details",:id1 => params[:id],:id2 => #hcsy.id
else
flash[:alert]="Data could not saved successfully"
flash[:color]="invalid"
render 'hcsy_reg'
end
end
def scan_hcsy
#hcsy=THcsy.find_by_Receipt_No(params[:hcsy][:reciept])
if #hcsy
flash[:notice]="Check the record"
flash[:color]="valid"
redirect_to :action => 'hcsy',:id => #hcsy.id
else
flash[:alert]="Receipt number could not found"
flash[:color]="invalid"
render 'hcsy'
end
end
def hcsy
if params[:id]
#hcsys=THcsy.find(params[:id])
end
end
def scanrecord
#hcsy=THcsy.find(params[:id])
end
def hcsy_deatils
#t_hcsy=THcsyFundTypeMaster.new
end
def create_details
#t_hcsy=THcsyFundTypeMaster.new(params[:t_hcsy])
if #t_hcsy.save
flash[:notice]="Check the record"
flash[:color]="valid"
redirect_to :action => 'hcsy_details_master',:id1 => params[:id1] ,:id2 => params[:id2] , :id3 => #t_hcsy.HCSY_Fund_Type_ID
else
flash[:alert]="Receipt number could not found"
flash[:color]="invalid"
render 'hcsy_deatils'
end
end
def hcsy_details_master
#t_hcsy_master=THcsyDetails.new
end
def create_details1
#admin=Admin.find(params[:id1])
#hcsy=THcsy.find(params[:id2])
#t_hcsy=THcsyFundTypeMaster.find_by_HCSY_Fund_Type_ID(params[:id3])
#t_hcsy_master=THcsyDetails.new(params[:t_hcsy_master])
#t_hcsy_master.Created_By=#admin.id
#t_hcsy_master.HCSY_ID=#hcsy.id
#t_hcsy_master.HCSY_Fund_Type_ID=#t_hcsy.HCSY_Fund_Type_ID
if #t_hcsy_master.save
flash[:notice]="Record has created"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]="Record could not create"
flash[:color]="invalid"
render 'hcsy_details_master'
end
end
end
Here i have done for simple number please help me to fetch data from both table by the second input number(i.e-123456789/1).For this all operations are executing inside "scan_hcsy" method.Atleast help me to split the number(i.e-123456789/1) to 123456789 and 1 so that i can fetch data according to this number and id.
You can split the string, which results into an Array:
"12345678/1".split('/')
=> ["12345678", "1"]
In your case:
splitted = params[:hcsy][:reciept].split('/')
hcys = splitted[0]
table_id = splitted[1]

How to show the link inside the email in proper way

Please help to resolve this issue.Actually i want to send one confirmation link to email via ROR.The email has been sent but the url is not showing in proper way.
BELOW IS MY CODE SNIPPETS.
views/users/index.html
<h1>This is index page</h1>
<center>
<p>Enter data</p>
<div class="option">
<p><%= link_to "Click here to enter data",users_new_path %></p>
<p><%= link_to "Display data",users_show_path%></p>
</div>
</center>
views/users/edit.html.erb
<h1>Edit your data here</h1>
<center>
<%= form_for #user ,:url => {:action => "update",:id => params[:id]} do |f| %>
<div class="div_reg">
<p>
<label for="username" class="uname" data-icon="u" >username </label>
<%= f.text_field:name,placeholder:"Enter your user name" %>
</p>
<p>
<label for="username" class="uname" data-icon="u" >Email </label>
<%= f.text_field:email,placeholder:"enter your email" %>
</p>
<p>
<label for="username" class="uname" data-icon="u" >Password </label>
<%= f.password_field:password,placeholder:"Enter your password" %>
</p>
<p>
<label for="username" class="uname" data-icon="u" >Password </label>
<%= f.password_field :password_confirmation %>
</p>
<center>
<%= f.submit "Update",:class => 'btn-custom' %>
</center>
<div class="back_btn">
<button type="button" class="btn-custom " style="cursor:pointer;">Back</button>
</div>
</div>
<% end %>
</center>
<% if #user.errors.any? %>
<ul class="Signup_Errors">
<% for message_error in #user.errors.full_messages %>
<li><%= message_error %></li>
<% end %>
</ul>
<% end %>
views/users/show.html.erb
<h1>Show your data</h1>
<center>
<ul>
<% #user.each do |t| %>
<li>
<%= t.name %> |
<%= t.email %> |
<%= t.password%> |
<%= t.created_at %>
<%= link_to "edit",users_edit_path(:id => t.id) %> || <%= link_to "Reset Password",users_reset_path(:id => t.id) %>
</li>
<% end %>
</ul>
<div class="back_btn">
<button type="button" class="btn-custom " style="cursor:pointer;">Back</button>
</div>
</center>
controller/users_controller.rb
class UsersController < ApplicationController
def index
end
def new
#user=User.new
end
def create
#user=User.new(users_param);
if #user.save
flash[:notice]="You signed up successfully"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]="You have not signed up successfully"
flash[:color]="invalid"
redirect_to :action => 'new'
end
end
def show
#user=User.all
end
def edit
#user=User.new
end
def update
flash[:notice]=params[:id]
#user=User.find(params[:id])
if #user.update_attributes(update_params)
flash[:notice]="Your data is updated succesfully"
flash[:color]="valid"
redirect_to :action => 'show'
else
flash[:alert]="Your data could not update,Please check it..!!"
flash[:color]="invalid"
redirect_to :action => 'edit'
end
end
def reset
#user=User.new
end
def emailsend
#user=User.find(params[:id])
if #user.email== params[:user][:email]
UserMailer.registration_confirmation(#user).deliver
flash[:notice]="Check your email to reset the password"
flash[:color]="valid"
redirect_to :action => 'reset'
else
flash[:notice]="Check your valid email or your email is not found"
flash[:color]="invalid"
redirect_to :action => 'show'
end
end
def resetpass
#user=User.new
end
def passres
#user=User.find_by_email(params[:user][:email])
if #user.update_attributes(updates_password)
flash[:notice]="Your password id updated succefully"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]="Your data could not update..Please check it..!!"
flash[:color]="invalid"
redirect_to :action => 'show'
end
end
private
def users_param
params.require(:user).permit(:name, :email, :password,:password_confirmation)
end
def update_params
params.require(:user).permit(:name,:email,:password,:password_confirmation)
end
def updates_password
params.require(:user).permit(:email,:password,:password_confirmation)
end
end
users_mailer/registration_confirmation.text.erb
<%= #user.name %>
Thank you for registering!
Edit Your Password <%= link_to "Click Here",users_resetpass_url( :host => "localhost:3000") %>
mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "w5call.w5rtc#gmail.com"
def registration_confirmation(user)
#user = user
mail(:to => user.email, :subject => "Registered")
end
end
config/initializer/setup_mail.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "w5call.w5rtc#gmail.com",
:password => "w5rtc123#",
:authentication => "plain",
:enable_starttls_auto => true
}
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
Inside the email inbox it is showing like the below format.
bapi
Thank you for registering!
Edit Your Password Click Here
But I want only "Click Here" to be shown and when user clicks on that text it should redirect to given link(Change Password).
Please help me to edit this and run it successfully.
Thanks in advance.
You have only provided a text template for your e-mail, and text files (since they're not HTML) can't contain HTML links.
You should provide another template registration_confirmation.text.html.erb (or rename your current template)
From the documentation:
The mail method, if not passed a block, will inspect your views and
send all the views with the same name as the method, so the above
action would send the welcome.text.erb view file as well as the
welcome.text.html.erb view file in a multipart/alternative email.
From the file extension of your email template (.text.erb) you can see that you are sending a plain text email, that is why the link tag shows up in its raw form in the mail client. You should just send the plain URL in the plain text version of your email:
# users_mailer/registration_confirmation.text.erb
<%= #user.name %>
Thank you for registering!
To edit your password, please visit the following URL:
<%= users_resetpass_url( :host => "localhost:3000") %>
You can provide both a plain text and a html version of your email by using the .html.erb extension for the second template:
# users_mailer/registration_confirmation.html.erb
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<p><%= #user.name %></p>
<p>Thank you for registering!</p>
<p>Edit Your Password <%= link_to "Click Here",users_resetpass_url( :host => "localhost:3000") %></p>
</body>
</html>
Also see http://guides.rubyonrails.org/action_mailer_basics.html#walkthrough-to-generating-a-mailer

How to solve ParameterMissing error using ROR

Please help me to solve the below error.
Error:
ActionController::ParameterMissing in CustmersController#create
param is missing or the value is empty: users
When i am submitting the data,this error is coming.
My code is as follows
views/custmers/new.html.erb
<h1>Enter your data here</h1>
<center>
<%= form_for #users,:url => {:action => 'create'} do |f| %>
<% if #users.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#users.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #users.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<label for="name">Name:</label>
<%= f.text_field :name,placeholder:"Enter your name",:class => "input-field" %>
</p>
<p>
<label for="email">Email:</label>
<%= f.email_field :email,placeholder:"Enter your email",:class => "input-field" %>
</p>
<p>
<label for="phone">Phone no:</label>
<%= f.telephone_field :phoneno,placeholder:"Enter your phone number",:class => "input-field" %>
</p>
<p>
<%= f.submit "Submit" %>
</p>
<% end %>
<%= link_to "BACK",custmers_index_path %>
</center>
controller/custmers_controller.rb
class CustmersController < ApplicationController
def index
end
def new
#users=Custmer.new
end
def show
end
def create
#users=Custmer.new(user_params)
if #users.save
flash[:notice]="You have signed up successpully"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]="You have not signed up successfully"
flash[:color]="invalid"
render :new
end
end
private
def user_params
params.require(:users).permit(:name,:email,:phoneno)
end
end
model/custmer.rb
class Custmer < ActiveRecord::Base
EMAIL_REGEX = /\A[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :name,presence:true,length: { minimum: 5 }
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :phoneno, presence: true,length: {minimum: 10}
end
I am using rails version-4.2.0 and ruby version-1.9.3.Please help me to resolve this error.
If you look at the stack trace accompanying your error, you could tell definitely where the problem is -- look for the first line in the stack trace that refers to your code (and not library code).
But a fair guess is the require(:users) line in your controller. It looks like you either copy/pasted this code from another controller, or changed the name of your controller after generating it as part of your scaffold.
It should be requires(:custmer) instead, as that is the class of the thing you're submitting.
As a general approach, you should follow the standard Rails practices for naming things, throughout. If you really want to use the misspelled, Custmer class, have at it, but use #custmr inside your controller and views to refer to an instance, not #users.

RoR undefined method

Ive had a problem for a couple of days. I've tried every solotion thad i found on stackoverflow, but nothing worked...
Rails gives me an error when i try to add a new 'journey' (just a simple datapase entry): undefined method `journeys_index_path' for #<#:0x3d0b888>
Problem resolved!
The issue was the model name. It was Journeys but it should be journey ! The only thing I had to do is rename the modelfile in app/models. (i had to change the filename and the contents, pretty simple!
Oh and the model name is not the same as the database name.
Please check http://rubyonrailsthrissur.wordpress.com/2012/07/18/naming-conventions-while-creating-tables-in-rails/ for more naming rules
Routes.rb:
Fabulam::Application.routes.draw do
root "home#home"
resources :journeys
match 'auth/:provider/callback', to: 'sessions#create', via: 'get'
match 'auth/failure', to: redirect('/'), via: 'get'
match 'signout', to: 'sessions#destroy', via: 'get', as: 'signout'
end
journeys_controller (relevant part)
def index
#current_user ||= User.find(session[:user_id]) if session[:user_id]
#journey = Journeys.where(uid: current_user.uid)
end
def show
#journey = Journeys.find(params[:id])
end
def new
#journey = Journeys.new
end
new.html.erb (location = journeys/new.html.erb)
<!DOCTYPE html>
<html>
<body>
<div>
<h1> Maak hieronder een nieuwe reis aan! </h1>
<% form_for(#journey) do |f| %>
<p>
<%= f.label :name %><br/<
<%= f.text_field :name %>
</P>
<p>
<%f.submit "Dubmittt" %>
</p>
<% end %>
</div>
</body>
</html>
index.html.erb (location journeys/index.html.erb, this one is working fine)
<!DOCTYPE html>
<html>
<body>
<div id="user_nav">
<% if current_user %>
Signed in as <strong><%= current_user.name %></strong>!
<%= link_to "Sign out", signout_path, id: "sign_out" %>
<% else %>
<%= link_to "Sign in with Facebook", "/auth/facebook", id: "sign_in" %>
<% end %>
</div>
<div id="travels">
Hieronder al je reizen!
<% #journey.each do |journey| %>
<h2><%= link_to journey.name, journey %></h2>
<% end %>
</div>
<p><%= link_to "Add a new journey" , new_journey_path %> </P>
</body>
</html>
Rake routes
Prefix Verb URI Pattern Controller#Action
root GET / home#home
journeys GET /journeys(.:format) journeys#index
POST /journeys(.:format) journeys#create
new_journey GET /journeys/new(.:format) journeys#new
edit_journey GET /journeys/:id/edit(.:format) journeys#edit
journey GET /journeys/:id(.:format) journeys#show
PATCH /journeys/:id(.:format) journeys#update
PUT /journeys/:id(.:format) journeys#update
DELETE /journeys/:id(.:format) journeys#destroy
GET /auth/:provider/callback(.:format) sessions#create
auth_failure GET /auth/failure(.:format) redirect(301, /)
signout GET /signout(.:format) sessions#destroy
Am I missing something?
Thanks in advance!

Save record in rails 4.0 using params hash

I'm trying to create a new record in rails using the form_for helper method. I think the params hash is blank because I keep getting blank errors when I submit the form.
This is my form:
<% provide(:title, "Add Department") %>
<h1>Add Department</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#department) do |f| %>
<%= render 'shared/department_error_messages' %>
<%= f.label :Full_Department_Title %>
<%= f.text_field :full_name %>
<%= f.label :Department_Abbreviation %>
<%= f.text_field :abbreviation %>
<%= f.submit "Add department", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
This is my departments controller
class DepartmentsController < ApplicationController
def show
#department = Department.find(params[:id])
end
def new
#department = Department.new
end
def create
#department = Department.new(params[department_params]) # Not the final implementation!
if #department.save
redirect_to root_path
else
render 'new'
end
end
private
def department_params
# This says that params[:department] is required, but inside that, only params[:department][:full_name] and
# params[:department][:abbreviation] are permitted. Unpermitted params will be stripped out
params.require(:department).permit(:full_name, :abbreviation)
end
end
This is the Model:
class Department < ActiveRecord::Base
validates :full_name, presence: true, length: { minimum: 6 }
end
When I submit, errors are rendered saying the full_name can't be blank (and the fields are now empty). The debug info is:
--- !ruby/hash:ActionController::Parameters utf8: ✓
authenticity_token: EjfYjWAzaw7YqVZAkCPZwiEMFfb2YLIRrHbH1CpZOwA=
department: !ruby/hash:ActionController::Parameters
full_name: Sports Department
abbreviation: SD
commit: Add department
action: create
controller: departments
I've also checked the development log. The transaction starts and is then rolled back. I can save a record in the console so I'm guessing its something to do with the params hash, but can't figure it out. Please help
This line:
#department = Department.new(params[department_params])
Should be:
#department = Department.new(department_params)
And, as a minor issue (but not causing this problem), your label tags should look like:
f.label :full_name, "Full Department Title"
This way they are correctly associated with the input.

Resources