我在窗體中有多個複選框,其中我將每個字段的結果存儲在我使用attr_accessor:attribute在模型中指定的相應臨時字段中。我想用逗號分隔符將這些字段的內容加入到一個字段中,並將其存儲到數據庫中。如何使用分隔符在rails 4應用程序中將字符串字段連接到一個字段?
我的完整形式是這樣的:
<%= form_for @company do |f| %>
<% if @company.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@company.errors.count, "error") %> prohibited
this company from being saved:</h2>
<ul>
<% @company.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<div class="row">
<div class='col-sm-6'>
<div class="form-group">
<p>
<%= f.label :name %><br>
<%= f.text_field :name,class:'form-control' %>
</p>
<p>
<%= f.label :grade %><br>
<%= f.select(:grade, options_for_select([['Dream', 'dream'], ['A++', 'a++'], ['A+', 'a+'],['A', 'a']])) %>
</p>
<p>
<%= f.label :beCutoff %><br>
<%= f.text_field :beCutoff,class:'form-control' %>
</p>
<%= f.label :branchesAllowed %><br>
<%= f.check_box :coe, {}, "COE", "" %><%= label_tag :COE %><br>
<%= f.check_box :ece, {}, "ECE", ""%><%= label_tag :ECE %><br>
<%= f.check_box :ice, {}, "ICE", ""%><%= label_tag :ICE %><br>
<%= f.check_box :it, {}, "IT", ""%><%= label_tag :IT %><br>
<%= f.check_box :mpae, {}, "MPAE", ""%><%= label_tag :MPAE %><br>
<%= f.check_box :bt, {}, "BT", ""%><%= label_tag :BT %><br>
<%= f.check_box :is, {}, "IS", ""%><%= label_tag :IS %><br>
<%= f.check_box :sp, {}, "SP", ""%><%= label_tag :SP %><br>
<%= f.check_box :pc, {}, "PC", ""%><%= label_tag :PC %><br>
<p>
<%= f.label :backsAllowed %><br>
<%= f.text_field :backsAllowed,class:'form-control' %>
</p>
<p>
<%= f.label :details %><br>
<%= f.text_field :details,class:'form-control' %>
</p>
<p>
<%= f.label :package %><br>
<%= f.text_field :package,class:'form-control' %>
</p>
<p>
<%= f.label :xiiCutoff %><br>
<%= f.text_field :xiiCutoff,class:'form-control' %>
</p>
<p>
<%= f.label :xCutoff %><br>
<%= f.text_field :xCutoff,class:'form-control' %>
</p>
<%= f.label :deadline %><br>
<div class='input-group date' id='datetimepicker1'>
<%= f.text_field :deadline ,class:'form-control',:readonly=>true%>
<span class="input-group-addon" ><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
<script type="text/javascript">
$(function() {
$('#datetimepicker1').datetimepicker({format: 'YYYY-MM-DD HH:mm:ss '});
});
</script>
</div>
<p>
<%= f.submit %>
</p>
<% end %>
我指定的字段:安科:ECE,:冰等在我的模型作爲attr_accessor:
class Company < ActiveRecord::Base
attr_accessor :coe,:ece,:ice,:it,:mpae,:bt,:is,:pc,:sp
validate :deadline_on_or_before_now
def deadline_on_or_before_now
errors.add(:deadline, "can't be in the past") if
!deadline.blank? and deadline < (Time.zone.now+19800).to_datetime
end
validates :name, presence: true,
length: { minimum: 2 };
validates :grade,:beCutoff,:details,:package, presence: true;
validates_inclusion_of :beCutoff,:xiiCutoff,:xCutoff, :in=> 0..100,:message=>"Out of Range";
end
我的控制器看起來像這樣:
class CompaniesController < ApplicationController
def new
@company = Company.new
end
def edit
@company = Company.find(params[:id])
end
def update
@company = Company.find(params[:id])
if @company.update(company_params)
redirect_to @company
else
render 'edit'
end
end
def create
@company = Company.new(company_params)
if @company.save
redirect_to @company
else
render 'new'
end
end
def index
@companies = Company.all
end
def destroy
@company = Company.find(params[:id])
@company.destroy
redirect_to companies_path
end
def show
@company = Company.find(params[:id])
end
private
def company_params
params.require(:company).permit(:name, :beCutoff,:grade,:xiiCutoff,:xCutoff,:backsAllowed,:details,:package,:deadline,:branchesAllowed,:coe,:ece,:ice,:it,:mpae,:bt,:is,:sp,:pc) if params[:company]
end
end
我想加入:安科:ECE,:冰等領域,並將其存儲到:branchesAllowed(串場)的F使用commma分隔符,並且可以在需要分開使用時分割此字段。 我使用rails 4.1的postgresql。
我不想再次混淆代碼,所以當然一個讓我做一次更改方法的方法會有所幫助。但我想知道這兩種方法。 – Pranav