Ruby On Rails - 如何使用form_for在其Normal控制器之外的输出更新模型记录?

时间:2013-03-05 21:11:29

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2

我有一个Rails 3.2.11应用程序,我在我的视图中有一个联系表单,名为contact,只是发送了一封电子邮件。逻辑运作良好。现在我们决定使用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])

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分: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中。