我有一個應用程序,用戶可以將項目輸入數據庫。Rails:如果check_box_tag中沒有選擇任何內容,則默認選擇
有一個選項,他們可以爲他們的項目選擇許多不同的技術。目前,如果用戶沒有選擇至少1種技術,應用程序會標記錯誤。
我想改變這個,所以如果他們不選擇一項技術,它會自動下降爲「其他」。
這裏是我的項目負責人的行動,新的創造:
def new
@project = Project.new
@technol = Technol.new(params[:tech])
@all_technols = Technol.order('tech ASC')
@all_technols = Technol.all
tech_ids = params[:technols][:id].reject(&:blank?) unless params[:technols].nil?
@project_technol = @project.projecttechnols.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @project }
end
end
def create
@project = Project.new(params[:project])
@project.client = params[:new_client] unless params[:new_client].blank?
@project.industry = params[:new_industry] unless params[:new_industry].blank?
@project.business_div = params[:new_business_div] unless params[:new_business_div].blank?
if !params[:technols].nil?
params[:technols][:id].each do |tech|
if !tech.empty?
@project_technol = @project.projecttechnols.build(:technol_id => tech)
end
end
end
這裏是該技術領域
<ul>
<% @all_technols.each do |technol| %>
<li class="split">
<%= check_box_tag "project[technol_ids][]", technol.id, @project.technols.include?(technol) %>
<%= technol.tech %>
</li>
<% end %>
</ul>
技術ID,在 「其它」 的新觀點技術表是「18」。那麼有沒有辦法說,如果沒有選擇技術,那麼:technol_id => ["18"]
。
我得到的幫助較早,並建議我應該補充一點:
def create
...
technol_ids = params[:technol_ids].blank? ? [18] : params[:technol_ids]
technol_ids.each do |id|
...
end
...
end
我無法得到這個工作。我認爲我沒有把它放在我的代碼中。我還是個新手,因此請在嘗試幫助時記住這一點。非常感謝
編輯
在沒有technols_id選擇
Processing by ProjectsController#create as HTML
Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"8Hgc1GXqNhWkzO3Wgkfpf6z+fdImf6QvAv0XLbP0a5g=", "project"=>{"project
_name"=>"Test", "archive"=>"0", "username"=>"test", "status"=>"Active", "exception_pm"=>"", "client"=>"", "business_d
iv"=>"", "project_owner"=>"", "start_date"=>"11-12-2012", "first_name"=>"test", "last_name"=>"test", "fullname"=>
"test test", "entry_date"=>"2012-11-26", "end_date"=>"19-12-2012", "techinfo"=>"Test", "role"=>"", "industry"=>""
, "summary"=>"Test", "lessons_learned"=>"Test", "customer_benefits"=>"Test", "financials"=>"£500,000 - £999,999 "}, "n
ew_exception_pm"=>"Test", "new_client"=>"Test", "new_business_div"=>"Test", "new_project_owner"=>"Test", "new_role"=>"Te
st", "new_industry"=>"Test", "commit"=>"Save New Project"}
隨着1 technols_id
Started POST "/projects" for 192.168.16.127 at 2012-11-26 16:47:27 +0000
Processing by ProjectsController#create as HTML
Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"8Hgc1GXqNhWkzO3Wgkfpf6z+fdImf6QvAv0XLbP0a5g=", "project"=>{"project
_name"=>"Test", "archive"=>"0", "username"=>"Test", "status"=>"Active", "exception_pm"=>"", "client"=>"", "business_d
iv"=>"", "project_owner"=>"", "start_date"=>"11-12-2012", "first_name"=>"Test", "last_name"=>"McLaughlin", "fullname"=>
"Test Test", "entry_date"=>"2012-11-26", "end_date"=>"20-12-2012", "technol_ids"=>["1"], "techinfo"=>"Test", "rol
e"=>"", "industry"=>"", "summary"=>"Test", "lessons_learned"=>"Test", "customer_benefits"=>"Test", "financials"=>"£250,
000 - £499,999 "}, "new_exception_pm"=>"Test", "new_client"=>"Test", "new_business_div"=>"Test", "new_project_owner"=>"
Test", "new_role"=>"Test", "new_industry"=>"Test", "commit"=>"Save New Project"}
當您提交沒有technol_ids選擇的表單時,「params」的輸出是什麼?至少有一個選擇? – MrYoshiji
我已將我的日誌添加到問題 – Jazz
嘗試使用'params [:project] [:technol_ids] || = [18]'創建操作的第一行。如果'params [:project] [:technol_ids]'爲零,它會用'[18]'設置'technol_ids'數組。但是正如@Rodrigo Dias所說,設置這樣的默認id並不是一個好主意。你可以有一個像'self.default_technology_id'這樣的技術類方法返回id。 – MrYoshiji