I'm thinking of creating a community site using Active Storage, but when I make a page transition after posting,
This will result in an error.
The error message is as follows
ActionView:: Template:: Error (undefined method `images' for nil: NilClass):
Below is the Post Model
class Post <ApplicationRecord
has_many_attached —images
belongs_to:user, optional:true
end
The following is the user model
class User<ApplicationRecord
# Include default device modules.Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
device:database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
# Include default device modules.Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
has_many —posts
has_one:profile, dependent::destroy
delete:name,:leaning_history,:purpoose,:image, to:profile
end
Below is 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
default
@post=Post.find(post_params)
end
def create
return redirect_to new_profile_path, alert: "Please register your profile" if current_user.profile.blank?
@post=current_user
@post=Post.create params.require(:post).permit(:content, images:[])
binding.ply
[email protected]
redirect_to root_path, notice: 'Post Successful'
else
render:new
end
end
default update
[email protected](post_params)
redirect_to root_path
else
render —edit
end
end
def destroy
[email protected] store
redirect_to root_path, alert: 'Post deleted'
else
redirect_to root_path
end
end
private
def post_params
param.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 do not have permissions' [email protected]!=current_user
end
end
This is the new posting page
<div class='post-content'>
<%=form_with model:@post,local:true do|f|%>
<%=f.text_area:content,:placeholder=> "What kind of Kanji is it now?"%>
<%=f.file_fiffeld:images,direct_upload:true,multiple:true%>
<div class='submit-block'>
<%=f.submit 'Post', class: "button"%>
</div>
<%end%>
</div>
This is the page where you can view a list of posts
<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 Corporation
</div>
<div class="mania-history">
Mania history: 1 year
</div>
</div>
<div class="enjoy-point">
Fun point: It's fun to see what you make move.
</div>
</div>
</div>
<div class="text">
<p><%post.content%>/p>
</div>
<%[email protected] tached?%>
<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 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 device modules.Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
device:database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
# Include default device modules.Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
has_many —posts
has_one:profile, dependent::destroy
delete:name,:mania_history,:enjoy_point,:image, to:profile
end
I tried to take out the post with post.all on railsc on the console, but the image didn't come out, so I think it's probably not saved on DB.
The other one is
When I found out that I would run the generator resource to implement Active Storage, it was mentioned in the article below.
[Rails 5.2]How to use Active Storage
I wasn't running that command at the time,
I'm wondering if that's the reason, but I haven't found a clue to the solution.
I tried post.all
on the console with railsc
, but I couldn't find any images.
As Yusuke Sangenya pointed out, after correcting the typo that was f.file_iffeld
, we successfully fixed the <%@post.images.each do | image | %>
and <%@post.images.each do | %>
.
Thank you.
© 2024 OneMinuteCode. All rights reserved.