在“创建”上提交表单后,Rails属性将保存为nil

时间:2014-01-04 22:06:31

标签: ruby-on-rails ruby ruby-on-rails-3 forms

我一直在学习Ruby on Rails大约一个月,并决定构建一个简单的成绩计算器应用程序,但我一直坚持这个问题,我不明白为什么。 我的问题是,一旦我提交了一份包含课程名称和个别成绩的表格,所有属性的相关模型都会保存为零。

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

该模型在所有属性上都有attr_accessor,但我仍然不知道它为什么保存为nil

course.rb

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

这些成绩以表格格式输入。

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> 

在课程/索引路径的代码中,我包含了course.inspect以进行调试。根据检查功能输出的内容,通过表单输入的所有属性都将保存为零。

#<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"> 

所有代码都可以在https://github.com/isyed867/Grade-Calc找到 谢谢!

1 个答案:

答案 0 :(得分:0)

更新您的Course模型以使用attr_accessible而不是attr_accessor,以便在属性上允许mass assignment

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

ActiveModel::MassAssignmentSecurity的文档应该让您了解mass_assignment的详细信息。