0

我有一个 Rails 3.2.11 应用程序,在我的视图中有一个名为联系人的联系表单,它刚刚发送了一封电子邮件。逻辑运作良好。现在我们决定使用 PostgreSQL 数据库将表单的内容保存到 ReceivedEmail 模型中。

我将 form_tag 表单更改为 form_for @received_email 表单。这是我的 form_for 视图:

<%= form_for(@received_email) do |f| %>
  <p class="radio-buttons">
    <span class="label1"><%= f.label "Select one: (required)" %></span>
    <%= f.radio_button :purpose, '1' %><span class="label2"><%= f.label :purpose_order, 'My Order' %></span>
    <%= f.radio_button :purpose, '2' %><span class="label3"><%= f.label :purpose_special, 'Quote For Special Order' %></span>
    <%= f.radio_button :purpose, '3' %><span class="label4"><%= f.label :purpose_wholesale, 'Wholesale Discounts' %></span>
    <%= f.radio_button :purpose, '4' %><span class="label5"><%= f.label :purpose_church, 'Store/Church Discounts' %></span>
    <%= f.radio_button :purpose, '5' %><span class="label6"><%= f.label :purpose_other, 'General Inquiry' %></span>
  </p><br><br>
  <%= f.label "First Name (required)" %><%= f.text_field :first_name, :minlength => 2, :maxlength => 30, placeholder: "First Name (max. 30)" %><br>
  <%= f.label "Last Name (required)" %><%= f.text_field :last_name, :minlength => 2, :maxlength => 30, placeholder: "Last Name (max. 30)" %><br>
  <%= f.label "Email (required)" %><%= f.text_field :email, :minlength => 2, :maxlength => 60, placeholder: "Valid Email Address (max. 60)" %><br>
  <%= f.label "Order Number" %><%= f.text_field :order, :minlength => 2, :maxlength => 20, placeholder: "Order #  (max. 20)" %><span class="contact-required">Required for My Order</span><br>
  <%= f.label "Business / Organization" %><%= f.text_field :company, :minlength => 2, :maxlength => 30, placeholder: "Business/Organization Name (max. 30)" %><span class="contact-required">Required for Wholesale Discounts & Store/Church Discounts</span><br>
  <%= f.label "Business / Organization Website" %><%= f.text_field :website, :minlength => 2, :maxlength => 70, placeholder: "Company Website (max. 70)" %><br>
  <%= f.label "More Info About You" %><%= f.text_area :about_you, :minlength => 10, :maxlength => 255, placeholder: "More Info About You and/or Your Organization (max. 255.)" %>
  <span class="contact-required">Required for Wholesale Discounts & Store/Church Discounts</span><br>
  <%= f.label "Address (required)" %><%= f.text_field :address, :minlength => 2, :maxlength => 50, placeholder: "Street Address (max. 50)" %><br>
  <%= f.label "City (required)" %><%= f.text_field :city, :minlength => 2, :maxlength => 30, placeholder: "City (max. 30)" %><br>
  <%= f.label "State (required)" %><%= f.text_field :state, :minlength => 2, :maxlength => 30, placeholder: "State/Province (max. 30)" %><br>
  <%= f.label "Postal Code / Zip (required)" %><%= f.text_field :zip, :minlength => 2, :maxlength => 15, placeholder: "Postal/Zip (max. 15)" %><br>
  <%= f.label "Country (required)" %><%= f.text_field :country, :minlength => 2, :maxlength => 30, placeholder: "Country (max. 30)" %><br>
  <%= f.label "Message (required)" %><%= f.text_area :message, :minlength => 10, :maxlength => 255, placeholder: "Message or Project Information (max. 255)" %><br>
  <%= f.submit "Submit" %>
<% end %>

我做的下一件事是对我的控制器代码进行微小的更改:

if params[:commit]=='Submit'
  if params[:first_name].blank? || params[:last_name].blank? || params[:address].blank? || params[:city].blank? || params[:state].blank? || params[:zip].blank? || params[:country].blank? || 
     params[:message].blank? || params[:purpose].blank? || params[:email].blank? || params[:email] !~ /\A[\w+\-.]+@[\w\-.]+\.[a-z]+\z/i
    flash.now[:error] = "First Name, Last Name, Address, Email, Address, City, State, Postal Code / Zip, Country and Message are required for all emails.  You must also select an email type."
    render 'contact'
  elsif params[:purpose] == '1' && params[:order].blank?
    flash.now[:error] = "Order Number is required when selecting My Order."
    render 'contact'
  elsif (params[:purpose] == '3' || params[:purpose] == '4') && (params[:company].blank? || params[:about_you].blank?)
    flash.now[:error] = "Business / Organization and More Info About You are required when selecting Wholesale Discounts or Store/Church Discounts."
    render 'contact'
  else
    @received_email = ReceivedEmail.new(params[:received_email])
    if @received_email.save
      begin
        HyperCubeOfLoveMailer.inquiry_email(@received_email).deliver
        flash.now[:success] = "The HyperCube Of Love Team has received your inquiry.  You should receive a confirmation email shortly."
      rescue Net::SMTPFatalError, Net::SMTPSyntaxError, Net::SMTPAuthenticationError => pages_error
        logger.warn pages_error
        flash.now[:notice] = "There was a problem with sending your inquiry to the HyperCube Of Love Team.  Please resubmit your inquiry."
      end   
      redirect_to home_path
    else
      render 'contact'
    end
  end
else
  render 'contact'
end

我将 :received_emails 添加到 config.rb 因为我第一次尝试执行此代码时收到错误消息,指出与模型相关的路由不存在。

下次我尝试执行此代码来创建 ReceiveEmail 记录时,我收到了一个初始化错误,因为它正在寻找 received_emails_controller.rb。

然后我用新视图创建了控制器。在尝试执行此操作后,我得到了一个错误,指出我没有创建视图。然后我创建了创建视图。我只是创建它来看看我最终的结果。它显示了我的创建视图。我知道我不应该做这些事情。

目前唯一的数据库访问权限是为此表单添加记录。我所需要的只是将数据从我的表单移动到记录中并将其添加到模型中。

我对 Rails 命令做了一些检查。我相信我可以为表单上的所有字段做这样的事情来创建记录:

ReceiveModel.create(purpose: params[:purpose], first_name: params[:first_name], etc.)

但是我认为也许我可以以某种方式使用 params[:received_email] 进行某种插入,但我不知道该怎么做或者这是否可能。

ReceiveModel.create(params[:received_email])

任何帮助,将不胜感激。

4

1 回答 1

0

如果我理解正确,控制器代码中已有的结构是处理事情的正确方法:

@received_email = ReceivedEmail.new(params[:received_email])
if @received_email.save
  # Send the e-mail 
else
  # Re-show the page because something didn't work
end

但是,由于 Rails 脚手架支持您要做的 90%,因此您可能希望从rails generate scaffold ContactMessage purpose:integer first_name:string .... 这将为您提供创建和查看联系信息的所有代码。然后您可以将生成的_form.html.erb 替换为您自己的表单,并将您的通知电子邮件代码放在ContactMessageController 中。

于 2013-03-05T21:35:28.963 回答