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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:09:48+00:00 2026-05-31T14:09:48+00:00

I develop RoR app and i met thing which i can’t solve. I have

  • 0

I develop RoR app and i met thing which i can’t solve. I have 2 models – User and Teacher.

class User < ActiveRecord::Base
  ...
  has_one :teacher
  accepts_nested_attributes_for :teacher 
end

class Teacher < ActiveRecord::Base
  ...
  belongs_to :user
end

Also i have view with forms for fill data of User and Teacher. After submit data from this form goes in other method of my controller – create_teacher. Also i have other method new_teacher, in this method i have my view with forms.

class AdminsController < ApplicationController
  def new_teacher
    @user = User.new
    teacher = @user.build_teacher
  end

  def create_teacher
    params[:user][:user_role] = "teacher"
    user = User.new(params[:user])

    if user.valid?
      user.save
      teacher = user.build_teacher( params[:user][:teacher_attributes] )
      if teacher.valid?
        teacher.save
        redirect_to admins_users_of_system_path
        flash[:success] = "Teacher created!"
      else
        redirect_to admins_new_teacher_path
        flash[:error] = teacher.errors.full_messages.to_sentence
        user.destroy
      end
    else
      redirect_to admins_new_teacher_path
      flash[:error] = user.errors.full_messages.to_sentence
    end
  end
end

My view new_teacher.html.erb

<%= form_for @user, :url => create_teacher_url, :html => {:class => "form-horizontal"} do |f| %>
  <%= field_set_tag do %>
    <%= f.fields_for :teacher do |builder| %>
      <div class="control-group">
        <%= builder.label :teacher_last_name, "Last name", :class => "control-label" %>
        <div class="controls">
          <%= builder.text_field :teacher_last_name %>
        </div>
      </div>

      ...
    <% end %>


    <div class="control-group">
       <%= f.label :user_login, "Login", :class => "control-label" %>
        <div class="controls">
          <%= f.text_field :user_login, :value => "" %>
          <%= link_to_function "Generate login", "generate_login()", :class => "btn" %>
        </div>
    </div>
      ...
  <% end %>

    <%= f.submit "Create", :class => "btn btn-large btn-success" %>
<% end %>

I have 2 questions:

1) How can i show all errors (for user and teacher)? Is that possible?

2) Is that possible to keep my data in forms when i redirect to admins_new_teacher_path from create_teacher method?

  • 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-31T14:09:49+00:00Added an answer on May 31, 2026 at 2:09 pm

    I found right solution. For this we should use accepts_nested_attributes_for. Then, when we did this when we create to models

    user = User.new( params[:user] ) # Using nested attributes in our model
    if user.save
       ..
    else
       all_errors = user.errors 
    end
    

    Here we get all errors by user.errors.

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

Sidebar

Related Questions

I develop a dnn module and use public class classNameInfo: IHydratable (c#) Can someone
I have started to think in RoR as an option to develop my applications,
I develop web applications and in my job so far, I have been waging
I develop a variety of applications and have 3-4 miscellaneous libraries that I reuse
I develop a client-server application and I have log in the server, so I
I develop a webservice application in a tomcat container, I have a lot of
To Develop using the Microsoft robotics studio, do I have to use only Microsoft
I develop a lot of frameworks for Flash games and applications. I have always
I'm using Netbeans to develop my RoR project so it is managing the SQL
I develop commercial unmanaged C++ app on Visual Studio 2008, and I want to

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.