Home>

I'm new to Rails.
post.user.image_name is displayed, but
post.post.post_image cannot be displayed due to a method error.
What's wrong?

index.html.erb
  <% @ posts.each do | post |%>                    <!-Add a value to the following src so that the user's image is displayed->  ">                 ">
                    <!-Use the link_to method to create a link to the user detail page->                  <% = link_to (post.content, "/ posts/# {post.id}")%>              <% end%>
posts_controller.rb
class PostsController</pre>
<strong>users_controller.rb</strong>
<pre><code data-language = "rails">class UsersController</pre>
<strong>application_record.rb</strong>
<pre><code data-language = "rails">class ApplicationRecord</pre>
<strong>like.rb</strong>
<pre><code data-language = "rails">class Like</pre>
<strong>post.rb</strong>
<pre><code data-language = "rails">class Post</pre>
<strong>user.rb</strong>
<pre><code data-language = "rails">class User</pre>
<strong>schema.rb</strong>
<pre><code data-language = "rails">#This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If i need to create the application database on another
# system, you should be using db: schema: load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord :: Schema.define (version: 2020_10_30_055415) do
  create_table "likes", force:: cascade do | t |
    t.integer "user_id"
    t.integer "post_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
  create_table "posts", force:: cascade do | t |
    t.text "content"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "user_id"
    t.string "post_image"
  end
  create_table "users", force:: cascade do | t |
    t.string "name"
    t.string "email"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "image_name"
    t.string "password"
  end
end

Postscript

In index.html.erb
">
">
I changed it to, and the error disappeared, but the image could not be displayed properly.

Changed if @ post.save for posts_controller.

  • Answer # 1

    posts_controller.rbAs far as you can see, the column name of the image for posting is probablypost_imageI think it has become.
    posts/index.html.erbHow about changing the corresponding code in to the following?

     ">
    ↓
       ">

    [Addition]
    posts_controller.rbofif @ post.saveWhy don't you change the following like this?

      if @ post.save
          if params [: post_image]
         @ Post.post_image = "# {@ post.id} .jpg"
         Post_image = params [: post_image]
         File.binwrite ("public/post_images/# {@ post.post_image}", post_image.read)
          end
          @ post.save
          flash [: notice] = "Created a post"
          redirect_to ("/ posts/index")
        else else
          render ("posts/new")
        end