1

I am working in a Rails Project and my problem is that for some reason the home view shows the SQL Query next to the book image. Here is the HTML code that shows the index view

<div id="books-index">
  <% @books.each_slice(4) do |book| %>
      <div class="row">
        <%= book.each do |book| %>
            <div class="col-md-3 col-sm-3">
            <h3><%= book.title %></h3>
            <%= image_tag(book.coverpath) %>
            <%= link_to 'Read More', book_path(book), class:"btn btn-primary" %>
            </div>
        <% end %>
      </div>
  <% end %>
</div>

and the books controller

class BooksController < ApplicationController
  def new
    @page_title = 'Add Book'
    @book = Book.new
    @category = Category.new
    @author = Author.new
    @publisher = Publisher.new
  end

  def create
    @book = Book.new(book_params)
    if @book.save
      flash[:notice] = "Book Created"
      redirect_to books_path
    else
      render 'new'
    end
  end

  def index
    @books = Book.all
  end

  private
    def book_params
      params.require(:book).permit(:title, :category_id, :author_id, :publisher_id, :isbn, :price, :buy, :format, :excerpt, :pages, :year, :coverpath)
  end
end

Thanks a lot for your help

1 Answer 1

2

Remove the = on your book.each iteration:

<% @books.each_slice(4) do |book| %>
  <div class="row">
    <% book.each do |book| %>
      ...

<% ... %> just evaluated, not printed.

<%= ... %> evaluated and printed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.