Sending Email with Ruby - ruby-on-rails

I am a rather green RubyOnRails developer. My apologies in advance for the rather simplistic nature of this question.
I am trying to create a page in my RoR app that allows a user to enter the email addresses of friends/family. When the user hits the 'invite' button at the bottom of the form, the app will send invitation emails to the addresses that are entered.
Since the form doesn't corresponding to a model, I can probably use a simple form helper (i.e. form_tag). Also, I've watched the (very good) railscast on how to set up ActionMailer (# 61) and feel comfortable doing this.
My confusion is how to 'link' the form submission (with the email addresses) to the ActionMailer class(es). According to the form description page, I need to specify a named route in the submit tag.
How can I specify a named route for the ActionMailer class(es) and how do I get the user to an acceptable page (i.e. "thank you for the invitations") after they press the 'invite' button.
Thanks in advance!

The form submission has nothing to do with ActionMailer, directly speaking. Just put your ActionMailer call in an action somewhere, and then point your form at that route.
In routes.rb
match '/send_invite' => 'the_controller#send_invite', :as => :send_invite
Point your form to '/send_invite' (or the send_invite_path helper, which will return that string).
Then in "the_controller", define the method "send_invite", and put your ActionMailer code there. Then render a view with your "thank you for the invites" message.

This railscasts is probably the best for learning to send email.
http://railscasts.com/episodes/61-sending-email

Related

can i use ruby on rails get and post for the same method?

I'm new to web development in general and ruby on rails in specific. I'm working on developing a web interface where i'm using a 'Get' and 'Post' requests on the same method. When i use a get method and send parameters (like username and password), they are being visible in the url. Hence, below is what i did.
form1.html.erb
<%= form_for :modify, :method => "post", :url => {:action => "method2"} do |f|%>
#code here to input username and password
<%=end%>
in my routes.rb i wrote the following routes to the method2:
post 'controller/method2'
get 'controller/method2'
When i enter username and password and click on submit, it is finding the post 'method2' and executing the code in the controller, and displaying method2.html.erb as there is a get request for the same method and also there is a view for method2.
However, i suspect this is not the right way to do it. I do not want the password to be visible. I came to know that i have two options, store the password in a session or send a post request. I do not want to store in session as it is not safe. When i write a post method the page expires when the user tries to come back. To prevent either of these happening, i used the same action in controller as post and get and now i do not see any parameters visible in the url.
Please let me know if this is not the right way to do
If you want a solid method for manipulating user & password, I recommend you go through the Ruby on Rails tutorial, it's an excellent tutorial and it will learn you the basics to start with Rails programming, including a safe username/password use.
Alternatively, you can use Devise, which is a very popular gem for this purpose.
I would not try to implement a secure user/password system without really knowing what you are doing...
In your controller you should have this :
render 'controller/method2'
And you should have a file in this path :
app/views/controller/method2.html.erb
You don't need to have two routes.

Can anyone show me how to make "Mailboxer"'s controller, model, view, and routes?

First of all, thanks in adavance.
This might be so noob question.
I already did setup devise and mailboxer. devise works completely fine!
Then "username" column is added to Users table, and I configured just like this
/config/initializers/mailboxer.rb
#Configures the methods needed by mailboxer
config.email_method = :email
config.name_method = :username
Now I'm totally stucked!! I don't know how to make the rest for simple message system.
What I want is these message function listed below
index(index/received or index/sent)... you can see the list of messages received(from/to, subjectes, and received date are only shown)
show...you can see whole body of the message.
new... message create form, as an option, you can choose 'reply' from "show" page. in this case body text field already includes messages quotes.
Can someone make very simple example for me?
I can pay up to $50 for it via paypal!
I think I need to make
"messages_controller"
"message" model if you needed with mailboxer gem
"view/messages/index", "view/messages/show", "view/messages/new"
"routes" has to be modified.
Thanks
I assume you've resolved your issues by now, but for future reference, a sample app featuring the mailboxer gem is:
https://github.com/RKushnir/mailboxer-app
(not mine, but helpful to get up and going)

Handling forms in rails

I am a little confused with forms in RoR.
I have a contacts page with a corresponding method in my controller. What I am trying to do is have a form so people can leave a message. I will then be emailing that message to myself.
I have the form and everything created. However, I am a little confused on how I would actually get the data from the form once they hit the submit button. would it just be accessible through my contacts method in my controller using params[:message]?
Also, what if I had multiple forms on one page? Would I just be doing params[:message1], params[:message2], etc., in the contacts method in my controller?
Take a look at this answer for details on what exactly the params hash is. When you reference params[:message], this implies that you are POST'ing to your controller action with, say, "message[subject]=abc123", which Rails helpfully turns into a hash with a key like: params[:message]['subject'].
If you're looking to send an email, check out mail_form, which simplifies creating a non-database-backed model that get's turned into an email.
Lastly, about having multiple forms on a page: each form POST's to its action and includes all of the form elements that are children of that form in the DOM. So, only the children of that message will actually be included in the params[:message] hash. You can use fields_for to have multiple models within a single form.

Ruby on Rails: Custom actions

I'm new to rails, so I'll just explain my situation to you:
I've got a User Model and a UsersController. Users log in with their email address and a password. Special users can invite other users, by typing the email address of the invitee into a form and hitting submit. The invited user then receives a mail containing a link to activate his account, by entering his password for the first time.
Here's the problem:
The "invitation" form is mapped to the create action of my UsersController atm. But what do I map the "activation" form to?
Is it possible for me to define a custom action "activate" or something that can be accessed like /users/3/activate (of course, there should be some authentication token here too...) and would activate the user with id 3?
I've found some stuff on custom actions, but I don't quite get the hang of it yet.
Thx for any help
You probably have something like this in your routes file. (If not, please post that.)
resources :users
If you want to add a custom action that acts on a single User, you can add that as a :member like this.
resources :users do
member do
get :activate
end
end
Note that using a get for something that modifies data is a bit wrong, but you're talking about hitting this from a link in an email.
As you play with routes don't forget that rake routes will show you all of the routes that you currently have available to you.

Braintree "shopping cart"-like implementation in rails app / finding previous params[:id]?

I'm trying to integrate Braintree into my rails app which already has a deposits controller, model, and view. Right now basically you can specify an amount but I have its status set to "Pending". I would like it so that the user can make such a deposit but then pay for it at any time using Braintree (ala shopping cart). Would I have to create another controller and/or model to do this? (For example all the Braintree examples I've seen want the payment immediately).
Specifically, I've been trying to just work with the 'deposits' I already have. I put the form for the user's name, credit card info, etc. on the deposits "show" page and a confirm button. This seems to work fine if all fields satisfy validation, however it doesn't when there is an error and renders the show page again.
In DepositsController.rb:
def confirm
#deposit = Deposit.find(params[:id])
#result = Braintree::TransparentRedirect.confirm(request.query_string)
if #result.success?
render :action => "confirm"
else
render :action => "show"
end
end
The problem is that :id now is the Braintree transaction ID, rather than the deposits id (primary key). So of course Deposit.find(params[:id]) can't be found.
What is the best way to implement this? Should I store the previous id somehow or get it another way? Should I be using another controller? Thanks!
Short answer is you should be using a Cart model, connected to this Deposit model from what i can gather here. Based on other questions, however, that feeling could change.
So, solely based on what you wrote above:
If we follow a RESTful approach, you should be creating a Deposit#new for all new deposits.
In your Deposit#create, you would then put all of your logic into the deposit.rb model file. this logic includes, going to Braintree and such.
You say you are working with the deposits you already have, in that case, they should be handled in the Deposit#edit method.
Further questions I would ask of you in this regard, are you using ActiveMerchant? If not, why not?

Resources