如何将两个相同的块组合成一个块?

时间:2021-01-21 12:19:18

How can I use a single block in this:

我如何在这个中使用单个块:

devise_parameter_sanitizer.for(:sign_up) do |u|
  u.permit(:full_name, :email, :password, :password_confirmation)
end
devise_parameter_sanitizer.for(:account_update) do |u|
  u.permit(:full_name, :email, :password, :password_confirmation)
end

2 个解决方案

#1


Looking at the definition of for, you can see that the kind argument is being used as the key to the @blocks hash. Maybe that is a sign that you shouldn't try to "combine" those two statements.

查看for的定义,您可以看到kind参数被用作@blocks哈希的键。也许这表明你不应该试图“结合”这两个陈述。

If you purely want to re-use the codes in the block, just use Proc. So something like:

如果您纯粹想要重新使用块中的代码,只需使用Proc。所以类似于:

block = Proc.new do |u| 
  u.permit(:full_name, :email, :password, :password_confirmation) 
end

devise_parameter_sanitizer.for(:sign_up, block)
devise_parameter_sanitizer.for(:account_update, block)

#2


Proc helps you, Try following,

Proc帮助您,尝试以下,

devise_parameter_sanitizer.for(:sign_up, &your_method)
devise_parameter_sanitizer.for(:account_update, &your_method)

def your_method
  Proc.new { |u| u.permit(:full_name, :email, :password, :password_confirmation) }
end

#1


Looking at the definition of for, you can see that the kind argument is being used as the key to the @blocks hash. Maybe that is a sign that you shouldn't try to "combine" those two statements.

查看for的定义,您可以看到kind参数被用作@blocks哈希的键。也许这表明你不应该试图“结合”这两个陈述。

If you purely want to re-use the codes in the block, just use Proc. So something like:

如果您纯粹想要重新使用块中的代码,只需使用Proc。所以类似于:

block = Proc.new do |u| 
  u.permit(:full_name, :email, :password, :password_confirmation) 
end

devise_parameter_sanitizer.for(:sign_up, block)
devise_parameter_sanitizer.for(:account_update, block)

#2


Proc helps you, Try following,

Proc帮助您,尝试以下,

devise_parameter_sanitizer.for(:sign_up, &your_method)
devise_parameter_sanitizer.for(:account_update, &your_method)

def your_method
  Proc.new { |u| u.permit(:full_name, :email, :password, :password_confirmation) }
end