【问题标题】:Rails 4 + Devise: Keep form fields filled after errorRails 4 + Devise:错误后保持表单字段填写
【发布时间】:2016-04-20 21:53:41
【问题描述】:

我有一个相当长的用户注册表单,并希望找到一种方法来在出错后保留表单值。我遇到了类似的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

【问题讨论】:

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


    【解决方案1】:

    渲染时,您的参数正在传递给视图。所以,是这样的:

    f.email_field :email, value: params.include?(:user)?参数[:user][:email] : ""

    应该可以。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 是的,填写的字段在sign_up_params。我该如何使用它们来填充字段?
      猜你喜欢
      • 1970-01-01
      • 2021-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      • 2021-02-25
      相关资源
      最近更新 更多