表單單選ui和select2-1情境model準備_第1頁
表單單選ui和select2-1情境model準備_第2頁
表單單選ui和select2-1情境model準備_第3頁
已閱讀5頁,還剩3頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

情境和Model 準備這一章同樣示范如何在表單實作一個單選的UI,例如活動(Event)需要一個分類,但是跟上一章是固定有限的選項不同,希望分類是可以持續編輯的,因此需要再新增一個CategoryModel,然后讓Categoryhas_manyEvent來建立數據關系。railrailsgmodelcategoryname:stringdb/db/migrate/20170414082617_create_categories.rb20170414082617_create_categories.rbclassCreateCategories<ActiveRecord::Migration[5.0defchangecreate_table:categoriesdo|t|t.string:namet.timestampsend add_column:events,:category_id,:integer add_index:events,:category_idendendrakrakedb:migrateapp/models/category.app/models/category.rbapp/models/category.rbclassCategory<ApplicationRecord has_many:eventsendapp/models/event.app/models/event.rbapp/models/event.rbclassEvent<ApplicationRecord belongs_to:category,:optional=>truerailsgcontroller針對Categorymodelrailsgcontrolleradmin::categories蓋一個去編輯。railsconsole請直接railsconsole10.times{|i|Category.create!(:name=>"#{i}Category")使用Select 下拉選接著來編輯表單,在編輯活動時,可以選擇分類。app/app/views/admin/events/_form.html.erb <divclass="form-group" <%=f.label:category_id% <%=f.select:category_id,Category.all.map{|c|[,c.id]},{},:class=>"form-control"% </div>如果你用simple_form而不是Rails內建的form_for來制作表單的話,是寫<%=f.association:category%>效果是一樣的。app/app/controllers/admin/events_controller.rbapp/controllers/admin/events_controller.rbdefevent_params params.require(:event).permit(:name,:description,:friendly_id,:status) params.require(:event).permit(:name,:description,:friendly_id,:status,:category_id)end成果:顯示分類并避免nil 錯誤臺的活動頁面,需要顯示該活動的分類。app/app/views/events/show.html.erbapp/views/events/show.html.erb+<h2><%=@%></h2活動的

是后來才增加的字段,因此有些活動是沒有

資料的。category_idcategory_id這時如果去點其他還沒有選分類的活動,會發現出現以下的錯誤category_idcategory_idNoMethodErrorNoMethodE@event.category

是nil,再調用

就會出 了。try針對這種情況,Rails提供了一個try

方法,參數就是要調用的方法名稱。透 try管是不是nil,都不會報錯:tryapp/views/events/show.html.erb-<h2><%=@%></h2+<h2><%=@event.category.try(:name)%></h2這樣就算沒有選Cateogry也不會報錯了:使用jQuerySelect2Pluginrailsconsole當選項非常多時,單純的下拉選單,可能就不好選了。例如,請railsconsole再多加一些分類:100.times{|i|Category.create!(:name=>"#{i+100}Category")這種情況,可以安裝jQueryPlugin:Select2是一個非常好用的單選、多選選單,非常適合選項非常多的情境。這個jQueryPlugin有包好的gem:select2rails,請編輯Gemfile,加上Gemfilegem"select2-rails"執行bundle,重啟服務器。app/app/assets/javascripts/application.jsapp/assets/javascripts/application.js//=requirejquery//=requirejquery_ujs//=requireturbolinks//=requirebootstrap-sprockets //=requireselectapp/app/asssets/stylesheets/application.scssapp/asssets/stylesheets/application.scss@import"bootstrap-sprockets";@import"bootstrap"; @import"select2"; @import"select2-bootstrap";這個這個Select2有提供配合Bootstrap的樣式app/views/admin/app/views/admin/events/_form.html.erbapp/views/admin/events/_form.html.erb<script>$("#event_categor

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論