代码之家  ›  专栏  ›  技术社区  ›  That dude

在新页面中显示选定的下拉选项

  •  0
  • That dude  · 技术社区  · 6 年前

    chair.html.erb 在这里,我只想展示具有类别价值的产品。我想用 <% if @product.category = chair? %> undefined method 椅子`

    <%= form_with(model: product, local: true) do |form| %>
      <% if product.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:</h2>
    
          <ul>
          <% product.errors.full_messages.each do |message| %>
            <li><%= message %></li>
          <% end %>
          </ul>
        </div>
      <% end %>
    
      <div class="field">
        <%= form.label :title %>
        <%= form.text_field :title, id: :product_title %>
      </div>
    
    
      <div class="field">
        <%= form.label :category %>
        <%= form.select :category, ['Chair', 'Bench', 'Divanbed'] %>
      </div>
    
      <div class="field">
        <%= form.label :description %>
        <%= form.text_area :description, id: :product_description %>
      </div>
    
    
    
      <div class="field">
        <%= form.label :image %><br>
        <%= form.file_field :image %>
      </div>
    
      <div class="actions">
        <%= form.submit %>
      </div>
    <% end %>
    

    <head>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    
    <link rel="stylesheet" href="/assets/chairs.scss">
    </head>
    
    <body>
    
      <!--Carousel -->
      <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
      <ol class="carousel-indicators">
        <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
      </ol>
      <div class="carousel-inner">
        <div class="carousel-item active">
          <img class="d-block w-100" src="/assets/living-carousel-1.jpeg" alt="First slide">
        </div>
        <div class="carousel-item">
          <img class="d-block w-100" src="/assets/living-carousel-2.jpeg" alt="Second slide">
        </div>
        <div class="carousel-item">
          <img class="d-block w-100" src="/assets/living-carousel-3.jpeg" alt="Third slide">
        </div>
      </div>
      <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
      </a>
      <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
      </a>
    </div>
    
    
    
    
    <div class="container-fluid section-title ">
          <div class="container py-4">
            <h3 class="text-center">products</h3>
            <hr class="title-border">
            <p class="text-justify text-center">The modestly scaled Engineer Denny Dots  product is  perfect for a corner spot or entryway adds so much cool attitude having a prime example for you in your relaxing time.</p>
          </div>
    </div>
    
    
    
    
      <main>
    
    
    
    
        <div class="container-fluid bg-cover">
          <div class="container product-section-cover">
    
            <div class="row">
    
    
              <% @products.each do |product| %>
              <% if @product.category = chair? %>
              <div class="col col-md-4 col-sm-6">
                  <div class="product-grid">
                    <div class="image">
                        <img src= "<%= product.image %>" alt="<%= product.title %> 's image" class="img-fluid">
                        <span class="overlay">
                            <span class="product-details">
                               <%= link_to "View Details", product, class: "linky" %>
                            </span>
                        </span>
                    </div>
                    <div class="product-discription py-3">
                    <h4 class="text-center"><%= product.title %></h4>
                    <p class="text-center"><small><%= product.description %></small></p>
    
                    </div>
                    <div class="product-btn py-2 text-center">
                    <a href="#" class="btn btn-lg " role="button" aria-pressed="true">ADD TO CART</a>
                    </div>
                  </div>
              </div>
              <% end %>
              <% end %>
    
          </div>
        </div>
    
    
      </main>
    
    </body>
    

    当我拆掉线的时候 < 它工作良好,显示所有的产品。无论如何,是否只显示具有该类别价值的产品。

    2 回复  |  直到 6 年前
        1
  •  1
  •   John Baker    6 年前

    = 您应该使用的运算符 == 接线员。

    假设 category 属性是字符串类型:

    <% if @product.category == "chair" %> 你的案子该办了。

    =

        2
  •  0
  •   nourza    6 年前

    假设椅子类别是3个类别中的最后一个

    <% Product.category.order("created_at asc").last(3).each do |product| %>
              <div class="col col-md-4 col-sm-6">
                  <div class="product-grid">
                    <div class="image">
                        <img src= "<%= product.image %>" alt="<%= product.title %> 's image" class="img-fluid">
                        <span class="overlay">
                            <span class="product-details">
                               <%= link_to "View Details", product, class: "linky" %>
                            </span>
                        </span>
                    </div>
                    <div class="product-discription py-3">
                    <h4 class="text-center"><%= product.title %></h4>
                    <p class="text-center"><small><%= product.description %></small></p>
    
                    </div>
                    <div class="product-btn py-2 text-center">
                    <a href="#" class="btn btn-lg " role="button" aria-pressed="true">ADD TO CART</a>
                    </div>
                  </div>
              </div>
              <% end %>
              <% end %>