1

我有一个表格,它是一个搜索表格。当它无法验证而不是保留字段值时,它们将被删除并且用户必须重新输入它们。如何保留字段值?

登录.html.erb:

     <% form_tag( params.merge({ :action => "sign_in" }), :method => "post", :id => "sign_in_form" ) do %>
          <div class="form_item_half">
            <%= build_field_label(:user, :username, "Username") %>
            <%= text_field :user, :username, :class => "text" %>
          </div>

          <div class="form_item_half" onKeypress="return keyRestricted(event)" onKeydown="return keyRestricted(event)">
            <%= build_field_label(:user, :ssn_third, "Last four digits of Social Security Number") %>
            <span id="exes">XXX-XX-</span>
            <%= password_field :user, :ssn_third, :maxlength => "4", :class => 'text securitynum3', :autocomplete => "off" %>
          </div>

          <%= build_field_label(:user, :dob, 'Date of birth') %>
          <div class="padding-bottom10 form_item_full clearfix date">
            <%= select_month 14, :prompt => 'Select Month' %>
            <%= text_field :dob, :day, :size => 2, :value => 'Day', :class => 'text' %>
            <%= text_field :dob, :year, :size => 4, :value => 'Year', :class => 'text' %>
            <div id="date_errors"></div>
          </div>

          <div class="padding-bottom10 form_item_full clearfix">
            <%= render :partial => "/shared/captcha" %>
          </div>

          <div class="mp_buttons clearfix">
            <input type="image" class="button" name="continue"
                   src="/images/v6/buttons/btn_signinsecurely.png" title="SignIn" alt="Sign In Securely">
          </div>
      <% end %>

onboarding_controller.rb:

  def sign_in
    if request.post?
      flash[:errors] = Messages['onboarding/invalid_creds']; return unless @onboarding_user
      flash[:errors] = Messages['onboarding/invalid_creds']; return unless params[:user][:username] == @onboarding_user.username
      flash[:errors] = Messages['onboarding/invalid_creds']; return unless @onboarding_user.same_last_four?(params[:user][:ssn_third])
      flash[:errors] = Messages['general/wrong_captcha']; return unless captcha_match?
      unless
          Date.new(params[:dob][:year].to_i, params[:date][:month].to_i, params[:dob][:day].to_i) == @onboarding_user.dob.to_date
        flash[:errors] = Messages['onboarding/invalid_creds']
        render :sign_in
      end
      session[:onboarding_user] = @onboarding_user
      redirect_to :action => sign_up
    end
  end

如果输入的值与数据库中存储的@onboarding_user 值不匹配,那么我们会闪烁错误消息并呈现:sign_in。但这不会保留字段值...

@onboarding_user 在 before_filter 中加载:

before_filter :load_objects_from_params, :only => :sign_in

   def load_objects_from_params
    @onboarding_user = OnboardingUser.find(params[:id]) unless params[:id].blank?
    @onboarding_user = OnboardingUser.find_by_username(params[:user][:username]) unless @onboarding_user or request.get?
  end

这是控制器看到的参数哈希:

Processing OnboardingController#sign_in (for 127.0.0.1 at 2013-06-18 09:17:17) [POST]
  Parameters: {"authenticity_token"=>"Ij6v3x/QVZQ9pU1JPL433xMnzAJ/I4ndo+nFMCHK1Ok=", "action"=>"sign_in", "captcha_word"=>"asa", "date"=>{"month"=>"1"}, "user"=>{"username"=>"al", "ssn_third"=>"[FILTERED]"}, "continue.x"=>"-325", "continue.y"=>"-810", "controller"=>"onboarding", "dob"=>{"year"=>"12", "day"=>"1"}}

下面是实际为标签生成的 HTML:

<form action="/onboarding/sign_in?authenticity_token=Ij6v3x%2FQVZQ9pU1JPL433xMnzAJ%2FI4ndo%2BnFMCHK1Ok%3D&amp;captcha_word=asa&amp;continue.x=-325&amp;continue.y=-810&amp;date%5Bmonth%5D=1&amp;dob%5Bday%5D=1&amp;dob%5Byear%5D=12&amp;user%5Bssn_third%5D=3333&amp;user%5Busername%5D=al" id="sign_in_form" method="post" novalidate="novalidate">

非常感谢任何想法/建议!提前致谢!

此外,我正在通过 jQuery Validation 插件进行客户端验证。我没有在此处包含代码,因为我认为它不相关,但如有必要,我可以提供。

4

1 回答 1

0

这里实际上存在两个问题:

  1. 在所有验证案例中,我都没有呈现 sign_in 页面:

    flash[:errors] = Messages['onboarding/invalid_creds']; 渲染:sign_in;除非@onboarding_user 否则返回

  2. sign_in.html.erb 中的参数名称必须与正在验证的对象相同。因此,例如,而不是这个:

    “文本”%>

我需要这个:

  <%= text_field :onboarding_user, :username, :class => "text" %>

希望这对将来的人有所帮助!

于 2013-06-21T19:06:01.560 回答