【问题标题】:Sanitize parameters in model before save in Ruby on Rails在 Ruby on Rails 中保存之前清理模型中的参数
【发布时间】:2021-06-09 08:57:13
【问题描述】:

是否可以对从模型中的控制器传输的参数进行清理?

假设我有这个控制器:

class FooController < ApplicationController

  ...

  def new
    @foo||= Foo.new

    render :action => :new
  end


  def create
    @foo= Foo.new(foo_params)

    if @foo.save
      flash[:notice] = 'Foo created'
      redirect_to foo_path(@foo.id)
    else
      new
    end
  end

  ...

  private

  def foo_params
    params.require(:name, :bars => [])
  end
end

还有这个模型:

class Foo < ApplicationRecord

end

在这个模型中,我想清理bars 数组中的参数。 bars 数组包含字符串,但这些字符串只是数字。我想将它们转换为整数。我该怎么做?

感谢所有答案。

【问题讨论】:

    标签: ruby-on-rails ruby activerecord ruby-on-rails-6 activemodel


    【解决方案1】:

    使用 before_save 回调,您可以调用一个方法将字符串转换为整数:

    class Foo < ApplicationRecord
      before_save :sanitize_bars
    
      def sanitize_bars
        self.bars = bars.reject(&:empty?).map(&:to_i)
      end
    end
    

    【讨论】:

      【解决方案2】:

      在保存@foo 之前,您可以像这样转换bars 数组

      def create
      @foo= Foo.new(foo_params)
      
      # this converts array of strings into integers
      @fou.bars = @fou.bars.map(&:to_i)
      
      if @foo.save
        flash[:notice] = 'Foo created'
        redirect_to foo_path(@foo.id)
      else
        new
      end
      

      结束

      【讨论】:

        【解决方案3】:

        以下是改进后的答案。这也会拒绝空字符串。

        class Foo < ApplicationRecord
          before_save :sanitize_bars
        
          def sanitize_bars
            self.bars = bars.reject(&:empty?).map(&:to_i)
          end
        end
        

        【讨论】:

        • 谢谢。完美运行!
        【解决方案4】:

        另一种方法是在模型中为此参数定义属性编写器:

        class Foo < ApplicationRecord
          def bars=(value)
            super(value.map(&:to_i))
          end
        end
        

        它也很容易测试:

        foo = Foo.new(bars: ["1", "2"])
        expect(foo.bars).to eq [1, 2]
        

        【讨论】:

        • 也是一个非常好的解决方案。谢谢。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-24
        • 1970-01-01
        • 2012-04-01
        • 1970-01-01
        • 2018-04-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多