我有一个搜索表单。当它无法验证而不是保留字段值时,它们将被删除,用户必须重新输入它们。如何保留字段值?
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 Validation插件进行客户端验证。我没有在这里包含代码,因为我认为它不相关,但如果有必要,我可以提供它。
答案 0 :(得分:0)
这里实际上有两个问题:
我没有在所有验证案例中呈现sign_in页面:
flash [:errors] =消息['onboarding / invalid_creds'];渲染:sign_in;返回,除非@onboarding_user
sign_in.html.erb中的参数名称必须与要验证的对象相同。 所以,例如,而不是:
“text”%&gt;我需要这个:
<%= text_field :onboarding_user, :username, :class => "text" %>
希望这可以帮助将来的某个人!