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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:09:44+00:00 2026-06-05T04:09:44+00:00

I have the controller home with these two actions: resources :home do collection do

  • 0

I have the controller home with these two actions:

  resources :home do
    collection do
      get 'index'
      get 'contact'      
    end
  end

And the model:

class Home
  include ActiveModel::Validations  
  include ActiveModel::Conversion  
  extend ActiveModel::Naming

  attr_accessor :name, :message

  validates :name, :presence => {:message => 'Name cannot be blank.'}, :allow_blank => true, :length => {:minimum => 2, :maximum => 40}
  validates :message, :presence => {:message => 'Message cannot be blank.'}, :allow_blank => true, :length => {:minimum => 10}

  def initialize(attributes = {})  
      attributes.each do |name, value|  
        send("#{name}=", value)  
      end  
    end  

    def persisted?  
      false  
    end
end

The controller:

class HomeController < ApplicationController
  def index
  end

  def contact
    @home = Home.new
  end
end

And the form (/views/home/contact.html.erb)

  <%= form_for(@home, :validate => true) do |f| %>
    <% if @home.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@home.errors.count, "error") %> prohibited this role from being saved:</h2>
          <ul>
        <% @home.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
        </ul>
      </div>
    <% end %>

    <div class="field">
      <%= f.label :name %><br />
      <%= f.text_field :name %>
    </div>
    <div class="field">
      <%= f.label :message %><br />
      <%= f.text_field :message %>
    </div>
    <div class="actions">
      <%= f.submit %>
    </div>
<% end %>

I would like to validate the form as in the case, if the model has the DB table, but unfortunately I have no experience with the case, that I have a model without DB table and is needed to validate the form… I am still getting the error

undefined method `to_key' for nil:NilClass

Could anyone help me, please, how to make it work?

Thank you

  • 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-06-05T04:09:47+00:00Added an answer on June 5, 2026 at 4:09 am

    I suggest you to watch/read this railscast http://railscasts.com/episodes/219-active-model?language=en&view=asciicast which also explains your exception

    Also I don’t see your create action. Do you have the variable set when the create action fails and renders the form again?

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

Sidebar

Related Questions

I have a Home controller with Index() and Foo(int id) actions and routings like
I have two actions inside my controller (shoppingCartController) public ActionResult Index() { //some stuff
I have written the following code : @Controller @RequestMapping(/test) public class Home { @RequestMapping(value
I have these two routes: routes.MapRoute( Default, // Route name {controller}/{action}/{id}, // URL with
I have a controller called HomeController with the actions Index(), MethodOne() and MethodTwo(). Each
I have these routes: routes.MapRoute( ActionOnly, {action}, new { controller = Home, action =
I have controller PlayerController and actions inside: View , Info , List . So
I have Controller: [MySite]\Controllers\DistributionTools\TrackingChannelsController.cs [HttpPost] public void InitTcFirstPageView() { var model = new TcFirstPageModel
I have @Controller @RequestMapping(value=core/*) public class CoreController { public static String exceptionOccurredView = /core/exceptionOccurred;
I have a controller called articles , which creates the articles model which gets

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.