Using rails mailer how can choose template from sendgrid Legacy Email Template - ruby-on-rails-3

I have created templates in sendgrid, How I need to choose in my rails application while delivering emails
class Notifier < ActionMailer::Base
include SendGrid
...
def welcome_user
mail(:to => 'xxx#gmail.com' , :subject => "Welcome to our website.")
end
end

The Legacy Email Templates doesn't have multiple templates to choose from. If you created multiple templates, you need to use the Template Engine.
For either implementation, you'll want to use the SMTPAPI Gem to build the appropriate SMTPAPI header. You can define which Template Engine template to use in SMTPAPI, as well as activate the Legacy Template app.

Related

How to pull a Rails 3 Twitter feed without a gem

So I am ruby/rails novice and have been looking at: https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline This is the link to the twitter docs for using the 1.1 api. I am clear that I need a twitter app which I have setup, and that I need to set global vars for the key and secret, but then what.
Should I create a controller method in my Pages_controller (the model for static pages)?
How should I code this method to grab my global vars and then create an instance var that includes my tweets?
thanks for the help
You say no gem but much easier if you just use the gem Twitter, put the config in a module if you want. It will look something like this
twitter_client = Twitter::Client.new(
:consumer_key => ENV["CONSUMER_KEY"],
:consumer_secret => ENV["CONSUMER_SECRET"],
:oauth_token => ENV["OAUTH_TOKEN"],
:oauth_token_secret => ENV["OAUTH_SECRET"],
)
In your controller you can then retrieve the tweets with something like
twitter_client.user_timeline('twitter_handle').map(&:attrs)
Any further help just ask

Rails 3: How to get a custom restful member route without ID

I'm working on a project where users can upload videos through a simple form and additionally by FTP to a certain directory and then simply choose the file from the FTP directory instead of uploading it through the form.
I got the following, pretty standard setup for a videos_controller:
# routes.rb
resources :videos
# new.html.rb
form_for(#video) do |f|
...
end
The restful actions in the controller are all working and just standard behaviour. The upload works, that's not the problem. The problem is if I do the following:
# routes.rb
resources :videos do
member do
post :from_ftp
end
end
# new.html.rb
form_for(#video, :url => from_ftp_video_url) do |f|
...
end
I get the error: No route matches {:action=>"from_ftp", :controller=>"videos"}, because the generated route looks like this:
from_ftp_video POST /videos/:id/from_ftp(.:format) videos#from_ftp
which seems right, since it's a member route. But I don't need the :id part of the URL, since I'm creating a new Video object, not through a form but simply by using the file from the FTP directory... So it basically is another create action, that's why I would like to do it as a POST request...
So how do I tackle this the best way?
Although the selected answer is correct for Vapire's situation, it doesn't necessarily answer the title question. If you came here looking for how to get member actions without an ID because you don't need an ID, the answer is a little different.
Say you implemented authentication that sets current_user. You let users edit their own profile only. In that case users/:id/edit doesn't make sense because :id is dictated by the current_user method. In this case /users/edit makes more sense.
You can change your routes.rb file to create member actions without an id in the path.
...instead of this...
resources :user
...use this (note the plurality of resource)...
resource :user
The way to understand member and collection routes is this:
Member routes do something to an object that you have.
Collection routes do something to the set of all objects.
So when we consider what the create route would be, it's a collection route, because it's adding a new object to the collection!
So your from_ftp method should also be a collection route, because it's adding to the collection.
Also, you might want to consider if you can accommodate the FTP functionality within your existing create method - it might be neater.

form_tag or form_for for updating an external API? Rails 3.2

When connecting to an external json api and submitting a form to update a resource is it better to use form_for or form_tag ?
Specifically I'm using the Shopify API http://api.shopify.com/
In config/routes.rb I made default resource routes with resources :variants and now I'm trying to make a form that updates a variant resource but can't configure the form to have the proper action.
==== Update ====
Yes there's a shopify API gem: https://github.com/Shopify/shopify_api that does most of the heavy lifting- just can't quite figure out how to make it work.
To update an #variant object I need to PUT here: PUT /admin/variants/#{id}.json
But I can't quite construct the form_tag properly. I have these routes:
rake routes:
variants GET /variants(.:format) variants#index
POST /variants(.:format) variants#create
new_variant GET /variants/new(.:format) variants#new
edit_variant GET /variants/:id/edit(.:format) variants#edit
variant GET /variants/:id(.:format) variants#show
PUT /variants/:id(.:format) variants#update
DELETE /variants/:id(.:format) variants#destroy
Does your app define the Model? If so, you should be able to use form_for. If not, then you have to use form_tag.
form_for takes a Model instance as parameter (hence the name). Output of form fields is more concise, since form_for can infer a lot about from the model. You can also use fields_for to do nested forms.
If you use form_tag, then you need to write more code to properly construct the HTTP parameters so that Rails can reconstruct the params hash on the server.

How to specify language to be used for Ruby on Rails Mailer

To start with: I am using Ruby On Rails 3.2.x
I have an observer running which triggers emails upon saving orders, two emails to be precise, one email to the seller and one email to the buyer.
All works perfectly fine, the ActionMailer sends out the emails. I have now added multi language support, i.e. the user can set his preferred language in his user settings.
Unfortunately the ActionMailer now sends out the two emails in the language set by the user who triggers the order processing, hence the other party of the order gets the wrong language should it differ from the initiator.
How can I make the ActionMailer use a specific locale/language when sending out an email?
I did come across
<%= I18n.t('daily_mail.hello', :locale => #user.locale) + #user.name %>
but that doesn't work because I have different views for different languages.
Some progress, the solution seems to work and I havent noticed any side effects.
I store the current locale
tmp = I18n.locale
I18N.locale = User.find(uid).language
# send the emails
I18n.locale = tmp
Is this the right way to do it? Are there any possible side effects that I should be aware of?
I18n.with_locale(locale) do
# send the email
end
I found a better way to do it.

Rails 3 controller from plugin

I am creating a Rails 3 plugin and I want to integrate controllers in it that will be automaticly consider by rails as a "normal" controller from the app/controllers folder. How can I do that or what is the best solution for me to have custom controllers from a plugin?
I have found documentations from guides.rubyonrails.org but they have changed the documentation and plugin development doesn't include controllers anymore.
Thanks
You will need to define a class within your plugin that inherits from Rails::Engine. In effect, the feature you want is an engine.
Define the class like this:
lib/your_thing/engine.rb
module YourThing
class Engine < Rails::Engine
end
end
You can then define your engine's controllers at app/controllers within that plugin and for them to work neatly you will also need to define routes for them, which you can do inside config/routes.rb inside the engine like this:
YourThing::Engine.routes.draw do
resources :things
end
Next, you'll need to mount your engine inside your application:
mount YourThing::Engine, :at => "/"
The application then will be able to use routes from your engine.
For more information, I'm in the progress of writing the official Rails Engine guide which you can reference here. Please let me know if you have any further questions and I'll try to answer them in the guide.