如何计算表单提交上两个字段的差异 Ruby on Rails

发布于 2024-08-02 16:20:07 字数 160 浏览 5 评论 0 原文

我是 Ruby on Rails 的新手,一直在使用脚手架。在其中一个表单上,我希望能够有两个字段,然后将两个字段之间的差异提交到数据库。

我希望计算(结束金额 - 起始金额)并将其输入数据库的 pickupamount 列,而不是 :pickupamount。

提前致谢!

I am new to Ruby on Rails and have been using Scaffolding. On one of the forms I want to be able to have two fields and then have the difference between the two submitted to the database.

Instead of :pickupamount, I want (Ending Amount - Starting Amount) to be calculated and entered into the pickupamount column of my database.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满栀 2024-08-09 16:20:07

您可以在模型或控制器中执行此操作。与 Skinny Controller Fat Model 一起使用,它可能最好将功能放入您的模型中。查看 ActiveRecord 回调

class MyModel < ActiveRecord::Base
  attr_accessor :start_amount, :end_amount
  before_create :calculate_pickup_amount

  private
    def calculate_pickup_amount
      self.pickupamount = end_amount.to_i - start_amount.to_i
    end
end

然后,在您的控制器中:

def create
  # Assuming params[:my_model] has all the data for initializing a MyModel,
  # including start_amount and end_amount but not pickupamount:
  my_model = MyModel.new(params[:my_model])

  if my_model.save
    # Yay, do something
  else
    # Fail, do something else
  end
end

将以下扩展方法包含到 Ruby 的 String 类中可能会很有用(感谢 sikelianos),可能在 Rails 应用程序 lib 目录中的文件中:

class String
  def numeric?
    Float self rescue false
  end
end

然后您可以在设置 pickupamount 之前执行检查:

def calculate_pickup_amount
  if end_amount.numeric? && start_amount.numeric?
    self.pickupamount = end_amount.to_i - start_amount.to_i
  else
    # Throw exception, set some default value, etc.
  end
end

You could do this in either your model or your controller. Going along with Skinny Controller Fat Model, it might be better to put the functionality in your model. Check out ActiveRecord callbacks.

class MyModel < ActiveRecord::Base
  attr_accessor :start_amount, :end_amount
  before_create :calculate_pickup_amount

  private
    def calculate_pickup_amount
      self.pickupamount = end_amount.to_i - start_amount.to_i
    end
end

Then, in your controller:

def create
  # Assuming params[:my_model] has all the data for initializing a MyModel,
  # including start_amount and end_amount but not pickupamount:
  my_model = MyModel.new(params[:my_model])

  if my_model.save
    # Yay, do something
  else
    # Fail, do something else
  end
end

It might be useful to include the following extension method to Ruby's String class (thanks to sikelianos), perhaps in a file in your Rails app's lib directory:

class String
  def numeric?
    Float self rescue false
  end
end

Then you could perform a check before setting pickupamount:

def calculate_pickup_amount
  if end_amount.numeric? && start_amount.numeric?
    self.pickupamount = end_amount.to_i - start_amount.to_i
  else
    # Throw exception, set some default value, etc.
  end
end
猫七 2024-08-09 16:20:07

在您看来:

form_tag '/some_action' do
    text_field_tag 'start_amount'
    text_field_tag 'end_amount'
end

在您的控制器中:

def create
    model = Model.new
    model.pickupamount = params[:end_amount].to_i - params[:start_amount].to_i
    model.save!
end

In your view:

form_tag '/some_action' do
    text_field_tag 'start_amount'
    text_field_tag 'end_amount'
end

In your controller:

def create
    model = Model.new
    model.pickupamount = params[:end_amount].to_i - params[:start_amount].to_i
    model.save!
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文