Attaching an e-mail form to a controller

こ雲淡風輕ζ 提交于 2019-12-13 04:19:34

问题


I would like to change the below link into an form that posts params of the form to my controller to send an email... the current link works and sends an email...

<%= button_to 'Hello', contact_pages_path, :method => :put %>

In My controller I have:

 def contact
   Contact.contact_form.deliver
 end

My Mailer:

class Contact < ActionMailer::Base
  default from: "****"
  default to: "****"

  def contact_form
    mail(:subject => "Registered")
  end


end

and in my routes I have:

  resources :pages do
  put :contact, :on => :collection
  end

I realise that I need to create a body in the mailer - but I am not sure how to create a form to do this and pass it all on. I did think about creating a model to do this, but I thought having an entire model for just sending an email from a form would be slight over kill?


回答1:


<%= form_tag(contact_pages_path, :method => "post") do %>
  <%= text_field_tag "article", "firstname" %> 
  <%= submit_tag("Search") %>
<% end -%>

When you submit it will go to contact_pages_path and in your controller try params[:article], so its value should be "first name".




回答2:


You can create custom forms using form_tag and then use a text_area_tag to take in the body. As long as you give it a name, it will be sent in the params. Example (using HAML):

= form_tag contact_pages_path, :method => :put
    = text_area_tag "body"
    = submit_tag "Send"

And then in your controller you can access the text in the body with params[:body].

Look here for more information about the text_area_tag (takes in many options you may want to use) and you can also read up more on the form_tag.

This also doesn't require you to make an extra model.




回答3:


try this

In erb file

<%= form_tag(contact_pages_path, :method => "post") do %>
  From : <%= text_field_tag "from_email", "" %> <br/>
  To : <%= text_field_tag "to_email", "" %> <br/>
  Message:<br/>
  <%= = text_area_tag "message" %>
  <%= submit_tag "send" %>
<% end %>

in action

def contact
 from_email = params[:from_email]
 to_email = params[:to_email]
 message = params[:message]

 // do operation to send the mail
end


来源:https://stackoverflow.com/questions/8926992/attaching-an-e-mail-form-to-a-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!