-1

我是 Rails 的新手,试图为我的应用程序制作联系表格,但如果没有关联模型,我无法在 Emailer 类中捕获来自联系表格的参数(如姓名和消息)。对此有什么建议吗?这是类和控制器的列表。我的电子邮件类是:

class Contact < ActionMailer::Base
default from: "from@example.com"

# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#   en.contact.send_email.subject
def send_email(contact)
@greeting = "Hi"

mail to: "ostadfree@gmail.com"
end
end

静态页面控制器是:

def email_contact()
  Contact.send_email().deliver
  redirect_to contact_url
end

Contact.html.erb 最后包含一个表单和两个按钮:

 <%= submit_tag "Submit", class: "btn btn-large btn-primary" %>
 <%= link_to 'Send Email', email_contact_path %>

和 send_email.text.erb 是:

Contact#send_email

<%= @greeting %>, find me in app/views/app/views/contact/send_email.text.erb
<%#= "Name :" + @name.to_s %>

谢谢。

4

2 回答 2

4

根据您的代码,您真的不了解 rails 的设计方式。你可能最好遵循教程而不是在这里回答这样的问题。

http://seanrucker.com/simple-ruby-on-rails-contact-form-using-activemodel-and-pony/

于 2013-10-17T19:48:49.770 回答
1

尝试这个:

在邮件中:

def send_email(name,message)
  @greeting = "Hi"
  @name = name
  @message = message
  mail to: "ostadfree@gmail.com"
end

在控制器中:

def email_contact()
  Contact.send_email(params[:name],params[:message]).deliver
  redirect_to contact_url
end

wherenamemessage- 表单字段的名称。如果它之前发送过邮件,那么该代码应该可以工作。

无论如何,检查一下,真的: http: //guides.rubyonrails.org/action_controller_overview.html#parameters

于 2013-10-17T19:52:02.887 回答