简体   繁体   中英

Rails attributes are saving as nil once the form is submitted on 'create'

I've been learning Ruby on Rails for about a month now and decided to build a simple grade calculator application but I've been stuck on this problem and I don't understand why. My problem is that once I submit a form with the course name and individual grades, all the attributes get saved as nil for its associated model.

courses_controller.rb

class CoursesController < ApplicationController

def index
   @courses = Course.all
end

def show 
   @course = Course.find(params[:id])
end

def new
    @course = Course.new
end

def create
    @course = Course.new(params[:course])
    if @course.save
        redirect_to(:action => 'index')
    else 
        render 'new'
    end 
end

The model has attr_accessor on all the attributes but I still don't know why it is saving as nil

course.rb

class Course < ActiveRecord::Base
    attr_accessor :name, :test1, :test2, :lab1, :lab2, :midterm, :final
end

The grades are being inputed in a table format.

new.html.erb

<!DOCTYPE html>
<html> 
    <head>
        <title> Grade Calculator | NEW</title>
    </head>

    <body>
        <h1>New Course</h1>

    <%= form_for(:course, url: {action: 'create'}) do |f| %>

        <div class="field"><%= f.label :name %></div>
        <div class="field"><%= f.text_field :name %></div>

        <table cellspacing="10" border="3" >
            <tr>
                <th><h3>Evaluation</h3></th>
                <th><h3>Weight</h3></th>
                <th colspan="2"><h3>Marks</h3></th>
                <th><h3>Weight Average</h3></th>
            </tr>

            <tr align="center">
                <td>Tests</td>
                <td>25%</td>
                <td class="field"><%= f.text_field :test1, :size => 5 %></td>
                <td class="field"><%= f.text_field :test2, :size => 5 %></td>
                <td>AVG</td>
            </tr>

            <tr align="center">
                <td>Labs</td>
                <td>25%</td>
                <td><%= f.text_field :lab1, :size => 5 %></td>
                <td><%= f.text_field :lab2, :size => 5 %></td>
                <td>AVG</td>
            </tr>

            <tr align="center">
                <td>Midterm</td>
                <td>25%</td>
                <td  colspan="2"><%= f.text_field :midterm, :size => 5 %></td>
                <td>AVG</td>
            </tr>

            <tr align="center">
                <td>Final</td>
                <td>25%</td>
                <td colspan="2"><%= f.text_field :final, :size => 5 %></td>
                <td>AVG</td>
            </tr>

        </table>
        <div class="actions">
        <%= f.submit "Create Course" %>
        </div>
    <% end %>

    <h3>Final Grade: <%= @final_grade%>  </h3>
    <p><br> <%= @grades%> </p>
    <p> </p>
    <%= link_to 'List Courses', :action => "index" %>
    </body>

    </html> 

In the code for the course/index path I included course.inspect for debugging purposes. From what the inspect function outputs, all the attributes entered through the form are being saved saved as nil.

#<Course id: 1, name: nil, test1: nil, test2: nil, lab1: nil, lab2: nil, midterm: nil, final: nil, created_at: "2014-01-04 21:21:18", updated_at: "2014-01-04 21:21:18">                                Show    Edit    Delete
#<Course id: 2, name: nil, test1: nil, test2: nil, lab1: nil, lab2: nil, midterm: nil, final: nil, created_at: "2014-01-04 21:21:25", updated_at: "2014-01-04 21:21:25">                                Show    Edit    Delete
#<Course id: 3, name: nil, test1: nil, test2: nil, lab1: nil, lab2: nil, midterm: nil, final: nil, created_at: "2014-01-04 21:27:51", updated_at: "2014-01-04 21:27:51">                                Show    Edit    Delete
#<Course id: 4, name: nil, test1: nil, test2: nil, lab1: nil, lab2: nil, midterm: nil, final: nil, created_at: "2014-01-04 21:28:16", updated_at: "2014-01-04 21:28:16"> 

All the code can be found on https://github.com/isyed867/Grade-Calc Thanks!

Update your Course model to use attr_accessible instead of attr_accessor so that mass assignment is allowed on the attributes:

class Course < ActiveRecord::Base
    attr_accessible :name, :test1, :test2, :lab1, :lab2, :midterm, :final
end

The documentation of ActiveModel::MassAssignmentSecurity should give you an understanding of what mass_assignment is in detail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM