'Action Text Comments are not displaying attached image
I am having issues displaying an attached image to a comment, which is using action text. Below I have included some snippets in hopes someone can point out what Im doing wrong. I have install Action_text, which install active_storage, and comments are displaying fine. When I create a comment with an attachment it seems like it saves fine, as when I go into the console, I can pull a record by calling comment.body_media, but I cant get it to display.
https://github.com/smarandache1990/blog if you want to clone it and take a look.
<%= form_with model: [ @article, @article.comments.build ] do |form| %>
<p>
<%= form.label :commenter %><br>
<%= form.text_field :commenter %>
</p>
<p>
<%= form.label :body %><br>
<%= form.rich_text_area :body %>
</p>
<p>
<%= form.label :status %><br>
<%= form.select :status, ['public', 'private', 'archived'], selected: 'public' %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<div class="p-5 mb-4 bg-light rounded-3">
<div class="container-fluid py-5 rounded-3">
<h2 class="display-5 fw-bold"> <strong> <%= comment.commenter %> </strong> </h2>
<p class="col-md-8 fs-4">
<%= comment.body %>
<%#= image_tag comment.body_media %>
</p>
<hr>
<% if comment.updated_at - comment.created_at > 1 %>
<span>Edited <%= time_ago_in_words(comment.updated_at) %> ago.</span>
<% end %>
<span>Posted <%= time_ago_in_words(comment.created_at) %>.</span>
<% if comment.user == current_user %>
<%= link_to "Delete", [comment.article, comment], data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
}, class:"btn btn-secondary btn-sm" %>
<%= link_to "Edit", edit_article_comment_url(@article), class:"btn btn-primary btn-sm" %>
<% end %>
</div>
</div>
class CommentsController < ApplicationController
before_action :authenticate_user!
#http_basic_authenticate_with name: "dhh", password: "secret", only: :destory
def new
@comment = Comment.new
end
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.new(comment_params)
@comment.user = current_user
@comment.save
redirect_to article_path(@article)
end
def edit
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
end
def update
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
if @comment.update(comment_params)
redirect_to @article
else
redirect_to :edit_article_comment, status: :unprocessable_entity
end
end
def destroy
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
if ownership_check(@comment.user)
@comment.destroy
redirect_to article_path(@article), status: 303
else
redirect_to @article, status: :unprocessable_entity, notice: "You cant destroy what doesn't belong to you!"
end
end
private
def comment_params
params.require(:comment).permit(:commenter, :status, :body, :body_media)
end
def ownership_check(user)
user == current_user
end
end
ActiveRecord::Schema.define(version: 2022_04_08_161714) do
create_table "action_text_rich_texts", force: :cascade do |t|
t.string "name", null: false
t.text "body"
t.string "record_type", null: false
t.bigint "record_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["record_type", "record_id", "name"], name: "index_action_text_rich_texts_uniqueness", unique: true
end
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.bigint "record_id", null: false
t.bigint "blob_id", null: false
t.datetime "created_at", precision: 6, null: false
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
end
create_table "active_storage_blobs", force: :cascade do |t|
t.string "key", null: false
t.string "filename", null: false
t.string "content_type"
t.text "metadata"
t.string "service_name", null: false
t.bigint "byte_size", null: false
t.string "checksum"
t.datetime "created_at", precision: 6, null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end
create_table "active_storage_variant_records", force: :cascade do |t|
t.bigint "blob_id", null: false
t.string "variation_digest", null: false
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "status"
t.integer "views", default: 0
t.integer "user_id", null: false
t.index ["user_id"], name: "index_articles_on_user_id"
end
create_table "comments", force: :cascade do |t|
t.string "commenter"
t.integer "article_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "status"
t.integer "user_id", null: false
t.index ["article_id"], name: "index_comments_on_article_id"
t.index ["user_id"], name: "index_comments_on_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", precision: 6
t.datetime "remember_created_at", precision: 6
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "name"
t.integer "views", default: 0
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "articles", "users"
add_foreign_key "comments", "articles"
add_foreign_key "comments", "users"
end
irb(main):002:0> comment = Comment.first
Comment Load (1.7ms) SELECT "comments".* FROM "comments" ORDER BY "comments"."id" ASC LIMIT ? [["LIMIT", 1]]
=>
#<Comment:0x00007fd878321628
...
irb(main):003:0> comment.body
ActionText::RichText Load (1.8ms) SELECT "action_text_rich_texts".* FROM "action_text_rich_texts" WHERE "action_text_rich_texts"."record_id" = ? AND "action_text_rich_texts"."record_type" = ? AND "action_text_rich_texts"."name" = ? LIMIT ? [["record_id", 1], ["record_type", "Comment"], ["name", "body"], ["LIMIT", 1]]
Rendered /usr/local/lib/ruby/gems/3.0.0/gems/actiontext-7.0.1/app/views/action_text/contents/_content.html.erb within layouts/action_text/contents/_content (Duration: 9.5ms | Allocations: 1019)
=>
#<ActionText::RichText:0x00007fd87b28e118
id: 19,
name: "body",
body: #<ActionText::Content "<div class=\"trix-conte...">,
record_type: "Comment",
record_id: 1,
created_at: Tue, 12 Apr 2022 22:18:47.466148000 UTC +00:00,
updated_at: Tue, 12 Apr 2022 22:18:47.466148000 UTC +00:00>
irb(main):004:0> comment.body_media
=>
#<ActiveStorage::Attached::One:0x00007fd878c0fd70
@name="body_media",
@record=
#<Comment:0x00007fd878321628
id: 1,
commenter: "kdkdkd",
article_id: 1,
created_at: Tue, 12 Apr 2022 22:18:47.426703000 UTC +00:00,
updated_at: Tue, 12 Apr 2022 22:18:47.469176000 UTC +00:00,
status: "public",
user_id: 2>>
irb(main):005:0>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|