开发者

How to call a custom model from a view?

开发者 https://www.devze.com 2023-02-07 13:36 出处:网络
I do not wish to save anything to a database, so created a model written as this : class Calculation attr_accessor :name, :docket_num, :payments

I do not wish to save anything to a database, so created a model written as this :

class Calculation
  attr_accessor :name, :docket_num, :payments

  def calculate
    ...

I call it in my controller like this :

class MainController < ApplicationController

  def calculator
  开发者_运维知识库  @calc = Calculation.new
  end

In my view as this :

= form_for @calc do |f|
  .field
    = f.label :judgement_balance
    %br/
    = f.text_field :judgement_balance
    ...

But it appears I am doing this wrong, because it return this :

undefined method `model_name' for Calculation:Class

1st Question : Anyone know what I am missing?

2nd Question : How can one transfer the params to a second view and perform a calculation with saving anything to a database?


If you're using rails3:


class Calculation
  extend ActiveModel::Naming

  def name
    "Calculation"
  end
end

> c = Calculation.new
> c.model_name
 => "Calculation"

That should get your form working. I don't understand your second question - you can instantiate a new Calculation object based on the params when the form is submitted and perform the calculation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号