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 6149693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:29:47+00:00 2026-05-23T19:29:47+00:00

I’m trying to use ActiveModel instead of ActiveRecord for my models because I do

  • 0

I’m trying to use ActiveModel instead of ActiveRecord for my models because I do not want my models to have anything to do with the database.

Below is my model:

class User
  include ActiveModel::Validations
  validates :name, :presence => true
  validates :email, :presence => true
  validates :password, :presence => true, :confirmation => true

  attr_accessor :name, :email, :password, :salt
  def initialize(attributes = {})
    @name  = attributes[:name]
    @email = attributes[:email]
    @password = attributes[:password]
    @password_confirmation = attributes[:password_confirmation]
  end
end

And here’s my controller:

class UsersController < ApplicationController
  def new
    @user = User.new
    @title = "Sign up"
  end
end

And my view is:

<h1>Sign up</h1>

<%= form_for(@user) do |f| %>
<div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
</div>
<div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
</div>
<div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password %>
</div>
<div class="field">
    <%= f.label :password_confirmation, "Confirmation" %><br />
    <%= f.password_field :password_confirmation %>
</div>
<div class="actions">
    <%= f.submit "Sign up" %>
</div>
<% end %>

But when I load this view in the browser, I am getting an exception:

undefined method 'to_key' for User:0x104ca1b60

Can anyone please help me with this?

Many thanks in advance!

  • 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-05-23T19:29:48+00:00Added an answer on May 23, 2026 at 7:29 pm

    I went rooting around the Rails 3.1 source to sort this out, I figured that would be easier than searching anywhere else. Earlier versions of Rails should be similar. Jump to the end if tl;dr.


    When you call form_for(@user), you end going through this:

    def form_for(record, options = {}, &proc)
      #...
      case record
      when String, Symbol
        object_name = record
        object      = nil
      else
        object      = record.is_a?(Array) ? record.last : record
        object_name = options[:as] || ActiveModel::Naming.param_key(object)
        apply_form_for_options!(record, options)
      end
    

    And since @user is neither a String nor Object, you go through the else branch and into apply_form_for_options!. Inside apply_form_for_options! we see this:

    as = options[:as]
    #...
    options[:html].reverse_merge!(
      :class  => as ? "#{as}_#{action}" : dom_class(object, action),
      :id     => as ? "#{as}_#{action}" : dom_id(object, action),
      :method => method
    )
    

    Pay attention to that chunk of code, it contains both the source of your problem and the solution. The dom_id method calls record_key_for_dom_id which looks like this:

    def record_key_for_dom_id(record)
      record = record.to_model if record.respond_to?(:to_model)
      key = record.to_key
      key ? sanitize_dom_id(key.join('_')) : key
    end
    

    And there’s your call to to_key. The to_key method is defined by ActiveRecord::AttributeMethods::PrimaryKey and since you’re not using ActiveRecord, you don’t have a to_key method. If you have something in your model that behaves like a primary key then you could define your own to_key and leave it at that.

    But, if we go back to apply_form_for_options! we’ll see another solution:

    as = options[:as]
    

    So you could supply the :as option to form_for to generate a DOM ID for your form by hand:

    <%= form_for(@user, :as => 'user_form') do |f| %>
    

    You’d have to make sure that the :as value was unique within the page though.


    Executive Summary:

    • If your model has an attribute that behaves like a primary key, then define your own to_key method that returns it.
    • Or, supply an appropriate :as option to form_for.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.