2013-03-05 43 views
0

我有一個Rails 3.2.11應用程序,我在我的視圖中有一個聯繫表單,名爲contact,它只是發送一封電子郵件。邏輯運作良好。現在我們決定使用PostgreSQL數據庫將表單的內容保存到ReceivedEmail模型中。Ruby On Rails - 如何使用form_for在其正常控制器之外的輸出更新模型記錄?

我將我的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+\-.][email protected][\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]) 

任何幫助,將不勝感激。

回答

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中。

+0

正常情況下,我需要添加,更改,刪除或查看記錄時,使用Rails處理進行數據庫維護。我想要做的就是添加一個我很少會看到的記錄。我已經有了工作代碼,我想對其進行一些修改,以便從表單數據創建記錄。如果可能的話,我不想重寫我的代碼並創建另一個控制器,特別是因爲我想要做的就是添加一條記錄。我正在詢問我可能不會在其他應用程序中使用的異常代碼。 – 2013-03-05 21:48:47

+0

好的。據我所知,控制器中的代碼應該做你想做的事(從表單中加載它的成員值)。這似乎沒有發生? – 2013-03-05 23:49:03

+0

丹,它沒有工作,因爲它期望我的模型的代碼在第二個控制器,我試圖避免創建。我已決定採納你的建議併爲此創建第二個控制器,儘管它沒有直接回答我的問題。如果我們對某個問題提出質疑,那麼添加該模型的目的就是爲了合法目的。如果需要,做第二個控制器將開始能夠訪問電子郵件數據的過程。聯繫表格將成爲新的方法。稍後我會編寫其他方法。 – 2013-03-07 16:22:28

相關問題