2014-04-20 28 views
0

我正在尋找在我的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" 

乾杯!

+0

您的代碼實際上並不表明你調用部分。你能顯示你正在使用的那個導致錯誤的實際代碼嗎? – matt

+0

@matt,按要求完成! – ianks

回答

1

http://guides.rubyonrails.org/layouts_and_rendering.html(查找「locals」!)中所述,您應該使用本地傳遞給partial作爲「真實」本地,換句話說,不附加':'附加到!

+0

謝謝@Danny。試過你的解決方案,但問題依然存在。我確實領導過,你不必通過':locals =>'標籤! – ianks

+0

我通常會做的是去掉部分直到它工作,然後再次構建它。這通常會告訴你它在哪裏去香蕉...乍一看,錯誤表明一些'結束'缺少... – Danny

1

我相信你應該改變你的局部視圖以下內容:

- 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 
+0

完成,編輯OP來反映這一點。問題依然存在。謝謝 :) – ianks