Note that there are some explanatory texts on larger screens.

plurals
  1. PODid I do this correctly - Call an action from controller with link_to
    primarykey
    data
    text
    <p>Please excuse the simplicity and length of this, but I have a little test app that has a users table, with name, email, and salary attributes. I created a mailer that will send out a report of this data to a specific user at my discretion, in other words, when I click a button. My button is created using link_to, and calls an action in my main users_controller, which then calls the mailer action. (I hope that just made sense). It looks like the following and is working just as I hoped; I'm just looking to know if this is the right way to do something like this:</p> <p>In my users_controller (created by the scaffold generator): </p> <pre><code>def sendemail @user = User.find(params[:id]) UserMailer.welcome_email(@user).deliver redirect_to user_path(@user) flash[:notice] = 'Email has been sent!' end </code></pre> <p>In the user_mailer.rb file: </p> <pre><code>def welcome_email(user) @user = user @url = "http://example.com/login" mail(:to =&gt; user.email, :subject =&gt; "Welcome to My Awesome Site") end </code></pre> <p>In the User's show.html.erb page, this is how the email gets sent: </p> <pre><code>&lt;%= link_to "Send Email", sendemail_user_path(@user) %&gt; </code></pre> <p>In my routes.rb file, so that everything executes properly (which it does):</p> <pre><code>resources :users do member do get 'sendemail' end </code></pre> <p>So, having said all this, it works just like it should. I click on the user's show.html.erb page where I would have the data and charts that I want to eventually display, and at my discretion, I can kick out an email to that user with this data, or whatever I put in the mailer.html.erb file. When it is sent, it flashes the message I specified in the controller, and leaves me on that page, just as I specified; so it's working. I just want to know, is this the right and most ruby/railsy way to do things?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload