2

I'm having trouble getting a variable filled with text to pass from my controller action "Submit", to my email template.

The code listed is to allow you to follow the variable named @message through my app.

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 show view file contains the following:

<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#submitModal">
  Submit for Approval
</button>

<!-- Submit Modal -->
<div class="modal fade" id="submitModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">X</button>
        <h4 class="modal-title" id="myModalLabel">Edit Email</h4>
      </div>
      <div class="modal-body">
        <%= render 'submit_email_fields' %>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        <%= button_to "Submit ECN", {action: "submit", :id => @ecn.id}, {class: "btn btn-primary"} %>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

Basically the modal pops up and renders the _submit_email_fields file which is shown next:

<%= form_tag :action => 'submit', :id => @ecn.id %>
<p><label for="email_message"></label><br/>
<%= text_area 'email', 'message', value: render('submitted.text.erb'), class: "span6" %></p>
<%= form_tag %>

At this point, I have created a form with a text_area that is populated with a text file "submitted.text.erb". I have a basic email set up, and when the user presses the button "Submit for Approval" in the show view, the modal pops up with a pre-filled form that they can edit before hitting the Submit ECN button. The information in the text area is then saved into the parameter email[:message].

Next lets look at the submit action in my controller:

  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

This action takes the parameters sets them to instance variables before running my mailer EcnNotifier 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 submit_engineering(@ecn, @message):

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

This should take my message and continue to use it under the name @message. This mailer send out the template named "submitted". This template contains the following:

<%= @message %>
<h1>TEST</h1>

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.

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.

sabrams
  • 1,128
  • 2
  • 15
  • 28
  • 1
    have you ever used a debugger before? if you have put it in your controller action and see if there's anything coming in params[:email][:message] – Mandeep Nov 22 '13 at 05:49
  • 1
    Just to add to that, use raise params.inspect in the controller. – Shaun Frost Duke Jackson Nov 22 '13 at 11:21
  • I have not used a debugger before, but the raise params.inspect did show that the params[:email][:message] still contained my text. I was able to follow it through and find the issue. Thanks! – sabrams Nov 22 '13 at 14:09
  • Can you explain the answer below? I'm having the exact same issue of an instance variable not getting passed to the mailer view. – Trevor McKendrick Nov 25 '13 at 20:13
  • I honestly don't know what changed. If i compare my working code with the code posted above, they are identical. I guess it was just restarting the server, or perhaps I changed something without realizing it. If you post your code in a new question, I can take a look at it. – sabrams Nov 25 '13 at 21:16

0 Answers0