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

  • Home
  • SEARCH
  • 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 6949603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:58:35+00:00 2026-05-27T13:58:35+00:00

I am developing a Rails v2.3.2 app. I have a Model with validations: class

  • 0

I am developing a Rails v2.3.2 app.

I have a Model with validations:

class Person < ActiveRecord::Base 
  validates_presence_of :name, :email

  # Please notice this method
  def pwd=(pwd)
      encrypted_pwd = pwd + randn(127)
      self.password = encrypted_pwd  
  end
end 

NOTE: The above model has attributes: name, email and password (not pwd), where password is not directly get and store to database, instead, the user input pwd value will be encrypted and then store as password in database. In database table, the column(attribute) is password. I use def pwd=(pwd) ... to achieve this.

My Controller:

class PersonsController < ApplicationController
   def create
     @person = Person.new(params[:person])
     if @person.save
       redirect_to persons_path
     else
       render :action=> :new
     end
   end

   def new
     #empty
   end

   def index
     @persons = Person.all
   end
end

The view (views/persons/new.html.erb):

 <% form_for :person, @person, :url => persons_url, :html=>{:method=>:post} do |f| %>
    <table>
      <tr>
        <td>Name:</td>
        <td><%= f.text_field :name %></td>
      </tr>
      <tr>
        <td>Email:</td>
        <td><%= f.text_field :email %></td>
      </tr>
      <tr>
        <td>password:</td>
        <td><%= f.text_field :pwd %></td> <!--NOTE: here I use :pwd not :password-->
      </tr>
    </table>
    <%= f.submit "Save"%>
<% end %>

The interesting & strange thing is , if in the view form, I input values to every field then submit the form, my new instance created successfully.

BUT, if I leave name and email empty, only input password field (which will be pwd value) in the form, then the application got broken, with the error message:

ActionView::TemplateError (undefined method `pwd' for #<Person:0xa0ec0e0>) on line #25 of app/views/persons/new.html.erb:
22:       </tr>
23:       <tr>
24:         <td>password:</td>
25:         <td><%= f.text_field :pwd %></td>
26:       </tr>
27:     </table>

I also tried from Rails console, I can create and save object like:

Person.new(:name=>'John', :email=>'john@gmail.com', :pwd=>'test')

So:

1. Why I can successfully create a model instance if all fields have been filled with values in the form, BUT I got that error when I leave name and email empty but input value to password(pwd) field?

2. Why I do not get error message which complains the name and email should not be empty?

  • 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-27T13:58:36+00:00Added an answer on May 27, 2026 at 1:58 pm

    The reason is your model is missing an attribute reader (getter) for pwd.

    Here’s what’s happening:

    As name and email are required (courtesy of validates_presence_of :name, :email), when you do not enter them and submit the form, an error occurs and the form is rendered again and the view wants to fill the pwd field, but as you haven’t defined it, you get that error message: undefined method 'pwd' for #<Person:0xa0ec0e0>.

    So you can add a getter like this to your Person class:

    def pwd
      "" # or whatever you like
    end
    

    And your form should work again. If you want your model to remember the unencrypted password when an error occurs, you can have something like this instead:

    def pwd=(pwd)
      @pwd = pwd
      encrypted_pwd = pwd + randn(127)
      self.password = encrypted_pwd
    end
    
    def pwd
      @pwd || ""
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a Ruby on Rails app and I have a model called
I am developing a rails app and I have 2 different user's role :
I have started developing a new Rails app on my server using RVM, Rails
I am developing a rails app and have a question. In my routes.rb: map.connect
I am co-developing a simple web app in Rails 3.0.9 and I have realized
i am developing a app and have been for a while using Rails 2.3.4
I am developoing a Rails v2.3.2 app. I have a controller: class SchoolController <
I have a question regarding a simple app I am developing in Rails 3.1,
I have a rails app that I enjoy developing on a sever much more
I've been trying to fix a bug in the Rails app I'm developing, 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.