我想合并这两个值:hours
和:minutes
并将它们转换为to_i
很快。接下来是将此值(应以秒为单位)分配给 :time_duration
这是 cars
中的一列db 在创建新的 service
之前。 :time_duration
位于 hidden_field
因为没有理由在 View 中呈现这些数据。
views
这是我的_car_fields.html.erb
这是名为 _form.html.erb
的 View 模板内的嵌套部分。 .
_car_fields.html.erb
<div class="nested-fields">
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %><br>
<%= f.label :hours %>
<%= f.select :hours, '0'..'8' %>
<%= f.label :minutes %>
<%= f.select :minutes, options_for_select( (0..45).step(15), selected: f.object.minutes )%><br>
<%= f.label :price %><br>
<%= f.text_field :price, :value => (number_with_precision(f.object.price, :precision => 2) || 0) %> <br>
<%= f.label :details %><br>
<%= f.text_area :details %></div>
<%= link_to_remove_association "Remove Car", f, class: 'btn btn-default' %>
<%= f.hidden_field :time_duration, value: %>
<br>
<hr>
</div>
_form.html.erb
<%= simple_form_for @service do |f| %>
<div class="field">
<%= f.label "Select service category" %>
<br>
<%= collection_select(:service, :service_menu_id, ServiceMenu.all, :id, :name, {:prompt => true }) %>
<%= f.fields_for :cars do |task| %>
<%= render 'car_fields', :f => task %>
<% end %>
</div>
<div class="links">
<%= link_to_add_association 'Add New Car', f, :cars, class: 'btn btn-default' %>
</div><br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
controller
services_controller
def new
@service = current_tech.services.build
end
def create
@service = current_tech.services.build(service_params)
respond_to do |format|
if @service.save
format.html { redirect_to @service, notice: 'Service was successfully created.' }
format.json { render :show, status: :created, location: @service }
else
format.html { render :new }
format.json { render json: @service.errors, status: :unprocessable_entity }
end
end
end
private
def service_params
params.require(:service).permit(:name, :service_menu_id, cars_attributes: [:tech_id, :name, :hours, :minutes, :price, :details, :_destroy])
end
models
service.rb
class Service < ActiveRecord::Base
belongs_to :tech
belongs_to :service_menu
has_many :cars, dependent: :destroy
accepts_nested_attributes_for :cars, :reject_if => :all_blank, :allow_destroy => true
end
汽车.rb
class Car< ActiveRecord::Base
belongs_to :service
belongs_to :tech
has_many :appointments
end
最佳答案
首先,您可以从表单中删除隐藏的 time_duration
字段,因为不需要它。
然后,您将为您的汽车模型创建一个 before_save
方法:
汽车.rb
class Car < ActiveRecord::Base
belongs_to :service
belongs_to :tech
has_many :appointments
before_save :generate_time_duration
def generate_time_duration
self[:time_duration] = hours.hours.to_i + minutes.minutes.to_i
end
end
它的作用:在保存汽车对象之前,它将运行 generate_time_duration
方法。此方法的作用是简单地将 hours.to_i
和 minutes.to_i
相加,并将其分配给汽车的 time_duration
属性。
更新旧数据库记录
由于您是在创建记录后在应用程序中添加此功能,因此有一种更新所有当前记录的快速方法:
- 在命令行中,通过运行命令
rails c
(或rails console
)打开 Rails 控制台 - 在控制台中,运行以下命令:
Car.all.each { |c| c.保存! }
这是一个快速的一次性修复,它将循环遍历所有汽车记录,保存它们,然后更新其 time_duration
字段。
关于ruby-on-rails - Rails - 将小时和分钟转换为秒,然后将值分配给键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31567307/