我有一個表單是搜索表單。當它無法驗證而不是保留字段值時,它們將被刪除,用戶必須重新輸入。我如何保留字段值?Rails 2.3搜索表單在驗證失敗時丟失字段值
sign_in.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&captcha_word=asa&continue.x=-325&continue.y=-810&date%5Bmonth%5D=1&dob%5Bday%5D=1&dob%5Byear%5D=12&user%5Bssn_third%5D=3333&user%5Busername%5D=al" id="sign_in_form" method="post" novalidate="novalidate">
任何想法/建議是 不勝感激!提前致謝!
另外,我正在通過jQuery驗證插件進行客戶端驗證。我沒有在這裏包含代碼,因爲我認爲這不是相關的,但如果有必要,我可以提供。