繁体   English   中英

为什么在我的博客应用程序上显示此内容? 〜Ruby on Rails〜我不怎么删除网页结尾处的奇怪内容

[英]Why it display me this on my blog application? ~Ruby on Rails~I don't how to remove a strange thing at the end of the web page

我试图使用Rails制作一个简单的博客应用程序。

在写完所有想要的代码和内容之后,它会在最后显示:

[#<  Post id: 2, title: "Another post", body: "this is the second post", created_at: "2017-11-19 15:02:23", updated_at: "2017-11-19 15:02:23">, #<Post id: 1, title: "First post", body: "lorem ipsum", created_at: "2017-11-19 14:55:58", updated_at: "2017-11-19 14:55:58">]

(我在2个测试职位之前做出了标题为“第一职位”和“另一个职位”,并在正文上带有一些随机文本的测试职位)

而且我不怎么删除它。

这是我的“ routes.rb”文件

Rails.application.routes.draw do
  resources :posts
  root "posts#index"
end

“ application.html.erb”:

<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
<%= csrf_meta_tags %>

<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>

<body>
  <div id="sidebar">
    <div id="logo">
      <%= link_to root_path do%> 
        <%= image_tag "logo.svg" %> 
      <% end %> 
    </div>

    <ul> 
      <li class="category">Website</li>
      <li><%= link_to "Blog", root_path %></li>
      <li>About</li>
    </ul>

    <ul>
      <li class="category">Social</li>
      <li><a href="https://www.facebook.com/kreker71">Facebook</a></li>
      <li><a href="https://github.com/Kreker71">GitHub</a></li>
      <li><a href="mailto:ursache.codrut71@gmail.com">Gmail</a></li>
    </ul>
   </div>

  <%= yield %>
  </body>
  </html>

“ posts_controller.rb”:

class PostsController < ApplicationController
  def index
    @posts = Post.all.order('created_at DESC')
  end

  def new
  end

  def create
    @post = Post.new(post_params)
    @post.save

    redirect_to @post 
  end

  def show 
    @post = Post.find(params[:id])
  end

  private 

  def post_params 
    params.require(:post).permit(:title, :body)
  end
end

“ index.html.erb”:

<%= @posts.each do |post| %>
  <div class="post_wrapper">
    <h2 class="title"><%= link_to post.title, post %></h2>
    <p class="date"><%= post.created_at.strftime("%B, %d, %Y") %></p>
  </div>
<% end %>

“ new.html.erb”:

<h1>New Post</h1>

<%= form_for :post, url: posts_path do |f| %>
  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %><br>
  </p>

  <p>
    <%= f.label :body %><br> 
    <%= f.text_area :body %><br>
  </p>

  <p>
    <%=f.submit %>
  </p>
<% end %>

“ show.html.erb”:

<h1 class="title">
  <%= @post.title %>
</h1>

<p class="date">
  Submitted <%= time_ago_in_words(@post.created_at) %> Ago
</p>

<p class="body">
  <%= @post.body %>
</p>

从index.html.erb上的<%=上删除=

<% @posts.each do |post| %>
  <div class="post_wrapper">
    <h2 class="title"><%= link_to post.title, post %></h2>
    <p class="date"><%= post.created_at.strftime("%B, %d, %Y") %></p>
  </div>
<% end %>

说明: =用于打印某些内容。 因此,在那一行您正在打印@posts数组,这就是您所看到的。 仅在需要输出某些东西时才使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM