2

I am trying to create a mail to users which includes a simple html form. For this I am using ActionMailer from Rails 3 as suggested in the tutorial at http://edgeguides.rubyonrails.org/action_mailer_basics.html#example-action-mailer-configuration

I am not able to use ActionView helpers like form_tag in the mailer erb file. How do I get access to them in the erb file.

My user.html.erb file has code like this:

<%= form_tag(:controller => "application", :action => "parent_select", :method=>"put") do %>
  Want To Attend
  <%= check_box_tag(:yes, '1', request.priority != 0) %>

<% end %>

I get this error:

undefined method `protect_against_forgery?' for #<#<Class:0xa0874c4>:0xa085a70>
A.K.
  • 3,593
  • 1
  • 17
  • 19
  • 1
    The only helper that fails to work is form_tag as it requires protect_from_forgery? which is implemented only by ActionController and is not available in views from ActionMailer. So I could get around it by hand-generating the html for the form_tag helper. The rest of the helpers (check_box_tag, submit_tag) work fine. – A.K. Jun 15 '11 at 06:12

1 Answers1

0

It's not a good idea to put your form in an email see:

http://www.campaignmonitor.com/blog/post/2435/how-forms-perform-in-html-emai/

But what you are seeing here is the form is being sent without the csrf_meta_tag (form token and auth). You can either turn forgery protection off for this action or add in these fields manually to your form.

MatthewFord
  • 2,918
  • 2
  • 21
  • 32
  • Thanks for the link. Even shutting off forgery protection at the top level does not help since the method protect_from_forgery? is required by form_tag. – A.K. Jun 15 '11 at 06:14