我正在用滑轨创建一个商店,我已经创建了一个名称,类别,描述和图像的Products控制器。添加产品管理员时,“我的类别”是一个下拉选择选项,用户可以从下拉菜单中选择产品所属的类别。一切都很好,但是现在我想为所有类别使用不同的索引页,假设一个产品有一个类别主席,并且我有一个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 %>
chair.html.erb
<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>
当我删除<% if @product.category = chair? %>
行时,它可以正常工作并显示所有产品。无论如何,是否只显示具有类别值的产品。
答案 0 :(得分:1)
似乎您要在此处比较值,因此应使用padding
运算符代替=
运算符。
假设==
属性为字符串类型:
category
应该适合您的情况。
在Ruby <% if @product.category == "chair" %>
(等于运算符)中用于分配。
答案 1 :(得分:0)
想象一下椅子类别是3的最后一个
boolean remove(E obj) {
ListIterator<E> iter = theList.listIterator();
while (iter.hasNext()){
if (obj.compareTo(iter.next()) == 0) {
}
}
return false;
}