我的表单字段在提交时被删除

时间:2010-10-18 13:46:49

标签: ruby-on-rails forms

结果它没有通过验证。

这是我的嵌入式表格:

- form_for [@organization, @referral] do |f|
  = f.error_messages

  = render :partial => 'referral_fields', :locals => { :f => f }

  = f.submit "Submit", :class => "button"


#_referral_fields.html.haml

.grid_7
  .grid_1{:style => "width: 64px;"}
    = f.label :org_name, "Business", :class => "right"
  .grid_4
    = f.text_field_tag :org_name
.grid_7
  .grid_1{:style => "width: 64px;"}
    = f.label :name,  '', :class => "right"
  .grid_4
    = f.text_field_tag :name
.grid_7
  .grid_1{:style => "width: 64px;"}
    = f.label :email, '', :class => "right"
  .grid_2.omega{:style => "width: 114px;"}
    = f.text_field_tag :email, '', :style => "width: 125px;"
  .grid_1.alpha
    = f.label :town, '', :class => "right"
  .grid_2
    = f.text_field_tag :town, '', :style => "width: 100px;"

当我点击提交时,SQL肯定会读取我输入的数据:

Processing ReferralsController#create (for ::1 at 2010-10-18 09:39:07) [POST]
  Parameters: {"name"=>"asdfasd", "commit"=>"Submit", "action"=>"create",   "authenticity_token"=>"/1bwOqHjojde3p0Py08mCrko8xULE4R+eXUvT6qf1cE=", "controller"=>"referrals", "org_name"=>"asdfasdf", "organization_id"=>"1", "town"=>"asdfasd", "email"=>"asdfasd"}

不确定我缺少什么。这是控制器和模型:

#referral.rb
belongs_to :organization, :touch => true
validates_presence_of :org_name, :name, :email, :town  

#referrals_controller.rb

def new
  @referral = Referral.new
  respond_to do |format|
    format.html { render :layout => 'manage' }
  end
end

def create
  @referral = Referral.new(params[:referral])
  if @referral.valid? && @organization.referrals << @referral
    flash[:notice] = "Referrals saved."
    redirect_to new_organization_referrals_path(@organization)
  else
    render :action => :new, :layout => 'manage'
  end
end

1 个答案:

答案 0 :(得分:1)

从查看参数看,您的表单字段设置不正确吗?

您正在使用params [:referral]哈希来构建引用,但我在参数列表中看不到:引用哈希....

您的表单字段应如下所示:

<input name="referral[name]"/>
<input name="referral[town]"/>
<input name="referral[org_name]"/>

等...

然后在您的参数列表中,您应该像{:referral =&gt; {:name =&gt; “foo”,“org_name”=&gt; “bar”,town =&gt; “博伊西”}}