Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue getting variable to pass from Controller Action to Mailer template in Ruby on Rails
    primarykey
    data
    text
    <p>I'm having trouble getting a variable filled with text to pass from my controller action "Submit", to my email template.</p> <p>The code listed is to allow you to follow the variable named @message through my app. </p> <p>To start, what I have is a basic "show" view for my model "ECN". It contains some nested information, but that is not relevant. I'm using twitter bootstrap to create a modal form. My <strong>show view</strong> file contains the following:</p> <pre><code>&lt;button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#submitModal"&gt; Submit for Approval &lt;/button&gt; &lt;!-- Submit Modal --&gt; &lt;div class="modal fade" id="submitModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"&gt; &lt;div class="modal-dialog"&gt; &lt;div class="modal-content"&gt; &lt;div class="modal-header"&gt; &lt;button type="button" class="close" data-dismiss="modal" aria-hidden="true"&gt;X&lt;/button&gt; &lt;h4 class="modal-title" id="myModalLabel"&gt;Edit Email&lt;/h4&gt; &lt;/div&gt; &lt;div class="modal-body"&gt; &lt;%= render 'submit_email_fields' %&gt; &lt;/div&gt; &lt;div class="modal-footer"&gt; &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Cancel&lt;/button&gt; &lt;%= button_to "Submit ECN", {action: "submit", :id =&gt; @ecn.id}, {class: "btn btn-primary"} %&gt; &lt;/div&gt; &lt;/div&gt;&lt;!-- /.modal-content --&gt; &lt;/div&gt;&lt;!-- /.modal-dialog --&gt; &lt;/div&gt;&lt;!-- /.modal --&gt; </code></pre> <p>Basically the modal pops up and renders the <strong>_submit_email_fields</strong> file which is shown next:</p> <pre><code>&lt;%= form_tag :action =&gt; 'submit', :id =&gt; @ecn.id %&gt; &lt;p&gt;&lt;label for="email_message"&gt;&lt;/label&gt;&lt;br/&gt; &lt;%= text_area 'email', 'message', value: render('submitted.text.erb'), class: "span6" %&gt;&lt;/p&gt; &lt;%= form_tag %&gt; </code></pre> <p>At this point, I have created a form with a text_area that is populated with a text file "<em>submitted.text.erb</em>". I have a basic email set up, and when the user presses the button "<em>Submit for Approval</em>" in the <em>show</em> view, the modal pops up with a pre-filled form that they can edit before hitting the <strong>Submit ECN</strong> button. The information in the text area is then saved into the parameter email[:message].</p> <p>Next lets look at the <strong>submit action</strong> in my controller:</p> <pre><code> def submit @ecn = Ecn.find(params[:id]) @email = params[:email] @message = @email[:message] @email_list = EmailList.all respond_to do |format| EcnNotifier.submit_engineering(@ecn, @message).deliver format.html { redirect_to home_url, alert: "Ecn has been submitted for approval." } format.json { render json: @ecns } end end </code></pre> <p>This action takes the parameters sets them to instance variables before running my mailer <strong>EcnNotifier</strong> on them. I've verified that the variable @message contains the full text from the form at this point in time. However, the email sends through with no body. Let's look at the mailer method <strong>submit_engineering(@ecn, @message)</strong>:</p> <pre><code>def submit_engineering(ecn, message) @ecn = ecn @message = message @email = EmailList.where(department: "Engineering").all to = [] @email.each do |e| to.push e.email end mail to: to, subject: 'ECN approval', template_name: 'submitted' end </code></pre> <p>This should take my message and continue to use it under the name @message. This mailer send out the template named "<strong>submitted</strong>". This template contains the following:</p> <pre><code>&lt;%= @message %&gt; &lt;h1&gt;TEST&lt;/h1&gt; </code></pre> <p>The template should simply print out the contents of @message, however the email sent is empty except for the TEST test I've added to verify the template was being used correctly. </p> <p>My question is, why is my @message variable not passing correctly from the controller to the final email? Thanks for reading this far and I appreciate your help.</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.
 

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