Rails的部分验证取决于contorller [英] Rails partial validations depending on conrtoller

查看:86
本文介绍了Rails的部分验证取决于contorller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用devise进行用户注册,这意味着默认情况下,新用户是通过registrations_controller注册的。因此,通过单击注册按钮,用户将被重定向到 new_user_registration路径。

I am using devise for user registrations, meaning, that by default new user is registered through registrations_controller. So by clicking button "Register" user is redirected to a new_user_registration path.

我的注册表单有两个步骤。第一步(new_user_registration),我要求输入名称和密码。在第二步(users_controller)中,保存用户后,我要输入地址。我正在使用向导gem:

My registration form however has two steps. In first step (new_user_registration) I am asking for a name and password. In the second step (users_controller), when user is saved, I am asking for address. This I am doing with wizard gem:

    def create
    @user = User.new(params[:user])
    if @user.save
      session[:user_id] = @user.id
      redirect_to user_steps_path
    else
      render :new
    end
  end

所以,这是一种部分验证,但是我无法根据步骤进行验证,因为我表格的第一部分是通过注册控制器处理的。但是第二部分在users_controller中。这里的情况越来越复杂,我想知道,是否可以根据控制器进行验证。像:

So, those a kind of partioal validations, but I cannnot validate depending in the step, as the first part of my form is handeled through registrations controller. The second part is however in the users_controller. It's getting complicated here and I would like to know, if I can validatede depending on the controller. Like:

validates :first_name, presence: true, if: -> { new_user_registration_path }
validates :last_name, presence: true, if: -> { new_user_registration_path }
validates :street, presence: true, if: -> { new_user_path }

我知道,这样毫无意义,但也许有助于理解我的在后面思考。另外,也许我可以使用smth。像:

I know, like this, it makes no sence, but maybe it helps to understand my thinking behind. Also, maybe I can work with smth. like:

validates :first_name, presence: true, if: -> { @user.save }
validates :last_name, presence: true, if: -> { @user.save }
validates :street, presence: true, if: -> { @user.update}

因此,基本上,在创建新用户时,我会验证名称和密码存在。然后添加地址(这是对用户的更新操作)时,我将检查地址是否存在。

So basically, when creating a new user, I would validate if name and password is present. And when then adding address (it's an update action for user), I will check if address is present. Does anyone has experince with forms like this?

另一个想法,也许是,我可以跳过注册控制器,直接重定向到用户并创建两个用于部分验证的步骤?但是,在进行设计时,我不知道是否可以通过用户控制器跳过new_user_registration路径。我做到了,在邪恶的教程上是如何建议的,但仍然出现在注册控制器中:

Another thought, maybe, I can skip registrations controller, redirecting directly to the user and creating two steps for partial validations? But as I am working with devise, I don't know, if I can just go throught users controller, skipping new_user_registration path. I did it, how it was advised on the wicked tutorial, but still ended up in the registrations controller:

 def create
      super
  end
 def update
      super
  end

谢谢!

推荐答案

此类问题的最佳解决方案是使用表单对象。请参阅此示例宝石改革

The best solution to this type of problem is to use form objects. See this example and the gem Reform.

使用表单对象,每个HTML表单是通过表单对象处理的。在您的示例中,您可以具有用户注册表单和用户更新表单。关键是验证是通过表单而不是模型完成的。这样,验证就与当前表单输入相关,并且避免了对验证规则进行分类的问题。

With form objects, each HTML form is processed via a form object. In your example you could have a "User Registration" form, and a "User update" form. The key point is that the validation is done by the form and not the model. That way the validation is relevant to the current form input, and you avoid the issues of classing validation rules.

这篇关于Rails的部分验证取决于contorller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆