其他模型的Rails表单 [英] Rails Form for another model
问题描述
我在页面/联系人中有一个表单,想将数据发送到另一个称为ContactForm的模型而不是Pages模型,所以我想这超出了基本约定.看起来像这样:
I have a form in pages/contact and want to send the data to another model called ContactForm instead of Pages model, so i guess this is outside the basic convention.. it looks like this:
<%= form_for @contact_form, url: pages_contact_path, :method => :post do |f| %>
...
...
<% end %>
class PagesController < ApplicationController
def contact
@contact_form = ContactForm.new(contact_form_params)
end
end
我创建了这条路线
post 'pages/contact' => 'pages#contact'
看起来一切正常,但是没有数据记录.在本地服务器上,我可以看到正在处理POST,但未执行任何SQL事务.为什么?
and it all seems to work fine, but no data is recorded. In the local server i can see is processing the POST but is not doing any SQL Transaction.. why??
Started POST "/pages/contact" for ::1 at 2016-06-22 15:58:47 -0300
Processing by PagesController#contact as HTML
Processing by PagesController#contact as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"WIb3jXmkxP0mxRPSPU0Yj050fFwCQOlm9FatCFiGuSrEaoRNXNtmn/w0ZOMtsZsRUwjq4NQweV+d56T5nFxL5Q==", "contact_form"=>{"name"=>"Diego", "phone"=>"", "email"=>"", "subject"=>"", "message"=>"Hoal"}, "commit"=>"Create Contact form"}
参数:{"utf8" =>✓","authenticity_token" =>"WIb3jXmkxP0mxRPSPU0Yj050fFwCQOlm9FatCFiGuSrEaoRNXNtmn/w0ZOMtsZsRUwjq4NQweV = d""=","",电子邮件" =>",主题" =>",消息" =>"Hoal"},提交" =>创建联系表"} 在布局/应用程序中呈现的页面/contact.html.erb(3.0毫秒)
Parameters: {"utf8"=>"✓", "authenticity_token"=>"WIb3jXmkxP0mxRPSPU0Yj050fFwCQOlm9FatCFiGuSrEaoRNXNtmn/w0ZOMtsZsRUwjq4NQweV+d56T5nFxL5Q==", "contact_form"=>{"name"=>"Diego", "phone"=>"", "email"=>"", "subject"=>"", "message"=>"Hoal"}, "commit"=>"Create Contact form"} Rendered pages/contact.html.erb within layouts/application (3.0ms)
非常感谢!
推荐答案
在本地服务器上,我可以看到正在处理POST,但没有执行 任何SQL事务..为什么?
In the local server i can see is processing the POST but is not doing any SQL Transaction.. why?
只要不请求任何事务,就不执行SQL
. contact_form
操作的作用是为变量分配参数,但是不要求对此变量进行验证并将其保存到数据库中.因此,它只是传递给视图...
No SQL
is done as long as no transaction is requested. What your contact_form
action does is assigning parameters to a variable, but this variable is not requested to be validated and saved to your database. So it is simply passed over to the view...
您所感兴趣的是:
@contact_form = ContactForm.new(contact_form_params)
if @contact_form.save
# render success
else
# render errors
end
这篇关于其他模型的Rails表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!