2015-07-13 168 views
0

我試圖將表單數據存儲到數據庫中,而是輸入空值,可能是什麼問題? 我看來form.html.erb文件rails表單數據沒有被保存到數據庫中

<%= 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 
+1

發佈完整的控制器文件。 –

+2

保存時也是你的'rails s'日誌輸出。 – membLoper

+0

爲什麼你的控制器中有一些erb? 0_o無論如何,你的模型中有沒有失敗的驗證? – BroiSatse

回答

0

我不得不修改我的模型文件的工具,像這樣

class CattleClient < ActiveRecord::Base 
     attr_accessible :client_name, :milk, :date 
    end 

感謝您的貢獻壽@馬特,Pavan,Max和其他所有,我很欣賞

+1

如果這是必要的,那麼錯誤可能在你的strong_params('cattle_client_params'方法)中。很奇怪,我建議你不斷嘗試用params找到問題,而不是加入'attr_accessible',這會破壞strong_params。 – Matt

+0

好吧,我會試着繼續尋找這個,馬上感謝你@Matt – BARBRA

相關問題