2

I'm trying to make a simple calculator that converts farenheit to celsius, using Ruby on Rails. I keep getting the error "Wrong number of arguments (1 for 2)" after I enter some input value. This app has been alot more difficult than it should be. I've been fixing errors as they come up, but can't figure this one out. Other posts I've read are much different and more elaborate than this simple app I'm trying to make. Thanks for any help.

The controller ...

class CalculatorController < ApplicationController
  def calculate
    @farenheit = params[:temperature]

    unless @farenheit.blank?
      @farenheit = Temperature.calculate({ :farenheit => @farenheit})
    end

    @celsius = (@farenheit - 32) * (5.0 / 9.0)
  end

  private
    ## Strong Parameters 
    def user_params
      params.require(:farenheit)
    end
end

The model...

class Temperature < ActiveRecord::Base
  # attr_accessible :farenheit

  validates_presence_of :farenheit

  validates_numericality_of :farenheit
end

The view...

<h1>Temperature Calculator</h1>

<%= form_tag(calculator_calculate_path, method: "get", action: "calculate") do |form|%>
  <p>Please enter a temperature in degrees Farenheit</p>
  <%= text_field_tag 'temperature', @farenheit %></p>
  <%= submit_tag 'Convert' %>

  <h2>Result: </h2>
  <h3> <% @celsius %> </h3>

<% end %>

The stack trace (first 10 lines)...

activerecord (4.1.6) lib/active_record/relation/calculations.rb:109:in `calculate'
C:in `calculate'
app/controllers/calculator_controller.rb:6:in `calculate'
actionpack (4.1.6) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.6) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.6) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.6) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:113:in `call'
4
  • The application trace is this: app/controllers/calculator_controller.rb:6:in `calculate'. The full trace is too long to post.
    – B. Bulpett
    Oct 12, 2014 at 15:55
  • post the full stacktrace otherwise everything is a speculation. At least post the first 5 lines Oct 12, 2014 at 15:57
  • @PNY Edited post to include head of stack trace
    – B. Bulpett
    Oct 12, 2014 at 16:14
  • 1
    Temperature.calculate the class method calculate, is it something you have defined or are you using Active Record's class method? If you have defined it, post it here as well. Oct 12, 2014 at 16:40

1 Answer 1

4

You've defined an instance method calculate in your CalculatorController, but calculate is also a class method defined in ActiveRecord. It is the ActiveRecord class method that you are calling when you say

@farenheit = Temperature.calculate({ :farenheit => @farenheit})

and that method requires at least two arguments. Here are the docs:

calculate(operation, column_name, options = {}) public

This calculates aggregate values in the given column. Methods for count, sum, average, minimum, and maximum have been added as shortcuts. Options such as :conditions, :order, :group, :having, and :joins can be passed to customize the query.

See http://apidock.com/rails/ActiveRecord/Calculations/ClassMethods/calculate

I'm not sure why you're using the calculate class method though. Why not use only your calculation of @celcius? The following should get your calculate method working without an ArgumentError:

  def calculate
    @farenheit = params[:temperature]

    unless @farenheit.blank?
      @celsius = (@farenheit.to_i - 32) * (5.0 / 9.0)
    end
  end
5
  • So what you're saying is I need to name my calculate method something else, as this is a protected word? That's what I'm understanding after reading the documentation. When I change the name, I get an undefined method error. Or am I missing your point?
    – B. Bulpett
    Oct 12, 2014 at 16:37
  • Sorry, I edited my answer to try and actually... well, provide a solution... rather than just identifying the problem. Let me know if that doesn't help. Oct 12, 2014 at 16:40
  • Thanks, but when I try this solution (which I've tried before), I'm back to getting errors like "undefined method `-' for "23":String" (I entered 23). I understand now why my above code doesn't work, but there must be some way to take the input and run it through the formula. And when I try to convert entry to integer (to_i), it throws a "missing template" error.
    – B. Bulpett
    Oct 12, 2014 at 16:59
  • Contrary to how it may seem, getting a new error is actually good. I mean, it'd be better if it just worked, but a new error means that you've at least solved your original problem. :) Do you have a app/views/calculator/index.html.erb template for Rails to render? Check this out: guides.rubyonrails.org/… Oct 12, 2014 at 17:12
  • Thank you, thohl! The guide is very helpful, much more so than the Oreilly book I'm using. Apparently it just needed a redirect statement to know where to go.
    – B. Bulpett
    Oct 12, 2014 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.