0

I'm new to Ruby and obviously RoR. I used a scaffold to create a rails project. For the model I required two fields, num1:integer and num2:integer. The view that was made to create those two fields was

<%= form_for(@calculation) do |f| %>
    .
    .
    .
  <div class="field">
    <%= f.label :num1 %><br>
    <%= f.number_field :num1 %>
  </div>
  <div class="field">
    <%= f.label :num2 %><br>
    <%= f.number_field :num2 %>
  </div>

I've been all over the internet and cannot find how to get the values that are entered into those fields in order to manipulate them. Here is what I have

<div class="actions">
    <%= f.button('Add', &Calculation.add(:num1,:num2)) %>
  </div>

In the above code I am trying to create a button that will run the Calculation.add method with the values from the number_fields as the numbers to add. With what I have I keep getting an error: "undefined method `+' for :num1:Symbol". I'm thinking that the num1 and num2 are just the names of the number_fields and so I need to know how to get the integers that are entered into those fields. Thanks for your help. Oh, and I'm using Ruby 193 and Rails 4.0 with the RubyMine IDE.

EDIT

Here is the model code. I'm sure its ugly for a real rubyist to see:

class Calculation < ActiveRecord::Base
  def self.add (x,y)
    x+y
  end

  def self.subtract(x,y)
    x-y
  end

  def self.multiply(x,y)
    x*y
  end
end
4

2 回答 2

0

服务器端您无法知道用户在用户提交表单之前插入了哪个值。

因此,如果您想在表单提交之前操作数据,您应该实现您的Calculation客户端(阅读:使用 javascript),否则您必须在控制器操作中捕获表单响应并操作用户数据,这些数据将出现在params控制器变量。

于 2014-02-12T21:32:43.367 回答
0

您必须将表单中的数据提交到控制器操作服务器端才能在其上使用 ruby​​ 模型代码。似乎您正在尝试执行客户端行为(javascript)而不是服务器端(Ruby on Rails)

于 2014-02-12T21:24:39.880 回答