简体   繁体   English

Ruby on Rails中的虚拟属性

[英]Virtual attribute in Ruby on Rails

I am a newbie in Rails. 我是Rails的新手。 I am not understanding what I'm doing wrong here. 我不明白我在做什么错。

I have the following model order.rb, with a virtual attribute value: 我有带有虚拟属性值的以下模型order.rb:

    class Order < ActiveRecord::Base
      ACTIONS = %w(buy sell)
      ORDER_TYPES = %w(LMT STP)
      TIFS = %w(DAY GTC OPG IOC)

      attr_accessible :action, :price, :quantity, :symbol, :tif, :order_type, :value

      validates :action, :symbol, :tif, :order_type, presence: true
      validates :price, numericality: { greater_than_or_equal_to: 0.0001 }
      validates :quantity, numericality: { only_integer: true, greater_than: 0 }
      validates :action, inclusion: ACTIONS
      validates :order_type, inclusion: ORDER_TYPES
      validates :tif, inclusion: TIFS

      def value
        price * quantity
      end

      def value= (val)
        self.quantity = val / self.price
      end
    end

This model works fine in rails console, where I can set value and it calculates quantity. 该模型在Rails控制台中可以正常工作,我可以在其中设置值并计算数量。

The problem is when I try to visualize the form. 问题是当我尝试可视化表单时。 app/views/orders/new.html.erb renders the following _form.html.erb: app / views / orders / new.html.erb呈现以下_form.html.erb:

    <%= form_for @order, html: { :class => 'form-inline' } do |f| %>
      <% if @order.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@order.errors.count, "error") %> prohibited this order from being saved:</h2>

          <ul>
          <% @order.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
          <% end %>
          </ul>
        </div>
      <% end %>

      <fieldset>
      <legend><%= params[:action].capitalize %> Order</legend>

      <table class="table">
        <thead>
          <tr>
            <th><%= f.label :action %></th>
            <th><%= f.label :symbol %></th>
            <th><%= f.label :price %></th>
            <th><%= f.label :value %></th>
            <th><%= f.label :order_type %></th>
            <th><%= f.label :tif, "TIF" %></th>
          </tr>
        </thead>

        <tbody>
          <tr>
            <td><%= f.select :action, Order::ACTIONS, {}, class: 'input-small' %></td>
            <td><%= f.text_field :symbol, class: 'input-small' %></td>
            <td><%= f.text_field :price, class: 'input-small' %></td>
            <td><%= f.text_field :value, class: 'input-small' %></td>
            <td><%= f.select :order_type, Order::ORDER_TYPES, {}, class: 'input-small' %></td>
            <td><%= f.select :tif, Order::TIFS, {}, class: 'input-small' %></td>
          </tr>
        </tbody>
      </table>

      <%= f.submit "Submit", class: "btn btn-primary" %>
      <%= f.submit "Clear", class: "btn", type: "reset" %>

      </fieldset>
    <% end %>

I get an error when I open the page in the browser, raised on the line of _form.html.erb with the text field value: 当我在浏览器中打开页面时出现错误,在带有文本字段值的_form.html.erb行上引发:

NoMethodError in Orders#new undefined method `*' for nil:NilClass nil:NilClass的Orders#new未定义方法`*'中的NoMethodError

What am I doing wrong? 我究竟做错了什么? I tried to follow http://railscasts.com/episodes/16-virtual-attributes 我试图遵循http://railscasts.com/episodes/16-virtual-attributes

For the newly built order object price is nil. 对于新建订单,对象价格为零。 So you are getting this error. 因此,您将收到此错误。

Before calculating value make sure that you have price and quantity 在计算价值之前,请确保您具有价格和数量

  def value
    return unless price || quantity
    price * quantity
  end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM