Rails 4 + Devise:错误后保持表单字段填充

时间:2016-04-20 21:53:41

标签: ruby-on-rails forms ruby-on-rails-4 devise

我有一个相当长的用户注册表单,并希望找到一种方法来在错误后保留表单值。我遇到了类似的question,但答案似乎没有用,也许是因为它来自2010年。有没有简单的方法来实现这一目标?这是我的相关控制器代码(主要是Devise):

def create
  if params[:user] && !params[:user][:password]
    params[:user][:password] = "testpassword"
    build_resource(sign_up_params)

    resource.save
    yield resource if block_given?
    if resource.persisted?
      if resource.active_for_authentication?
        set_flash_message! :notice, :signed_up
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      # I believe this is where errors are handled
      clean_up_passwords resource
      set_minimum_password_length
      respond_with resource
      #  render :action => "new" I tried adding this line instead of the line above with no success
    end
  else
    super
  end
end

2 个答案:

答案 0 :(得分:1)

渲染时,你的参数传递给视图。所以,像这样:

f.email_field:email,value:params.include?(:user)? params [:user] [:email]:“”

应该有用。

答案 1 :(得分:0)

只是一个疯狂的猜测:来自sign_up_params的结果哈希是否包含您有额外输入字段的其他用户属性?