ExiRe
ExiRe

Reputation: 4767

Rails 3 - show all errors for has-one belongs-to models via controller

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?

Upvotes: 1

Views: 803

Answers (4)

ExiRe
ExiRe

Reputation: 4767

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.

Upvotes: 0

almai
almai

Reputation: 26

You could e. g. validate your teacher_last_name field in your model:

validate :ensure_teacher_exists

def ensure_teacher_exists
  unless Teacher.find_by_teacher_last_name(teacher_last_name)
    errors.add(:teacher_last_name, "does not exist")
  end
end

Then you have to add in your view:

<%= builder.error_message_on :teacher_last_name %>

This will display the error message near the field.

If you encounter an "undifined method" error add following method to config/initializers/form_builder.rb

def error_message_on(attr)
  message = Error.message(object, attr)
  @template.content_tag(:div, message, :class => 'error_message') if message.present?
end

Upvotes: 0

Arpit Vaishnav
Arpit Vaishnav

Reputation: 4780

on the view page you can use form.object.errors Check the code

For form object f use f.object.errors in side form for form object builder builder.object.errors in side the nested form

And do a render :action => :new_teacher on the controller

To get the object you can write <%=debug(form.object.errors)%> as reference

Upvotes: 2

gayavat
gayavat

Reputation: 19398

def create_teacher
  user_errors, teacher_errors, redirect_path = nil, nil, nil 

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

  if user.save
    redirect_path = admins_users_of_system_path
    flash[:success] = "Teacher created!"
  else
    redirect_path = admins_new_teacher_path
    user_errors = user.errors.full_messages.to_sentence
    teacher_errors = user.teacher.errors.full_messages.to_sentence if user.teacher
  end

  errors_arr = [user_errors, teacher_errors].compact
  flash[:error] = "Errors for user: \n" + errors_arr.join("\n Errors for teacher") if errors_arr.present? 
  redirect_to redirect_path
end

Upvotes: 0

Related Questions