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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:43:09+00:00 2026-05-24T12:43:09+00:00

I am using Ruby on Rails 3.0.9 and I am trying to implement a

  • 0

I am using Ruby on Rails 3.0.9 and I am trying to implement a “Contact Us” form myself. So…

… in my model file I have:

require 'active_model'

class ContactUs
  include ActiveModel::Conversion
  include ActiveModel::Validations

  attr_accessor :email, :subject, :message

  def initialize(attributes = {})
    @attributes = attributes
  end

  validates :email,
    :presence   => true

  validates :subject,
    :presence   => true

  validates :message,
    :presence   => true


  def persist
    @persisted = true
  end

  def persisted?
    false
  end
end

… in my view file I have:

<%= form_for @contact_us, :url => contact_us_path do |f| %>
  <%= f.text_field :email %>
  <%= f.text_field :subject %>
  <%= f.text_area  :message %>
<% end %>

… in my router file I have:

match 'contact_us' => 'pages#contact_us', :via => [:get, :post]

… in my controller file I have:

class PagesController < ApplicationController
  def contact_us
    case request.request_method

    when 'GET'
      @contact_us = ContactUs.new

    when 'POST'
      @contact_us = ContactUs.new(params[:contact_us])
    end
  end
end

Using the above code, when I submit the form with at least a blank field (I do that in order to make it doesn’t pass validations) and the form is reloaded I don’t get those fields auto-populated. That is, after reloading the form (this happens after pressing the submit button) field values are set all to blank values.

What is the problem? Am I wrong on using the ActiveModel?

  • 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-24T12:43:10+00:00Added an answer on May 24, 2026 at 12:43 pm

    try to replace

    <%= form_for @contact_us, :url => contact_us_path do |f| %>
       <%= f.text_field :email %>
       <%= f.text_field :subject %>
       <%= f.text_area  :message %>
    <% end %> 
    

    with

    <%= form_for @contact_us, :url => contact_us_path do |f| %>
       <%= f.text_field :email, :value => @contact_us.email %>
       <%= f.text_field :subject, :value => @contact_us.subject %>
       <%= f.text_area  :message, :value => @contact_us.message %>
    <% end %> 
    

    EDIT:

    I believe you’ll have to add the mass-assigment functionality yourself, like so:

    def initialize(attributes = {})
      attributes.keys.each do |attr|
        self.class.send(:attr_accessor, attr.to_sym)
        instance_variable_set "@" + attr.to_s, attributes[attr.to_sym]
      end
    end
    

    You can actually skip this line: self.class.send(:attr_accessor, attr.to_sym) if you want to have attr_accessor :email, :subject, :message like you do already

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Ruby on Rails 3.0.7 and I am trying to implement an
I am using Ruby on Rails 3 and I am trying to implement a
I am trying to implement Superfeedr subscriptions using PubSubHubbub and Ruby on Rails. The
I am using Ruby on Rails 3 and I am trying to implement an
I am using Ruby on Rails 3 and and I am trying to implement
I'm trying to write an app using Ruby on Rails and I'm trying to
I'm trying to build a web service using Ruby on Rails. Users authenticate themselves
I am using ruby on rails with a MySQL backend. I have a table
I'm using Ruby on Rails with jQuery and trying to do the following: <%=
I have created a blog application using Ruby on Rails and have just added

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.