我试图将表单数据存储到数据库中,但是输入了null值,可能是什么问题? 我的视图form.html.erb文件是
<%= form_for(@cattle_client) do |f| %>
<% if @cattle_client.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@cattle_client.errors.count, "error") %> prohibited this cattle_client from being saved:</h2>
<ul>
<% @cattle_client.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :client_name %><br>
<%= f.text_field :client_name %>
</div>
<div class="field">
<%= f.label :milk %><br>
<%= f.number_field :milk %>
</div>
<div class="field">
<%= f.label :date %><br>
<%= f.date_select :date %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我的cattle_clients_controller.rb是
class CattleClientsController < ApplicationController
before_action :set_cattle_client, only: [:show, :edit, :update, :destroy]
# GET /cattle_clients
# GET /cattle_clients.json
def index
@cattle_clients = CattleClient.all
end
# GET /cattle_clients/1
# GET /cattle_clients/1.json
def show
end
# GET /cattle_clients/new
def new
@cattle_client = CattleClient.new
end
# GET /cattle_clients/1/edit
def edit
end
# POST /cattle_clients
# POST /cattle_clients.json
def create
@cattle_client = CattleClient.new(cattle_client_params)
respond_to do |format|
if @cattle_client.save
format.html { redirect_to @cattle_client, notice: 'Cattle client was successfully created.' }
format.json { render :show, status: :created, location: @cattle_client }
else
format.html { render :new }
format.json { render json: @cattle_client.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /cattle_clients/1
# PATCH/PUT /cattle_clients/1.json
def update
respond_to do |format|
if @cattle_client.update(cattle_client_params)
format.html { redirect_to @cattle_client, notice: 'Cattle client was successfully updated.' }
format.json { render :show, status: :ok, location: @cattle_client }
else
format.html { render :edit }
format.json { render json: @cattle_client.errors, status: :unprocessable_entity }
end
end
end
# DELETE /cattle_clients/1
# DELETE /cattle_clients/1.json
def destroy
@cattle_client.destroy
respond_to do |format|
format.html { redirect_to cattle_clients_url, notice: 'Cattle client was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_cattle_client
@cattle_client = CattleClient.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def cattle_client_params
params.require(:cattle_client).permit(:client_name, :milk, :date)
end
end
我的路线档案有
resources :cattle_clients
有人帮我确定问题所在吗?
我的迁移文件是
class CreateCattleClients < ActiveRecord::Migration
def change
create_table :cattle_clients do |t|
t.string :client_name
t.integer :milk
t.date :date
t.timestamps null: false
end
end
end
模型是
class CattleClient < ActiveRecord::Base
end
答案 0 :(得分:0)
我必须将我的模型文件编辑为像这样的工具
class CattleClient < ActiveRecord::Base
attr_accessible :client_name, :milk, :date
end
感谢@Matt,Pavan,Max和其他所有人的贡献,我很欣赏