I'd like to use some personnal params in my mail (especially in Devise mailer) :
Here is my DeviseMailer
def confirmation_instructions(record, token, opts={})
mail = super
#agency = Agency.find record.agency_id unless record.agency_id.blank?
#color = color(#agency)
#logo = logo(#agency)
mail.subject = "[#{(#agency.blank?) ? 'Evasion & Découverte' : #agency.name}] Plus qu'une étape pour finaliser votre inscription !"
mail.from = "#{(#agency.blank?) ? 'agence#evasion-et-decouverte.fr' : #agency.email}"
mail
end
But I have an error when the email is send because "#color" or "#logo", or "#agency" are nil
ok try this
def confirmation_instructions(record, token, opts={})
#agency = record.agency # or in your case record can be agency itself I dont know
#color = #agency && #agency.color || SOME_DEFAULT_COLOR_VALUE
#logo = #agency && #agency.logo || SOME_DEFAULT_LOGO
email = #agency && #agency.email || 'agence#evasion-et-decouverte.fr'
opts[:subject] = "[#{#agency.name}] Plus qu'une étape pour finaliser votre inscription !"
opts[:from] = email
super
end
make sure the record #agency = record.agency is trying access is available in database
Related
what I want to do : setup an invite process, when a user invite a new user it creates a user (email and password) and send a welcome email to this new user with a link to reset his password.
My User model
def set_initial_password_reset!
raw, enc = Devise.token_generator.generate(self.class, :reset_password_token)
self.reset_password_token = enc
self.reset_password_sent_at = Time.now.utc
save(validate: false)
#token_reset = raw
end
My InviteController contain
#user_invitee = User.new(email: invite_params[:email]) do |u|
u.password = SecureRandom.hex
# raise
end
#user_invitee.skip_confirmation!
#user_invitee.save
#user_invitee.set_initial_password_reset!
create_invite
if #invite.save!
InviteMailer.new_user_invite(#invite, edit_password_path(#resource = User.find_by(id:
#invite.recipient), reset_password_token: #token_reset)).deliver
redirect_to trip_trip_form(#trip)
when I "raise" into the User model in set_initial_password_reset! to analyze #token_reset, I have got a value, but in the InviteController that value is nil and I don't understand how to grab this value?
I have try other method that I saw on stackoverflow to implement that process like :
User model
def set_initial_password_reset!
self.reset_password_token = Devise.token_generator.generate(self.class, :reset_password_token)
self.reset_password_sent_at = Time.now.utc
save(validate: false)
end
and in InviteController
InviteMailer.new_user_invite(#invite, edit_password_path(#resource = User.find_by(id: #invite.recipient), reset_password_token: #resource.reset_password_token)).deliver
but the token generated was invalid. I should have a token like this : http://localhost:3000/users/password/edit?reset_password_token=i8t77fcdsj3PYRymVdEK
but I get a much longer token.
for info my mailer controller
class InviteMailer < ApplicationMailer
def new_user_invite(invite, edit_password_path)
#invite = invite # Instance variable => available in view
#new_user_registration_url = edit_password_path
mail(to: #invite.email, subject: 'Welcome to Travlr!')
#trip = Trip.find_by(id: #invite.trip_id)
#sender = User.find_by(id: #invite.sender)
end
Thanks for your help!
so the problem with set_initial_password_reset! is the you don't get the raw token value that will be later used to identify the resource in the password reset process.
look at the implementation of reset password token method from devise (below)
def set_reset_password_token
raw, enc = Devise.token_generator.generate(self.class, :reset_password_token)
self.reset_password_token = enc
self.reset_password_sent_at = Time.now.utc
save(validate: false)
raw
end
it saves the encrypted generated token to the user object and returns the raw value.
This raw value then should be included in the mail/link that is going to be used be the recipient (the user) while confirming that indeed it's he/she who can reset the password.
so if we revisit the code samples you've posted the controller would look somewhat like below:
in model: (basically same as the devise method. the only difference is that devise method is private afaik)
def set_initial_password_reset!
raw, enc = Devise.token_generator.generate(self.class, :reset_password_token)
self.reset_password_token = enc
self.reset_password_sent_at = Time.now.utc
save(validate: false)
raw
end
in controller:
#user_invitee = User.new(email: invite_params[:email]) do |u|
u.password = SecureRandom.hex
# raise
end
#user_invitee.skip_confirmation!
#user_invitee.save
token = #user_invitee.set_initial_password_reset!
create_invite
InviteMailer.new_user_invite(#invite, edit_password_path(User.find_by(id: #invite.recipient), reset_password_token: token)).deliver
I am trying save hashes to my database but before I want check request
I am using
require 'net/http'
gem 'http'
This is my controller (hashes I call :hammer)
class PaymentsController < ApplicationController
before_action :logged_in_user, only: [:create]
def create
#payment = current_user.payments.build(payment_params)
aza = ''
uri = URI("https://blockexplorer.com/api/tx/#{:hammer}")
res = Net::HTTP::Post.new(uri)
res1 = res.class.name
aza += Net::HTTP.get(uri)
#go = aza
if aza.include?( '3MGeicHK6P2pUpepsXyTiuA7omMbRZbZx3') #'"addresses":["3MGeicHK6P2pUpepsXyTiuA7omMbRZbZx3"]'
if aza.include? '"value":"0.03072025"'
if aza.include? '"confirmations":0'
flash[:info] = "Wait 15 minutes for confirm"
else
if #payment.save
flash[:success] = "You paid"
redirect_to root_url
else
render 'welcome/index'
end
end
else
flash[:danger] = "You paid less"
end
else
flash[:danger] = "#{res1}"
redirect_to root_url
end
end
def destroy
end
private
def payment_params
params.require(:payment).permit(:hammer)
end
end
When I was try to save it is not check, it is just show error 400
But If I use console it is work
uri = URI("https://blockexplorer.com/api/tx/f484f14ebf9726ab2ab46ffc491786db50fc69ceff737620122e51559a3ea379")
irb(main):003:0> Net::HTTP.get(uri)
I find want can to do
#test = payment_params[:hammer]
# hammer = ''
# hammer += params[:hammer].to_s
aza = ''
uri = URI("https://blockexplorer.com/api/tx/#{#test}")
I think the bug is there:
uri = URI("https://blockexplorer.com/api/tx/#{:hammer}")
in PaymentsController.
Try that instead:
uri = URI("https://blockexplorer.com/api/tx/#{params[:hammer]}")
You missed params[] in your interpolation.
I have this action in my rails controler,
def step_submit
validate_user()
#owning = #user.create_user_car_transaction(Variant.find(params[:variant]), params[:details], params[:address], params[:somethin1])
Contact.user_contact(current_user, params[:contact]) if #user.contact.nil?
redirect_to "/next_step"
end
I use codeClimate to check the quality of the code..
it shows this action's complexity ~ 30 ..
I actually broke a really huge method into this.. how can i still reduce this complexity?
these are the different methods the action calls
def self.user_contact(user, contact_hash = nil)
contact = user.contact || user.create_contact()
contact.update_attributes(contact_hash) if contact_hash.present?
contact
end
def validate_user
if params[:user] && current_user.nil?
user = User.create(params[:user])
sign_in user
end
end
def create_user_car_transaction(car, details_hash, address_hash, coupon_hash = nil)
transaction = self.transactions.create()
car.transaction_item = transaction.transaction_items.create()
car.save
payment_hash = details_hash
payment_hash.merge!(address_hash)
payment = transaction.create_payment(payment_hash)
transaction.update_attributes(:status=>"1") if transaction.status.nil?
transaction
end
I've got omniauth working flawlessly for my rails app on the web. I've also created an API for our iPhone app to interact and I'm trying to get omniauth to work.
Is there a way to pass an access token (received from the integrated iOS integration with the Facebook.app) to omniauth to create the provider entry in the database?
Right now in my web app I have an authentications controller with the following code
def create
omniauth = request.env["omniauth.auth"]
user = User.where("authentications.provider" => omniauth['provider'], "authentications.uid" => omniauth['uid']).first
if user
session[:user_id] = user.id
flash[:notice] = t(:signed_in)
redirect_to root_path
elsif current_user
user = User.find(current_user.id)
user.apply_omniauth(omniauth)
user.save
flash[:notice] = t(:success)
redirect_to root_path
else
session[:omniauth] = omniauth.except('extra')
flash[:notice] = "user not found, please signup, or login. Authorization will be applied to new account"
redirect_to register_path
end
end
In my user controller for the API I created the following:
def create
#user = User.new(params[:user])
#user.save
# Generate data for omni auth if they're a facebook user
if params[:fb_access_token]
graph = Koala::Facebook::API.new(params[:fb_access_token])
profile = graph.get_object('me')
#user['fb_id'] = profile['id']
#user['fb_token'] = params[:fb_access_token]
#user['gender'] = profile['gender']
# Generate omnihash
omnihash = Hash.new
omnihash['provider'] = 'facebook'
omnihash['uid'] = profile['id']
omnihash['info'] = Hash.new
omnihash['info']['nickname'] = profile['username']
omnihash['info']['name'] = profile['name']
omnihash['info']['email'] = profile['email']
omnihash['info']['first_name'] = profile['first_name']
omnihash['info']['last_name'] = profile['last_name']
omnihash['info']['verified'] = profile['verified']
omnihash['info']['urls'] = Hash.new
omnihash['info']['urls']['Facebook'] = profile['link']
omnihash['credentials'] = Hash.new
omnihash['credentials']['token'] = params[:fb_access_token]
omnihash['extra'] = Hash.new
omnihash['extra']['raw_info'] = Hash.new
puts omnihash
# Save the new data
#user.apply_omniauth(omnihash)
#user.save
end
as we all know that html_safe work in controller and view
i am sending some data from user_mailer.rb file and its having some special character like if i wrote &"samarth" so its show &amd"samarth"
the code is
def alert_publication_notification(user, cust_alert, home_url)
load_mailer_settings
#content_type = "text/html"
#subject = ("[abc]- #{cust_alert.alert.name.html_safe} : mail ") due to this line i am getting special character any solution
#from = "sam"
#recipients = user.login
#bcc = Settings.emailid_alerts
#sent_on = Time.now
#customer_alert = cust_alert
#user = user
#home_url = home_url
end
use raw(some_variable) function for RAILS 3 this will not show encoded characters.