grading.rb
class Grading
require 'autoinc'
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Autoinc
field :quantity, type: Float
field :count, :type => Float
field :grade_id, type: Integer
field :batch_id, type: Integer
field :variety_id, type: Integer
field :serial_id, :type => Integer
field :soaked, :type => Boolean
# Mongoid AutoInc
increments :serial_id
# Associations
belongs_to :grade
belongs_to :variety
belongs_to :batch
has_many :grading_weighments, :dependent=> :destroy
# validations
validates_presence_of :grade_id,:batch_id,:variety_id
validates_presence_of :quantity , numericality: {:greater_than => 0}
validates_presence_of :count ,numericality: {:greater_than_or_equal_to => 10, :less_than_or_equal_to => 150}
attr_accessor :weights
accepts_nested_attributes_for :grading_weighments, :allow_destroy => true
end
gradingcontroller.rb
class GradingsController < ApplicationController
before_action :set_grading, only: [:show, :edit, :update, :destroy]
# load_and_authorize_resource
# GET /gradings
# GET /gradings.json
def index
@gradings = Grading.all.order('date DESC')
@q = Batch.search(params[:q])
@batches = @q.result(:distinct => true).in(status:["HLQDone","GradingDone"]).order('updated_at ASC').page(params[:page]).per(5)
# @batches = Batch.all.order('created_at DESC').page(params[:page]).per(5) .where(status: "HLQ_Done")
@grades = Grade.all.map{|g| [g.id.to_s,g.name]}
@varieties = Variety.all.map{|v| [v.id.to_s,v.name]}
respond_to do |format|
format.js
format.html
end
end
# GET /gradings/1
# GET /gradings/1.json
def show
end
# GET /gradings/new
def new
@grading = Grading.new
end
# GET /gradings/1/edit
def edit
end
# POST /gradings
# POST /gradings.json
def create
@grades = Grade.all.map{|g| [g.id.to_s,g.name]}
@varieties = Variety.all.map{|v| [v.id.to_s,v.name]}
@grading = Grading.new(grading_params)
@batch= @grading.batch
weights=params[:grading][:weights]
@grading.quantity= weights.map! { |i| i.to_f }.sum
respond_to do |format|
if @grading.save
@grading_weighments=GradingWeighment.new
@grading_weighments.grading_id =@grading.id
@grading_weighments.weights =weights.join(',')
@grading_weighments.save
format.html { redirect_to gradings_path, notice: 'Grading was successfully created.' }
format.json { render action: 'show', status: :created, location: @grading }
format.js
else
format.html { render action: 'new' }
format.json { render json: @grading.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /gradings/1
# PATCH/PUT /gradings/1.json
def update
g=[]
puts "#{params[:grading][:grading_weighments_attributes]}"
params[:grading][:grading_weighments_attributes].each do |key,value|
g<<value[:weights]
end
puts "=============#{g}"
q= g.map{ |i| i.to_f }.sum
puts "==========aes==#{q}"
@grading.update_attributes(quantity: q)
@gw= GradingWeighment.find_or_create_by(grading_id: @grading.id)
@gw.update_attributes(weights: g.join(','))
respond_to do |format|
if @grading.update(grading_params)
@grades = Grade.all.map{|g| [g.id.to_s,g.name]}
@varieties = Variety.all.map{|v| [v.id.to_s,v.name]}
format.html { redirect_to gradings_path, notice: 'Grading was successfully updated.' }
format.json { respond_with_bip(@grading) }
format.js
else
format.html { render action: 'edit' }
format.json { render json: @grading.errors, status: :unprocessable_entity }
format.js
end
end
end
# DELETE /gradings/1
# DELETE /gradings/1.json
def destroy
@grading_id=@grading.id
batch_number= @grading.batch.batch_number
@grading.destroy
respond_to do |format|
@del_batch = Batch.find_by(batch_number: batch_number)
@length=@del_batch.gradings.length
@del_batch.update_attributes(status: "HLQ_Done") if @length==0
format.html { redirect_to gradings_url }
format.json { head :no_content }
format.js
end
end
def fetch_weights
@grading_weighments=GradingWeighment.find_by(grading_id: params[:id]).weights
@grading=Grading.find(params[:id])
(@grading.grading_weighments.first.weights.split(',').length-1).times {@grading.grading_weighments.build}
respond_to do |format|
format.html { render :nothing => true, :status => 200, :content_type => 'text/html'}
format.json { head :no_content }
format.js
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_grading
@grading = Grading.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def grading_params
params.require(:grading).permit(:quantity,:batch_id,:variety_id,:grade_id,:count, :weights,:grading_weighments_attributes, :soaked)
end
end
list.html.erb
<thead>
<tr>
<th>Count</th>
<th>Grade</th>
<th>Variety</th>
<th>Quantity</th>
<th>Soaked</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<%batch= @batch if !batch%>
<%if batch.gradings.length==0%>
<tr><td colspan="5" class="warning">Grading is not done for batch with number <%=batch.batch_number%></td></tr>
<%else%>
<%gradings=batch.gradings.order('created_at DESC')%>
<%gradings.each do |grading|%>
<tr id="<%=grading.id%>">
<td><%= best_in_place grading, :count, :as => :input, required:true%></td>
<td><%= best_in_place grading, :grade_id, :as => :select, :collection => @grades %></td>
<td><%= best_in_place grading, :variety_id, :as => :select, :collection => @varieties %></td>
<td><%= best_in_place grading, :quantity, :as => :input%></td>
<td><%= best_in_place grading, :soaked, :as => :input, required:true%></td>
<td>
<%= link_to "getweights/#{grading.id}", method: :get, :remote => true ,class:'btn btn-info' do%>
<i class="fa fa-plus"></i>
<% end %>
<%= link_to grading, method: :delete, remote:true,data: { confirm: 'Are you sure?' } ,title:'Delete this grading', class:'btn btn-danger' do%>
<i class="fa fa-trash-o"></i>
<%end%>
</td>
</tr>
<%end%>
<%end%>
</tbody>
</table>
new.grading.html.erb
<div class="form-group">
<%= f.label :soaked ,class:"sr-only"%>
<%= f.select :soaked ,options_for_select(["Soaked", "Un-soaked"]), {:include_blank => "Select Soaking"}, class: "form-control" , autocomplete:"off", required: true %>
</div>
这里我添加了一个名为&#34;字段的新字段:soaked,:type =&gt; Boolean&#34;,此字段不保存在数据库中,也不显示在视图中。如何解决这个问题请帮帮我。
注意:我也通过了分级参数(浸泡)。
答案 0 :(得分:2)
试试这个:
<%= f.select :active, [['Soaked', true], ['Un-soaked', false]] %>
答案 1 :(得分:0)
浸泡只接受TRUE
或FALSE
,而您尝试保存"Soaked", "Un-soaked"
<%= f.check_box :soaked %>
您可以使用CHECKBOX
。
如果您仍想使用"Soaked"
和"Un-soaked"
。您可以在您的控制器中进行检查。如果选择Soaked
,则保存TRUE
其他FALSE
试试这个
<%= f.select :soaked, options_for_select([['Soaked', true], ['Un-soaked', false]]) %>