title: Binding Rails 4 form with model without table
body: <p>I have a Java background. I used to bind POJO (Plain Old Java Object) classes to forms. POJO's are not associated with any database table.
  I was trying to do the same in Rails. I created following model:</p>
  
  <pre><code>class Email
  
  def initialize
     @to_email = ''
     @name = ''
     @subject = ''
     @message = ''
  end
  
  end
  </code></pre>
  
  <p>And following form:</p>
  
  <pre><code>&lt;h1&gt;Please fill up the form&lt;/h1&gt;
  &lt;%= form_for @email, :url =&gt; {:action =&gt; 'sendemail'} do |f| %&gt;
  &lt;p&gt;
    Name&lt;br/&gt;&lt;%= f.text_field 'name' %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    Email&lt;br/&gt;&lt;%= f.text_field 'to_email' %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    Subject&lt;br/&gt;&lt;%= f.text_field 'subject' %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    Message&lt;br/&gt;&lt;%= f.text_area 'message' %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;%= f.submit "Submit Data" %&gt;
  &lt;/p&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>And here is my controller:</p>
  
  <pre><code>class EmailController &lt; ApplicationController
  
  def capture
     @email = Email.new
  end
  
  def sendemail
     @email = Email.create(email_params)
  
     MandrillMailer.send_email(@email.to_email, @email.name, @email.subject, @email.message).deliver
  end
  
  def email_params
     params.require(:email).permit(:name, :email, :subject, :email)
  end
  
  end
  </code></pre>
  
  <p>The error I get is: <strong>undefined method `model_name' for #</strong></p>
  
  <p>I tried this URL as well but could not get through</p>
  
  <p><a href="http://railscasts.com/episodes/193-tableless-model" rel="nofollow noreferrer">http://railscasts.com/episodes/193-tableless-model</a></p>
  
  <p>In this case my model was:</p>
  
  <pre><code>class Email
  
  def self.columns()
     @columns ||= [];      # || represents concatenation
  end
  
  def self.column(name, sql_type=nil, default=nil, null=true)
     columns &lt;&lt; ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
  end
  
  column :to_email, :string
  column :name, :string
  column :subject, :string
  column :message, :text
  
  end
  </code></pre>
  
  <p>The error in this case is: </p>
  
  <pre><code>undefined method `model_name' for #&lt;Email:0x49f5b40&gt;
  </code></pre>
  
  <p>This is indicating for <code>&lt;%= form_for @email, :url =&gt; {:action =&gt; 'sendemail'} do |f| %&gt;</code> in the view</p>
  
  <p>If I write the model class as: </p>
  
  <pre><code>class Email &lt; ActiveRecord::Base
  </code></pre>
  
  <p>then I get the error:</p>
  
  <blockquote>
    <p>undefined method `type_cast_from_database' for "string":String</p>
  </blockquote>