Home>

I'm thinking of creating a community site using Active Storage, but when I make a page transition after posting,
I get an error
The error message is as follows

ActionView :: Template :: Error (undefined method `images' for nil: NilClass):

Problem source code

Below, the model for posting

class Post <
 ApplicationRecord
    has_many_attached: images
    belongs_to: user, optional: true
end

Below is the user model

class User <
 ApplicationRecord
  # Include default devise modules. Others available are:
  #: confirmable,: lockable,: timeoutable,: trackable and: omniauthable
  devise: database_authenticatable,: registerable,
         : recoverable,: rememberable,: validatable
  # Include default devise modules. Others available are:
  #: confirmable,: lockable,: timeoutable,: trackable and: omniauthable
  has_many: posts
  has_one: profile, dependent:: destroy
  delegate: name,: leaning_histry,: purpoose,: image, to:: profile
end

I will put the controller from here

Below, the controller for posting

class PostsController <
 ApplicationController
  before_action: authenticate_user!
  before_action: find_post, only: [: edit,: update,: show,: destroy]
    def index
        @posts= Post.all
      end
      def new
      @post= Post.new
    end
    def edit
      @post= Post.find (post_params)
    end
    def create
      return redirect_to new_profile_path, alert: "Register profile" if current_user.profile.blank?
      @post= current_user
      @post= Post.create params.require (: post) .permit (: content, images: [])
      binding.pry
      if @ post.save
        redirect_to root_path, notice:'Posted successfully'
        else else
          render: new
        end
      end
      def update
        if @ post.update (post_params)
          redirect_to root_path
        else else
          render: edit
        end
      end
      def destroy
        if @ post.destroy
          redirect_to root_path, alert:'Deleted post'
        else else
          redirect_to root_path
        end
      end
      private
        def post_params
          params.require (: post) .permit (: content, images: [])
        end
        def find_post
          @post= Post.find (params [: id])
        end
        def force_redirect_unless_my_post
          return redirect_to root_path, alert:'You don't have permission' if @ post.user!= current_user
        end
end

This is the new post page

<
div class='post-content' >
   ≪
%= form_with model: @post, local: true do | f |% >
    ≪
%= f.text_area: content,: placeholder= >
 "What kind of feeling is it now?"% ≫
    ≪
%= f.file_fifffeld: images, direct_upload: true, multiple: true% >
    ≪
div class='submit-block' >
       ≪
%= f.submit'submit', class: "button"% >
    ≪
/div >
   ≪
% end% >
≪
/div >

This is the page to display the post list

<
div class= "content-wrapper" >
    ≪
div class= "content-block" >
        ≪
% @ posts.each do | post |% >
        ≪
div class= "content" >
         ≪
div class= "user-about" >
         ≪
div class= "image" >
             ≪
%= image_tag'user.JPG'% >
         ≪
/div >
         ≪
div class= "profile" >
            ≪
div class= "name-history" >
                ≪
div class= "name" >
                    taka
                ≪
/div >
                ≪
div class= "mania-histry" >
                    Mania history: 1 year
                ≪
/div >
             ≪
/div >
             ≪
div class= "enjoy-point" >
                 Fun point: It's fun to see what you make with your own hands
             ≪
/div >
         ≪/div > <
/div >
         ≪
div class= "text" >
             ≪
p >
≪
% post.content% >
≪
/p >
         ≪
/div >
         ≪
% if @ post.images.attached?% >
         ≪
div class='images' >
         ≪
% @ post.images.each do | image |% >
         ≪
%= image_tag image% >
 ≪
br >
         ≪
/div >
         ≪
% end% >
         ≪
% end% >
         ≪
div class= "action-menu" >
             ≪
div class= "like" >
             ≪
/div >
             ≪
div class= "comment" >
             ≪
/div >
         ≪
/div >
        ≪
/div >
        ≪
% end% >
    ≪
/div >
    ≪
div class= "sidebar" >
     ≪
div class= "box" >
     ≪
/div >
     ≪
div class= "box" >
    ≪
/div >
    ≪
/div >
≪
/div >

# # Below is the model part

Below is a model for posting

class Post <
 ApplicationRecord
    has_many_attached: images
    belongs_to: user, optional: true
end

This is the user model

class User <
 ApplicationRecord
  # Include default devise modules. Others available are:
  #: confirmable,: lockable,: timeoutable,: trackable and: omniauthable
  devise: database_authenticatable,: registerable,
         : recoverable,: rememberable,: validatable
  # Include default devise modules. Others available are:
  #: confirmable,: lockable,: timeoutable,: trackable and: omniauthable
  has_many: posts
  has_one: profile, dependent:: destroy
  delegate: name,: mania_histry,: enjoy_point,: image, to:: profile
end

What I tried

I tried to retrieve the post on console by doing post.all with rails c, but I tried to retrieve it, but the image did not come out, so it is probably not saved in the DB. I think.

Another is
It was written in the article that I investigated to run genereta resource when implementing Active Storage.
Below
[[Rails 5.2] How to use Active Storage] [1]: https://qiita.com/hmmrjn/items/7cc5e5348755c517458a

I wasn't running that command at that time,
I'm wondering if that is the cause, but I haven't found a clue to the solution.

rails csopost.allI tried to retrieve the post on the console, but the image did not come out.

I have the string f.file_fifffeld, is this a typo? Or is this code actually working? What exactly happened when the rails console says "images don't come out"? I think it will be a clue to the solution if you copy and paste the executed command and the result.

Yusuke Sangenya2021-03-26 18:21:58