Когда я хочу создать коментарий под постом, в блоге и чтоб там выводился ник пользователя, но выводится такая ошибка
NoMethodError in Posts#show
Showing /home/ali/Рабочий стол/relsa/app/views/posts/show.html.erb where line #31 raised:
undefined method `user' for nil:NilClass
Extracted source (around line #31):
<% @post.comments.each do |comment| %>
<div class="alert alert-light">
<p>Пользователь <%=@comments.user.username%></p>
<p><strong><%=comment.username%></strong> <br> <%=comment.body%> <%= image_tag comment.image.url(:thumb), class: 'img-show' if comment.image? %> </p>
Вот код show.html.erb
<% if user_signed_in? %>
<span>Здравствуйте, <%= current_user.username %></span>
<%= link_to 'Выйти', destroy_user_session_path, :method => :delete %>
<% else %>
<%= link_to 'Войти', new_user_session_path %> или <%= link_to 'Зарегистрироваться', new_user_registration_path %>
<% end %>
<%=link_to "Главная страница", home_path %>
<%=link_to "Добавления статьи", new_post_path %>
<h3>Пользователь <%=@post.user.username%></h3>
<h1><%=@post.title %> </h1>
<p><%=@post.body %></p>
<p>
<%= image_tag @post.image.url(:thumb), class: 'img-show' if @post.image? %>
</p>
<hr>
<% if user_signed_in? && @post.user==current_user %>
<%= link_to "Редактировать", edit_post_path(@post), :class=> 'btn btn-warning' %>
<%= link_to "Удалить пост", post_path(@post), method: :delete, data: {confirm:"Вы хотите удалить статью ?"}, :class => 'btn btn-danger'%>
<%end%>
<% if @post.comments.size > 0 %>
<h2>Вcе коментарии <%=@post.comments.count %> </h2>
<% end %>
<% @post.comments.each do |comment| %>
<div class="alert alert-light">
<p>Пользователь <%=@comments.user.username%></p>
<p><strong><%=comment.username%></strong> <br> <%=comment.body%> <%= image_tag comment.image.url(:thumb), class: 'img-show' if comment.image? %> </p>
</div>
<% if user_signed_in? %>
<%= link_to "Удалить Коммент", post_comment_path(@post, comment), method: :delete, data: { confirm: "Вы хотите удалить?" }, :class => 'btn btn-danger'%>
<%= link_to "Редактировать комент", edit_post_comment_path(@post, comment), :class=> 'btn btn-warning' %>
<% end %>
<% end %>
<% if user_signed_in? %>
<%=form_for([@post, @post.comments.build]) do |f| %>
<p>
Пользователь<br>
<%=f.text_field(:username)%>
</p>
<p>
Текст комментария<br>
<%= f.text_area(:body)%>
</p>
Код comments_controller.rb
class CommentsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
def create
@post=Post.find(params[:post_id])
@comments.user_id = current_user.id
@comments=@post.comments.create(comment_params)
redirect_to post_path(@post)
end
def destroy
@post = Post.find(params[:post_id])
@post.comments.find(params[:id]).destroy
redirect_to post_path(@post)
end
def update
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
if (@comment.update(comment_params))
redirect_to post_path(@post)
else
render 'edit'
end
end
def edit
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
end
private def comment_params
params.require(:comment).permit(:username, :body, :image)
end
end
comment.rb
class Comment < ApplicationRecord
mount_uploader :image, ImageUploader
validates:username, presence:true, length:{minimum:5}
validates:body, presence:true, length:{minimum:5}
belongs_to :post
belongs_to :user
end
user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable,:confirmable, :validatable
has_many :posts
has_many :comments
validates :username, presence: :true, uniqueness: { case_sensitive: false }
end
schema.rb
ActiveRecord::Schema.define(version: 2019_03_23_152808) do
create_table "comments", force: :cascade do |t|
t.string "username"
t.text "body"
t.integer "post_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image"
t.integer "user_id"
t.index ["post_id"], name: "index_comments_on_post_id"
end
create_table "posts", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image"
t.integer "user_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["username"], name: "index_users_on_username", unique: true
end
end