【问题标题】:Brackets showing posts info in rails?在rails中显示帖子信息的括号?
【发布时间】:2017-09-04 00:46:05
【问题描述】:

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

Example=> "PHOTOGRAM 上的第一篇文章![#]"

帖子控制器

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

app/views/posts/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 %>

【问题讨论】:

  • 我不知道这是否能解决您发布的问题,但&lt;%= @posts.each do |post| %&gt; 应该使用&lt;% 作为开始标签。

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


【解决方案1】:

将您的 index.html.erb 更改为

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

(注意=已被删除)

在 ruby​​ 中,每个语句都有一个返回值。它可能是nil,但没有void 作为已知的其他编程语言。 而 ERB 的开始标签 &lt;%= 将输出以下 ruby​​ 代码。所以这行代码也会输出@posts中的所有帖子。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-20
    • 2017-12-09
    相关资源
    最近更新 更多