代码之家  ›  专栏  ›  技术社区  ›  Trip

提交时将删除我的表单域

  •  0
  • Trip  · 技术社区  · 14 年前

    结果它没有通过验证。

    这是我的嵌入表单:

    - 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;"
    

    当我单击submit时,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 回复  |  直到 14 年前
        1
  •  1
  •   Gordon Isnor    14 年前

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

    您正在使用params[:referrion]散列来构建引用,但在您的参数列表中没有看到:referrion散列。。。。

    表单字段应如下所示:

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

    等。。。

    然后在参数列表中,您应该是{:referration=>{:name=>“foo”,“org_name”=>“bar”,town=>“Boise”}