2

I am writing a Ruby on Rails application revolving around students and teachers

I created a simple Student controller to do basic Create, list actions for student.

This is the listing of the create action in the Student controller:

def create
  @student = Student.new(student_params)
  if @student.save
    redirect_to :action => 'index'
  else
    render :action => 'new'
  end
end

student_params is a private method:

private

    def student_params
      params.require(:student).permit(:teacherid, :firstname, :lastname, :dob, :email, 
                                      :cellphone, :username, :password,
                                      :addr_streetno, 
                                      :addr_aptno, :addr_city, :addr_state,
                                      :addr_zip, :photo)
    end
end

This is what I have in the student model:

class Student < ActiveRecord::Base
    attr_accessor :teacherid, :firstname, :lastname, :dob, :isadult, 
                  :email, :cellphone, :username, :password, :addr_streetno, 
                  :addr_city, :addr_state, :addr_zip, :photo

    scope :sorted, lambda { order("students.firstname ASC")}
end

This is how I have defined the table:

create_table "students", primary_key: "studentid", force: true do |t|
    t.integer  "teacherid",                      null: false
    t.integer  "parentid"
    t.string   "firstname",     limit: 45,       null: false
    t.string   "lastname",      limit: 45,       null: false
    t.datetime "dob",                            null: false
    t.boolean  "isadult",                        null: false
    t.string   "email",         limit: 45,       null: false
    t.integer  "cellphone"
    t.string   "username",      limit: 45,       null: false
    t.string   "password",      limit: 45,       null: false
    t.string   "addr_streetno", limit: 45
    t.integer  "addr_aptno"
    t.string   "addr_city",     limit: 45
    t.string   "addr_state",    limit: 45
    t.integer  "addr_zip"
    t.binary   "photo",         limit: 16777215
end

When I am saving the form for a new student, which is in turn calling the create method in the form_tag, i am getting this error in the browser:

ActiveRecord::StatementInvalid in StudentController#create
Mysql2::Error: Field 'teacherid' doesn't have a default value: INSERT INTO `students` VALUES ()

Extracted source (around line #16):
  14  def create
  15      @student = Student.new(student_params)
  16      if @student.save
  17        redirect_to :action => 'index'
  18      else
  19        render :action => 'new'

Here is the code for the new.html.erb:

<h1>Add A Student</h1>

<%= form_tag(:action=> 'create', :multipart =>true) do %>
<table summary="Student form fields">
   <tr>
     <th>Teacher ID</th>
     <td><%= number_field(:student, :teacherid) %></td>
   </tr>
   <tr>
     <th>FirstName</th>
     <td><%= text_field(:student, :firstname) %></td>
   </tr>
   <tr>
     <th>LastName</th>
     <td><%= text_field(:student, :lastname) %></td>
   </tr>
   <tr>
     <th>Date of Birth</th>
     <td><%= date_field(:student, :dob) %></td>
   </tr>
   <tr>
     <th>Email</th>
     <td><%= email_field(:student, :email) %></td>
   </tr>
   <tr>
     <th>Cellphone</th>
     <td><%= telephone_field(:student, :cellphone) %></td>
   </tr>
   <tr>
     <th>Username</th>
     <td><%= text_field(:student, :username) %></td>
   </tr>
   <tr>
   <tr>
     <th>Password</th>
     <td><%= password_field(:student, :password, size: 20) %></td>
   </tr>
   <tr>
     <th>Address Street#</th>
     <td><%= text_field(:student, :addr_streetno) %></td>
     <th>Apt #</th>
     <td><%= number_field(:student, :addr_aptno) %></td>
   </tr>
   <tr>
     <th>City</th>
     <td><%= text_field(:student, :addr_city) %></td>
     <th>State</th>
     <td><%= text_field(:student, :addr_state) %></td>
     <th>Zip</th>
     <td><%= number_field(:student, :addr_zip) %></td>
   </tr>
   <tr>
     <th>Photo</th>
     <td><%= file_field(:student, :photo) %></td>
   </tr>
</table>

   <%= submit_tag('Create Student') %>
<% end %>
gvermag
  • 389
  • 1
  • 5
  • 12
  • I just want to say that I am entering the required fields in the form that I have made. but some some reason, the sql generated for saving does not show any fields. – gvermag May 17 '14 at 03:24
  • Include the code for the form your posting? It looks like it's missing the `teacherid` field. – James Mason May 17 '14 at 03:24
  • Added the code. The code for teacher id is there. – gvermag May 17 '14 at 13:30

1 Answers1

-1

Try removing the attr_accessor line from your model. You don't need it, and it might be overriding the default setter logic in ActiveRecord::Base.

James Mason
  • 4,246
  • 1
  • 21
  • 26
  • I had got on #rubyonrails IRC channel and someone suggested the same thing. So I removed them and also fixed my data model so that the primary key is "id" and the foreign keys are teacher_id and parent_id (instead of teacherid and parentid). Then I started facing new errors in the form rendering. I will create another thread on that and update the link here. – gvermag May 18 '14 at 03:44
  • here is the link to the current problem http://stackoverflow.com/questions/23718445/undefined-method-while-using-form-for-in-new-controller-method/23718477?noredirect=1#23718477 – gvermag May 18 '14 at 04:22