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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:36:18+00:00 2026-06-14T16:36:18+00:00

What is the right way to create a data model object so that I

  • 0

What is the right way to create a data model object so that I can create a form. When I first create the object it doesn’t have params data with the valid attribute data so the build_ returns an invalid object and rails throws 500 Internal Server Error.

Here’s what I’m looking at:

Models

class Credentials < ActiveRecord::Base
belongs_to user
...
class User < Active ::Base
has_one :credentials, :dependent => :destroy
...

Controller

def new
@cred = current_user.build_credentials 
    render 'edit'
end
    ...

View

<%= form_for(@cred) do |f| %>

Log

Started GET "/credentials" for 127.0.0.1 at 2012-11-16 00:43:26 -0500
Processing by CredentialsController#new as HTML
  [1m[35mUser Load (0.3ms)[0m  SELECT `users`.* FROM `users` WHERE `users`.`remember_token` = 'oWGXC2dmcg2sRyg5mvA6fw' LIMIT 1
  [1m[36mUser Load (0.3ms)[0m  [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1[0m
  [1m[35mCredentials Load (0.4ms)[0m  SELECT `credentials`.* FROM `credentials` WHERE `credentials`.`user_id` = 1 LIMIT 1
  [1m[36m (0.1ms)[0m  [1mBEGIN[0m
  [1m[35m (0.1ms)[0m  COMMIT
  Rendered credentials/edit.html.erb within layouts/application (8.8ms)
Completed 500 Internal Server Error in 100ms

rsec results

undefined method `credentials_index_path' for #<#<Class:...

The credentials model has many verification requirements so the object that is being created by new is invalid (I can confirm this in the console) as the only value that is populated is the foreign key, in this case user_id. However in the console I can establish the @credentials variable and the in the next command assign all the attributes. In the web server the 500 error makes it impossible to follow the same approach.

Update
To make this more clear, the problem is that the @cred = current_user.build_credentials is returning a nil.
I put an raise @cred.inspect call directly after creating @cred to validate that its nil. When I get to the view form_for(@cred)... @cred is just a nil object so it pukes with undefined method 'model_name' for NilClass:Class

  • 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-14T16:36:22+00:00Added an answer on June 14, 2026 at 4:36 pm

    Why Credentials belongs to controller? It should belongs to user, and if you use has_one association, you should use singular noun of credential:

    Model

    class Credential < ActiveRecord::Base
    belongs_to user
    ...
    class User < Active ::Base
    has_one :credential, :dependent => :destroy
    

    Controller

    In your controller, if you want create new object for form, you only need:

    def new
      @cred = current_user.build_credential 
    end
    
    def create
      @cred = current_user.build_credential(params[:cred])
      if @cred.save
        # what you do when creating object success
      else
        # what you do when creating object failed
      end
    end
    

    View

    In your new.html.erb in app/views/credentials/ folder, create your form:

    <%= form_for @cred do |f| %>
    ...
    <% end %>
    

    To link to new.html.erb page, in your view create a link_to:

    <%= link_to 'New Credential', new_credential_path %>
    

    Routes

    If you want to use RESTful route, In your routes.rb, add resources for credential you don’t have:

    resources :credentials
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add an object created from Entity Data Model classes. I have
What is the right way to create custom pgsql types for django application so
What's the right way in Drupal to create reports? I was hoping to use
Is this the right way to return an object from a function? Car getCar(string
I have an object that I am developing a controller for that has many
I have an existing internal data model for a Picture , as follows: package
I have an ASP.NET MVC3 app that features a form with a nested-table input
I have a simple Django-Piston Handler that creates a new instance of a model
I have a ModelForm field that is based on the following Model: class Phrase(models.Model):
I create view for register and send data to controller //app::import('Model','Myprofile'); Class MembersController extends

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.