我已经继承了一个静态网站,在主页面上有一个典型的联系表单,其中包含名称,电子邮件,电话和消息,并且应在提交时向设置的电子邮件地址发送电子邮件。下面的JS和Slim部分给了我。我现在试图使用ActionMailer来连接Rails,但是我对表单参数和/或电子邮件对象的构建做了错误的处理。我不想将它存储在数据库中(尝试使用和不使用模型)。在Rails中配置评论表格
路线: resources :contacts, only: [:new, :create]
_contact_form.slim
(所有我已经改变为action
值)
form#contactform.form-horizontal action="/contacts" method="post" name="contactform" role="form"
/! Field 1
.row
.col-md-6
h3 name:
.input-text.form-group.left-field
input.input-name.form-control name="contact_name" type="text"/
.col-md-6
h3 Phone:
.input-text.form-group
input.input-name.form-control name="contact_phone" type="text"/
/! Field 2
h3 Email:
.input-email.form-group
input.input-email.form-control name="contact_email" type="email"/
/! Field 3
h3 Message:
.textarea-message.form-group
textarea.textarea-message.form-control name="contact_message" rows="7"
/! Button
button.btn.btn-default type="submit"
| Submit
i.flaticon-arrow209
的Javascript:
(我已经设置了url
并添加beforeSend
)
/* --------------------------------------------
Contact Form
-------------------------------------------- */
$(function($) {
$('#contactform').bootstrapValidator({
message: '',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
contact_name: {
validators: {
notEmpty: {
message: ''
}
}
},
contact_email: {
validators: {
notEmpty: {
message: ''
},
emailAddress: {
message: ''
}
}
},
contact_message: {
validators: {
notEmpty: {
message: ''
}
}
}
},
submitHandler: function(validator, form, submitButton) {
$('.contact-form').addClass('ajax-loader');
var data = $('#contactform').serialize();
$.ajax({
type: "POST",
url: "/contacts",
beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))},
data: $('#contactform').serialize(),
success: function(msg) {
$('.contact-form').removeClass('ajax-loader');
$('.form-message').html(msg);
$('.form-message').show();
submitButton.removeAttr("disabled");
resetForm($('#contactform'));
},
error: function(msg) {
$('.contact-form').removeClass('ajax-loader');
$('.form-message').html(msg);
$('.form-message').show();
submitButton.removeAttr("disabled");
resetForm($('#contactform'));
}
});
return false;
},
});
function resetForm($form) {
$form.find('input:text, input:password, input, input:file, select, textarea').val('');
$form.find('input:radio, input:checkbox').removeAttr('checked').removeAttr('selected');
}
});
contact.rb
class Contact
include ActiveModel::Model
include ActiveModel::Conversion
include ActiveModel::Validations
attr_accessor :contact_name, :contact_phone, :contact_email, :contact_message
validates :contact_name, :contact_email, :contact_message, presence: true
end
contacts_controller.rb:
class ContactsController < ApplicationController
def create
@contact = Contact.new(contact_params)
if @contact.valid?
ContactMailer.contact_email(@contact).deliver
redirect_to new_contact_path, notice: "Thank you for your message."
else
flash[:alert] = "An error occurred while delivering this message."
render :new
end
end
private
def contact_params
params.require(:contact).permit(:contact_name, :contact_phone, :contact_email, :contact_message)
end
end
contact_mailer.rb
class ContactMailer < ActionMailer::Base
default to: "[email protected]"
def contact_email(contact)
@contact = contact
mail(from: email: @contact.contact_email, body: @contact.contact_message)
end
end
contact_email.text.slim
| New contact
= @contact.contact_name
= @contact.contact_email
| wrote:
= @contact.contact_message
我目前的@contact
对象上得到一个语法错误contact_mailer.rb
电流输出,但在此之前Rails服务器日志告诉我,Contact
对象未初始化。我试过包括form_for
/form_tag
Rails助手,但这似乎没有帮助。我还为该联系人添加了一个索引操作,在HomeController
的其他地方阅读了此建议。
我可以继续玩'改变错误信息',但我想我得到了一些根本性错误(仍然是一个Rails新手),所以会真正感谢任何指针。我已阅读了ActionMailer文档以及其他所有我能找到的内容。
没有错误消息? –
问题与javascript直接无关。正在删除该标签 – charlietfl