【问题标题】:Illegal nesting: content can't be both given on the same line as %h1 and nested within it [duplicate]非法嵌套:内容不能与 %h1 在同一行给出并嵌套在其中 [重复]
【发布时间】:2015-10-23 16:18:54
【问题描述】:

我在一个电子商务网站上工作,我的索引页面出错,我没有找到 copred 的位置。 谁能帮帮我? 谢谢!

%h1 Products
 - if @products.present?
  %ul
   - @products.each do |product|
  %li= link_to product.name, product_path(product)
—
- if product.stock.zero?
Out of stock!
- else
= link_to 'Buy', checkout_product_path
 - else
  No products

 %a{href: new_product_path} Add product

我的产品_控制器.rb

class ProductsController < ApplicationController
 before_action :authenticate_user!, only: [:checkout, :buy]

 def index
  @product = Product.all
 end

def show
 @product = Product.find(params[:id])
end...

...

 private

 def product_params
  params.require(:product).permit(:name, :price, :image_url, :stock)
 end
end

【问题讨论】:

  • 我已将其作为副本关闭,链接答案中的解释适用于此处,但在您的情况下,这些行是前两行,您不能同时拥有 %h1 Products 和以下内容(这是缩进,因此也被视为 h1 标记的内容。您需要修复 - if @products.present? 块的缩进。

标签: ruby-on-rails ruby haml


【解决方案1】:

试试这个:

%h1 Products
  - if @products.present?
    %ul
      - @products.each do |product|
        %li
          = link_to product.name, product_path(product)
          &mdash;
          - if product.stock.zero?
            Out of stock!
          - else
            = link_to 'Buy', checkout_product_path
  - else
    No products

  %a{href: new_product_path} Add product

【讨论】:

  • &lt;ul&gt; 应该跟在&lt;h1&gt; (&lt;h1&gt;&lt;ul&gt;...&lt;/ul&gt;&lt;/h1&gt;) 后面时,难道不会尝试将它放在它后面吗?
猜你喜欢
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
相关资源
最近更新 更多