Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9215493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:13:00+00:00 2026-06-18T02:13:00+00:00

I am a newbie in Rails. I am not understanding what I’m doing wrong

  • 0

I am a newbie in Rails. I am not understanding what I’m doing wrong here.

I have the following model order.rb, with a virtual attribute value:

    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.

The problem is when I try to visualize the form.
app/views/orders/new.html.erb renders the following _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:

NoMethodError in Orders#new
undefined method `*’ for nil:NilClass

What am I doing wrong? I tried to follow http://railscasts.com/episodes/16-virtual-attributes

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:13:01+00:00Added an answer on June 18, 2026 at 2:13 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A rails newbie here I have 2 actions in my controller 1) index 2)
Rails newbie here. I have a legacy sqlite3 database that I have no control
Rails newbie here, writing a sample app that has the following three models and
I'm a newbie to rails.I have a model called OfflineExport. In the table I
i´m a newbie to rails and have a problem with my associated object not
I have the following code. I'm still a newbie in Ruby on Rails. As
Ruby newbie here. I'm following the Rails Tutorial and got stuck around 5.3 re:
Ruby newbie here trying to get to grips with rspec, rails, cucumber. I'm not
Rails newbie here struggling with a small project. I am creating a simple ship
I am Rails newbie and having some trouble passing parameters to form. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.