0

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!

Ian Ellis
  • 541
  • 6
  • 19
  • Are there any errors in the server log? Also, models should be named singuarly (`Subject` instead of `Subjects`). This might be causing problems with how ActiveRecord infers your database schema. – micahbf Nov 21 '13 at 19:50
  • Thank you for the reply. From the server log I see: 304 Not Modified ... WARN Could not determine content-length of response body. As for the Subject instead of Subjects, would you recommend I just rollback and remake the model, or is there an easier way to rename it to 'Subject'? – Ian Ellis Nov 21 '13 at 19:57
  • That error is not the one, are you sure there are no others? As far as the model goes, yes, rolling back and regenerating may be easiest. You can use `rails d model [...]` with the same params you used to generate it to automatically delete all of the files. – micahbf Nov 21 '13 at 20:02
  • I'm not SURE there are no others, but I don't see any. Both comments so far mention that Subjects should be singular, so I'll go ahead and spend some time fixing that. Thanks again. – Ian Ellis Nov 21 '13 at 20:06
  • The problem seems to be that the form is getting handled by the `index` action rather than `create`. Try specifying `url: {action: "create"}` on your `form_for`. – micahbf Nov 21 '13 at 21:56

2 Answers2

1

instead of changing variable names at places, you could also consider changing path in routes.rb

If you have mentioned :as => 'Subjects' there, then you might be getting this error. This can be fixed by changing all variable names as well, but this would be easier.

Hardik
  • 764
  • 5
  • 10
0

I can't pinpoint the exact problem but I can recommend the following change which might fix the issue:

Rename your model class Subject (singular).

The rails convention is to name you models singular, controllers plural (SubjectsController).

Also I'm not sure if you need attr_accessible if you are using strong parameters.

Can you post which error message you get? (Server log)

wpp
  • 7,093
  • 4
  • 33
  • 65
  • Thank you for the reply. I'm in the process of renaming my model to Subject. I updated my post to include the error message. For the moment I'll assume that at least some of my problem stems from having a plural name for my model class, so I'll work on fixing that. – Ian Ellis Nov 21 '13 at 20:07
  • No problem, be sure to have a look here for instructions on [renaming a model](http://stackoverflow.com/questions/471416/how-do-you-write-a-migration-to-rename-an-activerecord-model-and-its-table-in-ra) (and don't worry we all have to do it some day :)) – wpp Nov 21 '13 at 20:11
  • So I don't want to beg for reputation, but if the renaming solved your problem, you should mark the answer as correct. – wpp Nov 24 '13 at 20:51