Ruby on rails Ruby-如何在方法中乘法变量

Ruby on rails Ruby-如何在方法中乘法变量,ruby-on-rails,ruby,ruby-on-rails-4,model-view-controller,methods,Ruby On Rails,Ruby,Ruby On Rails 4,Model View Controller,Methods,我正在使用一种方法尝试将两个变量相乘,如下所示- def total_amount self.quantity.to_i * self.event.price.to_i end 我正在使用RubyonRails构建一个事件应用程序,该方法的目的是允许一个用户为一个付费事件预订多个空间 当我点击付款时,该方法根本不起作用,金额显示为0(零)。该方法在我的预订模型中,在我的预订控制器中,我有以下代码- class BookingsController < ApplicationContr

我正在使用一种方法尝试将两个变量相乘,如下所示-

def total_amount
  self.quantity.to_i * self.event.price.to_i
end
我正在使用RubyonRails构建一个事件应用程序,该方法的目的是允许一个用户为一个付费事件预订多个空间

当我点击付款时,该方法根本不起作用,金额显示为0(零)。该方法在我的预订模型中,在我的预订控制器中,我有以下代码-

class BookingsController < ApplicationController

  before_action :authenticate_user!

  def new
    @event = Event.find(params[:event_id])
    @booking = @event.bookings.new(quantity: params[:quantity])
    @booking.user = current_user
  end

  def create
    @event = Event.find(params[:event_id])
    @booking = @event.bookings.new(booking_params)
    @booking.user = current_user

    Booking.transaction do
      @event.reload
      if @event.bookings.count > @event.number_of_spaces
        flash[:warning] = "Sorry, this event is fully booked."
        raise ActiveRecord::Rollback, "event is fully booked"
      end
    end

    if @booking.save
      # CHARGE THE USER WHO'S BOOKED
      # #{} == puts a variable into a string
      Stripe::Charge.create(
        amount: @event.price_pennies,
        currency: "gbp",
        card: @booking.stripe_token,
        description: "Booking number #{@booking.id}")
      flash[:success] = "Your place on our event has been booked"
      redirect_to event_path(@event)
    else
      flash[:error] = "Payment unsuccessful"
      render "new"
    end

    if @event.is_free?

      @booking.save!
      flash[:success] = "Your place on our event has been booked"
      redirect_to event_path(@event)
    end
  end

  private

  def booking_params
      params.require(:booking).permit(:stripe_token, :quantity)
  end
end
class BookingsController@event.number\u空间
flash[:warning]=“抱歉,此活动已全部预订。”
raise ActiveRecord::回滚,“事件已全部预订”
终止
终止
如果@booking.save
#向已预订的用户收费
##{}==将变量放入字符串中
Stripe::Charge.create(
金额:@event.price_pennies,
货币:“英镑”,
卡:@booking.stripe_代币,
说明:“预订编号#{@Booking.id}”)
flash[:success]=“您在我们活动中的位置已预订”
重定向到事件路径(@event)
其他的
flash[:错误]=“付款未成功”
呈现“新”
终止
如果@event.u是免费的?
@预订。保存!
flash[:success]=“您在我们活动中的位置已预订”
重定向到事件路径(@event)
终止
终止
私有的
def预订参数
参数要求(:预订)。许可证(:条带标记,:数量)
终止
终止
我在events.show表单上有一个输入空间,允许用户输入所需的空间数。预订表有以下代码行,该代码应反映所需的总金额-

<p>Total Amount<%= @booking.total_amount %></p>
总金额


我将
添加到两个变量中,因为没有这个,我收到了一个NilClass错误。我如何修改它,以便该方法创建正确的输出?

如果您正在调用
booking.new
,这意味着您正在创建一个类的实例,其中as
self。
意味着您正在使用一个类变量


删除
self

您可以使用regex来去除您的货币符号

def total_amount
  quantity.to_i * strip_currency(event.price)
end

private

def strip_currency(amount = '')
  amount.to_s.gsub(/[^\d\.]/, '').to_f
end

nil.to_i
为零,因此如果其中任何一个值为零,则乘法将返回零。在我使用的尝试中,两个值都不会为零。我用2英镑表示数量,1.50英镑表示价格。但产出为零。这可能是因为货币符号<代码>'1.50'。to i
为1,
'1.50'。to f
为1.50,
'1.50'。to i
为0。这就是ruby字符串到整数转换的工作原理。您应该使用浮动货币,并在兑换前去掉
,这是否与使用.to\u f那么简单?不,因为有
符号。如果您确实知道字符串的第一个字符将是
,则只需执行
字符串[1..-1]。即可。或者参见i.e.
Monetize.parse('100.01')。如果我错了,请原谅我,我很高兴这样做。但是,在实例方法的上下文中,我相信
self
解析为实例,而不是类。如果在实例方法的上下文中,您希望访问类变量,那么可以执行类似于
self.class.class\u variable
的操作。因此,
quantity*event.price
应该与
self.quantity*self.event.price
相同。再一次,如果我弄错了,请提前道歉。你可能是对的,但我认为这与上面的回答有更多的关系,-符号将输出设置为0(零)。我认为这是主要问题。@jvillian完全正确;在方法前面加上
self.
的唯一不同之处是:1)定义类方法,2)调用self上被同名局部变量遮挡的方法,3)消除self上setter与局部变量赋值之间的歧义,4)调用私有方法(当使用显式接收方调用时将失败,即使该接收方是self…私有setter的情况除外,为避免语法歧义,会对其进行例外)。您好,我已尝试实现上述方法,但出现了“undefined method gsub”错误。我如何更正此错误?我觉得我很快就能得到问题的答案。希望您能够回答。检查您获得的金额值是否为string,或使用
amount将其转换为string。要解决此问题,我是否应该在方法中使用amount我没有将其存储在我的booking或events表中?将amount.to_放入上述方法只会带来语法错误。amount是您传递给此方法的event.price。event.price中有什么值