索引输出不在.html.erb文件中的内容 [英] index outputs content that isn't in the .html.erb file
问题描述
我在rails教程中使用ruby来构建一个使用数据库表的简单应用程序,出于某种原因,我的主页输出超出了应有的效果。它创建这个数组,看起来像使用提示进行数据库查询并将其放在页面上。我很确定问题出在我的index.html.erb文件(如果我清空文件并重新加载奇怪的内容不存在),但我无法弄清楚发生了什么。 这里是什么发生的截图。
控制器代码:
class BooksController< ApplicationController
def new
@page_title ='Add Book'
@book = Book.new
@category = Category.new
@author = Author.new
@publisher = Publisher.new
结束
def创建
@book = Book.new(book_params)
@ book.save
redirect_to books_path
结束
def更新
结束
def编辑
结束
def destroy
end
def index
@books = Book.all
end
def show
end
私人
def book_params
params.require(:book).permit(:title,:category_id,:author_id,:publisher_id,:isbn,:price,:buy,:format,:摘录, :pages,:year,:coverpath)
结束
结束
html:
< div id =books-index>
<%@ books.each_slice(4)do | book | %GT;
< div class =row>
<%= book.each do | book | %GT;
< 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>
我是ruby和ruby的新手,所以如果我需要发布更多资源或信息为了让我的问题更清楚,请让我知道。感谢您的帮助
您应该将 book.each
<%
标记,而不是<%=
标记:
<%books.each do | book | %GT;
现在,您输出 books.each
(它是 books
数组)到你的HTML中。
I'm following a ruby on rails tutorial to build a simple application using database tables and for some reason my home page outputs more than it should. It creates this array that looks like a database query made with prompt and puts it on the page. I'm pretty sure the problem is with my index.html.erb file (if I empty the file and reload the weird content isn't there), but I can't figure out what's going on. Here's a screenshot of what happens.
controller code:
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)
@book.save
redirect_to books_path
end
def update
end
def edit
end
def destroy
end
def index
@books = Book.all
end
def show
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
html:
<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>
I am new to ruby and ruby on rails, so if I need to post any more resources or info in order to make my question clearer please let me know. Thanks for the help
You should put book.each
call inside of <%
tag, instead of <%=
tag:
<% books.each do |book| %>
Now, you print the result of books.each
(which is books
array) into your HTML.
这篇关于索引输出不在.html.erb文件中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!