That dude
That dude

Reputation: 389

show a selected dropdown option in a new page

I am creating a shop in rails i have created a Products controller with name, Category, description and image. Where my Category is a dropdown select option when adding product admin user can select from the dropdown in which category the product falls. It all work nicely, but now i want to have different index pages for all the categories, suppose if a product have a category chair, and i have a chair.html.erb where i want to show only the products which have the category value chair. I thought of using the <% if @product.category = chair? %> for that but it gives me the error: undefined methodchair?' for`

Products form

<%= 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>

when i remove the line <% if @product.category = chair? %> it works fine and show all the products. Is there anyway to show only the products with the value of the category.

Upvotes: 0

Views: 149

Answers (2)

nourza
nourza

Reputation: 2321

Imagine the chair category is the last one of 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 %>

Upvotes: 0

John Baker
John Baker

Reputation: 2398

Seems like you're trying to compare values here so instead of = operator you should use == operator.

Assuming that the category attribute is of a String type:

<% if @product.category == "chair" %> should work on your case.

In Ruby = (equal operator) is used for assignment.

Upvotes: 1

Related Questions