我正在尋找在我的webapp中重用表單,我嘗試過使用partial來完成這個任務。這裏是我試圖幹出代碼:渲染局部的最佳方式,它要求HAML嵌套在parial下
#layouts/_form.html.haml
.col-sm-6.col-sm-offset-3.well.form-box
.col-sm-12
.row
%h1
.text-center Create a new trip.
.small.text-center
Let's get started planning your trip.
.row.top-buffer
= bootstrap_form_for(@trip, url: customers_trips_path) do |f|
= render 'shared/error_messages', object: f.object
= f.text_field :trip_name, label: "What would you like to call this trip?"
= f.select :location, options_for_select(Vendor.allowed_locations, params[:location]), label: "Where will you be going?"
= f.date_select :start_date, { :label => "What is the start date for your trip?", :start_year => Time.new.year }
= f.number_field :number_of_days, value: 1, in: 1...90, label: "How many days will you be going for?"
.center= f.submit "Create Trip", class: "btn btn-large btn-primary"
我的部分嘗試是這樣的:
- provide(:title, "Create a Trip")
.col-sm-6.col-sm-offset-3.well.form-box
.col-sm-12
.row
%h1
.text-center= header
.small.text-center
= slogan
.row.top-buffer
然而,當我試圖使我的部分像上面,然後窩我的形式,其下,我得到這個錯誤:
syntax error, unexpected keyword_ensure, expecting end-of-input
我很確定我做錯了。我正在考慮以某種方式傳遞bootstrap_form_(可能使用Object#send?),但是這看起來有點不重要,並且想知道是否有更優雅的解決方案。
更新:我修正了對局部局部調用的調用,並更新了代碼以反映這一點。問題依然存在。
這是我如何調用部分,順便說一句。
= render 'layouts/form', header: "original", slogan: "test"
= bootstrap_form_for(@trip, url: customers_trips_path) do |f|
= render 'shared/error_messages', object: f.object
= f.text_field :trip_name, label: "What would you like to call this trip?"
= f.select :location, options_for_select(Vendor.allowed_locations, params[:location]), label: "Where will you be going?"
= f.date_select :start_date, { :label => "What is the start date for your trip?", :start_year => Time.new.year }
= f.number_field :number_of_days, value: 1, in: 1...90, label: "How many days will you be going for?"
.center= f.submit "Create Trip", class: "btn btn-large btn-primary"
乾杯!
您的代碼實際上並不表明你調用部分。你能顯示你正在使用的那個導致錯誤的實際代碼嗎? – matt
@matt,按要求完成! – ianks