托架在铁轨中显示帖子信息?

时间:2017-09-04 00:46:06

标签: ruby-on-rails ruby rubygems paperclip ruby-on-rails-5

我正在关注ruby on rails教程。我的帖子信息显示在帖子标题旁边的括号中。如果你知道如何解决它,请帮忙!

  

示例=> “关于PHOTOGRAM的第一篇文章![#<帖子ID:1,标题:”首先   在PHOTOGRAM上发帖!“,created_at:”2017-09-04 03:24:25“,updated_at:   “2017-09-04 03:24:25”,image_file_name:“instagram.jpg”,   image_content_type:“image / jpeg”,image_file_size:64169,   image_updated_at:“2017-09-04 03:24:24”>]“

发布控制器

class PostsController < ApplicationController

  def index
    @posts = Post.all
  end
  def new
    @post = Post.new
  end

  def create
    @post = Post.create(post_params)
    redirect_to posts_path
  end

  private

  def post_params
     params.require(:post).permit(:image, :caption)
  end
end

应用程序/视图/帖/ index.html.erb

<h1> Photogram </h1>

<%= @posts.each do |post| %>
  <%= image_tag post.image.url(:medium) %>
  <%= post.caption %>
<% end %>

new.html.erb

<%= form_for(@post) do |f| %>
  <%= f.file_field :image %>
  <%= f.text_field :caption %>
  <%= f.submit %>
<% end %>

1 个答案:

答案 0 :(得分:0)

index.html.erb更改为

<% @posts.each do |post| %>
  <%= image_tag post.image.url(:medium) %>
  <%= post.caption %>
<% end %>

(请注意已删除的=

在ruby中,每个语句都有一个返回值。它可能是nil但是已知其他编程语言没有void。 ERB开头标记<%=将输出以下ruby代码。因此,这行代码也将输出@posts中的所有帖子。

(这个答案基本上是“max pleaner”在他的评论中所说的,只是有一些评论和解释)