I've been trying to figure this out for over a day now - so hopefully someone can shed some light on this issue for me.
I believe the issues stems from me creating my new model (Subjects) using generate model instead of scaffold. So I've been comparing everything I have to my Users model (which works, and is mostly derived from Hartl's tutorial) to my Subjects model, and I can't find anything.
So I'm trying to add a new subject via the create command, and it's not doing anything. The page even acts like it works before moving to the index. I'm able to add Subjects from the console. Any help would be greatly appreciated.
subjects_controller.rb
class SubjectsController < ApplicationController
def index
@subjects = Subjects.paginate(page: params[:page])
end
def new
@subject = Subjects.new
end
def create
@subject = Subjects.new(subject_params)
if @subject.save
flash[:success] = "Subject added to database."
redirect_to @subject
else
render 'new'
end
end
def show
@subjects = Subjects.find(params[:id])
end
private
def subject_params
params.require(:subject).permit(:subject_id)
end
end
subjects/new.html.erb
<div class="row">
<div class="span6 offset3">
<%= form_for(@subject) do |f| %>
<!-- Look for errors -->
<% if @subject.errors.any? %>
<div id="error_explanation"></div>
<h2>
<%= pluralize(@user.errors.count, "error") %> prohibited this subject from being saved:
</h2>
<ul>
<% @subject.errors.full_message.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<!-- Add new subject to Subjects -->
<p>Subject:</p>
<%= f.text_field :subject_id %>
<%= f.submit "Add Subject", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
subjects.rb
class Subjects < ActiveRecord::Base
attr_accessible :subject_id, :study_site, :treatment_group
validates :subject_id, presence: true, length: { is: 4 }, numericality: { only_integer: true }, presence: true, uniqueness: true
validates :study_site, presence: true, length: { is: 1 }, numericality: { only_integer: true }, presence: true
# validates :subject_id, :study_site, :numericality => { :only_integer => true}
end
EDIT 1.
Error log (localhost/server.log)
Started POST "/subjects_index" for 127.0.0.1 at 2013-11-21 11:59:51 -0800 Processing by SubjectsController#index as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"xqE/7CY2awqNy2E6DainqEKxU70DJ4uGr6hB8+qSmhE=", "subjects"=>{"subject_id"=>"5001", "study_site"=>"3"}, "commit"=>"Add Subject"} Subjects Load (0.4ms) SELECT "subjects".* FROM "subjects" LIMIT 30 OFFSET 0 Rendered subjects/index.html.erb within layouts/application (1.9ms) Rendered layouts/_shim.html.erb (0.0ms) User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = '1a-UA_7SDRyrc7Pd1zKU5g' LIMIT 1 Rendered layouts/_header.html.erb (2.1ms) Rendered layouts/_footer.html.erb (0.2ms) Completed 200 OK in 36.2ms (Views: 35.0ms | ActiveRecord: 0.7ms)
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-11-21 11:59:51 -0800 Served asset /application.css - 304 Not Modified (9ms) [2013-11-21 11:59:51] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Solution:
In case anyone stumbles into the same issue. I ended up renaming every variable to "Subject" from "Subjects". This caused a few problems, because some of those should have actually been "Subjects". So I ran rails generate model Subject, and then updated the new migration file, and then ran bundle exec rake db:migrate, and everything seems to be working now.
Thank you for the help!
Subject
instead ofSubjects
). This might be causing problems with how ActiveRecord infers your database schema. – micahbfrails d model [...]
with the same params you used to generate it to automatically delete all of the files. – micahbfindex
action rather thancreate
. Try specifyingurl: {action: "create"}
on yourform_for
. – micahbf