I recently came across sending emails via Rails. After watching railcast, it seems that you can write up a template when using Action Mailer. I really liked this feature for my purpose. I also came across Pony, which seems really easy to use.
I was wondering if I can use templates for sending emails via Pony, unless Pony is meant for express non-templated emails.
You can easily access the view framework by explicitly rendering a template:
Pony.mail(
:to => 'somewhere#example.com',
:from => 'sender#other.example.com',
:subject => 'an example',
:body => render_to_string("path/to/_partial", :locals => {foo: #foo}, :layout => false)
)
In my research, Pony seems to be promoted as a non-template based tool, making it "simpler" to use. The home page for the utility does not mention templates at all:
https://github.com/benprew/pony
Related
I'm trying to figure out how to send transactional email from my Rails 4 app.
I have found tutorials for the postmark gem, but I'm struggling to close the gaps between what's assumed in the tutorials (where to do the suggested steps!) and what I know.
I have installed both the ruby and the rails gems in my gemfile:
gem 'postmark-rails', '~> 0.13.0'
gem 'postmark'
I have added the postmark config to my config/application.rb:
config.action_mailer.delivery_method = :postmark
config.action_mailer.postmark_settings = { :api_token => ENV['POSTMARKKEY'] }
I want to try to make and use email templates in postmark.
The instructions in the postmark gem docs say I need to:
Create an instance of Postmark::ApiClient to start sending emails.
your_api_token = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
client = Postmark::ApiClient.new(your_api_token)
I don't know how to do this step? Where do I write the second line? I have my api token stored in my config. I don't know how to make an instance of the postmark api client.
Can anyone point me to next steps (or a more detailed tutorial)?
After you have installed the gems, you need to create a Mailer. I presume you have already configured the API keys etc. in the correct manner, so I will concentrate on actually sending out a templated / static email.
Lets create app/mailers/postmark_mailer.rb file with the following contents.
class PostmarkMailer < ActionMailer::Base
default :from => "your#senderapprovedemail.com>"
def invite(current_user)
#user = current_user
mail(
:subject => 'Subject',
:to => #user.email,
:return => '74f3829ad07c5cffb#inbound.postmarkapp.com',
:track_opens => 'true'
)
end
end
We can then template this mailer in the file app/views/postmark_mailer/invite.html.erb Let's use the following markup to get you started.
<p>Simple email</p>
<p>Content goes here</p>
You can write it like any other .html.erb template using tags, HTML and alike.
To actually send this email out you need to place an action in your controller, in the following manner.
PostmarkMailer.invite(current_user)
Alternatively, if you want this email to be sent upon visiting homepage, it would most likely look like this:
app/controllers/home_controller.rb with content
class HomeController < ApplicationController
# GET /
def index
PostmarkMailer.invite(current_user)
end
end
and corresponsing route
config/routes.rb with content
root :to => 'home#index'
I hope this answers your question.
So, I'm starting an API from a new rails-api project.
I would like to use Devise for all the authentication stuff. I already learned a lot from my recent googling-sessions. I have a working SessionsController, however I noticed with the RegisterController that I certainly missed something about the duo Rails-API+Devise.
I still get the following error :
NameError (undefined local variable or method 'flash' for # <RegistrationsController:0x007ff6022b44b8>)`
From a pure API perspective should I keep working with Devise flash messages since I don't want to render views? I didn't included ActionDispatch::Flash based on the principe that I'll just render JSON. So, is there a way to properly deal with that case?
Thank you.
I would rather suggest to send messages in json instead of having flash[:messages]. If you are not dealing with views then go for json, flash is not required.
Something like:
render :json => {:message => "message", :data => data}
I have a RoR app that tracks the status of values in a database and displays the visualization of this information in the form of charts, graphs, and tables generated by an erb file. This is very handy and I am able to save snapshots of the status' of the DBs by simply saving the page when I open it in a browser. What I would like, however, is for my app to automatically do this saving for me on a nightly basis. I assume this is possible but I'm not having much luck with this so far. Any suggestion on this point would be very helpful.
you can always use render_to_string method to render your erb. For example you need to render show.html.erb for show action in statistics controller:
my_page = render_to_string :controller => 'statistics', :action => 'show', :layout => 'application'
but firstly you should define all it's variables which you use int show view.
#data = Data.last_data
#users = User.active
enter code here
my_page = render_to_string :controller => 'statistics', :action => 'show', :layout => 'application'
snapshot = Snapshot.new :page => my_page
snapshot.save
API: http://apidock.com/rails/ActionController/Base/render_to_string
You can use whenever: https://github.com/javan/whenever
From the readme:
Whenever is a Ruby gem that provides a
clear syntax for writing and deploying
cron jobs.
There is also delayed-job: https://github.com/tobi/delayed_job
From the Readme:
Delayed_job (or DJ) encapsulates the
common pattern of asynchronously
executing longer tasks in the
background.
This is tough one to explain so i'll try my best, and hopefully edit the question if people need more information. I am not providing exact code, but merely an example of the issue.
I am using rails 2.3.8. I am on Unix.
I have a bunch of files under a directory not Apache accessible. (i.e. /data/files/file.rpk)
I have the following in my view.
link_to "RPK File", :controller => 'mycontroller', :action=> 'myaction', :file => '/data/files/file.rpk'
I have the following in my controller.
def myaction
if FileTest.exists?(params[:file])
render :file => params[:file]
end
end
When i select the link on the page i get a download prompt for my desired file, but the name of the file is "myaction" instead of the filename.
Thoughts on how i could get it named correctly?
Sounds like a job for send_file. The x_sendfile option prevents that your workers keep busy while transferring the actual file. You can read more about that in this blogpost.
send_file path_to_file_on_filesystem, :type => "application/zip", :x_sendfile => true
You want to use send_data with the :filename option. See the API documentation.
You want to be extremely careful with this, though. Never ever trust the client/user! They will send file=../../../../etc/group or something in order to read arbitrary files on your system, so be very sure to sanitize that value before passing it to any file-reading methods.
I have a ruby-on-rails application that wishes to utilise the tumblr gem for adding posts when an action is taken (eg: creating a blog post)
I currently have the tumblr gem installed and can manage to fetch my posts using
#tumblruser = Tumblr::User.new('myemail','mypassword')
However when i go to add a post where it asks me to pass the user information like so (according to the API for the gem)
post = Tumblr::Post.create(#tumblruser, :type => 'video', :embed => #post.video_html, :title => #post.title, :caption => #post.content)
it just does not want to authenticate and returns a 403 error
anyone had any experience with this?
NEW SOLUTION:
I have found recently that there has been a problem with the gem. So I have made a copy of it, changed a few things in the docs and code and put it at http://rubygems.org/gems/matenia-tumblr-api
Hope the changes and docs help someone else out there.
As always I welcome any improvements, or refactoring on any of my projects.
Kind Regards,
Matenia
OLD ANSWER BELOW
I managed to get around this by the way ... all i did was declare the username and password in place of #tumblruser like so:
post = Tumblr::Post.create(:email => 'user name email here',
:password => 'my password',
:type => 'video',
:embed => #post.video_html,
:caption => #postcontent)
where #postcontent is the html text of post.content and gsubbed to escape most of the html.
hope this saves someone else some time.
If you are only going to check authentication with any media like Facebook , Twitter ,LinkedIn ,Tumblr , Github and almost 20 others (you can check Here ) .Then omniauth gem is the first thing that comes to mind . Means It's clearly simplest solution for authentication and I love it