【问题标题】:Best way to render a partial which requires HAML to be nested under the parial渲染需要将 HAML 嵌套在 parial 下的部分的最佳方法
【发布时间】:2014-04-22 00:13:58
【问题描述】:

我希望在我的 web 应用程序中重用表单,我尝试使用部分表单来完成此任务。这是我试图干掉的代码:

#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_for(可能使用 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,按要求完成!

标签: ruby-on-rails ruby haml partial nested


【解决方案1】:

http://guides.rubyonrails.org/layouts_and_rendering.html 中所述(查找“locals”!),您应该使用传递给局部的本地作为“真正的”本地,换句话说,不附加':'!

【讨论】:

  • 谢谢@Danny。尝试了您的解决方案,但问题仍然存在。我确实带领你不必传递:locals => 标签!
  • 我通常做的是剥离部分直到它工作,然后重新构建它。这通常会向您显示香蕉的去向...乍一看,该错误表明缺少某些“结尾”...
【解决方案2】:

我认为您应该将部分视图更改为以下内容:

- 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

【讨论】:

  • 完成,编辑 OP 以反映这一点。问题仍然存在。谢谢你:)
猜你喜欢
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 2011-12-29
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多