Thursday, June 13, 2024
HomeRuby On RailsTurbo 8 | Drifting Ruby

Turbo 8 | Drifting Ruby


# Terminal
rails action_text:set up
rails g scaffold posts title
rails g mannequin remark submit:belongs_to session
rails g controller feedback
# app/fashions/submit.rb
class Put up < ApplicationRecord
  has_many :feedback, dependent: :destroy

  has_rich_text :content material
  has_one_attached :poster
  broadcasts_refreshes
finish
# app/views/posts/_form.html.erb
  <div class="mb-3">
    <%= type.label :poster, class: 'form-label' %>
    <%= type.file_field :poster, class: 'form-control' %>
  </div>

  <div class="mb-3">
    <%= type.label :content material, class: 'form-label' %>
    <%= type.rich_text_area :content material, class: 'form-control' %>
  </div>
# app/controllers/posts_controller.rb
def post_params
  params.require(:submit).allow(:title, :poster, :content material)
finish
# app/views/posts/present.html.erb
<%= turbo_stream_from @submit %>

<%= render @submit %>

<h2>Feedback</h2>

<div id="feedback">
  <%= render partial: "feedback/remark", assortment: @submit.feedback, locals: { submit: @submit } %>
</div>

<div data-turbo-permanent>
  <%= render partial: "feedback/new", locals: { submit: @submit, remark: @submit.feedback.new } %>
</div>
# app/views/posts/_post.html.erb
<div id="<%= dom_id submit %>" class="scaffold_record position-relative">
  <% if submit.poster.hooked up? %>
    <%= image_tag submit.poster.variant(resize_to_fill: [1600, 300]), class: "img-fluid" %>
  <% finish %>

  <h1 class="position-absolute top-50 start-50 translate-middle text-white text-center">
    <%= submit.title %>
  </h1>
</div>

<%= submit.content material %>
# config/routes.rb
sources :posts do
  sources :feedback, solely: [:create, :destroy]
finish
# app/controllers/comments_controller.rb
class CommentsController < ApplicationController
  before_action :set_post

  def create
    @remark = @submit.feedback.new(comment_params)
    @remark.session = session.id
    @remark.save
    # redirect_to @submit
    # render turbo_stream: [
    #   turbo_stream.replace(
    #     "new_comment",
    #     partial: "comments/new",
    #     locals: { post: @post, comment: Comment.new }
    #   ),
    #   turbo_stream.append(
    #     "comments",
    #     partial: "comments/comment",
    #     locals: { post: @post, comment: @comment }
    #   )
    # ]
  finish

  def destroy
    @remark = @submit.feedback.discover(params[:id])
    @remark.destroy
    redirect_to @submit
  finish

  non-public

  def set_post
    @submit = Put up.discover(params[:post_id])
  finish

  def comment_params
    params.require(:remark).allow(:content material)
  finish
finish
# app/views/feedback/_new.html.erb
<%= form_with mannequin: [post, comment], id: :new_comment do |f| %>
  <%= f.rich_text_area :content material %>
  <%= f.submit class: "mt-3 btn btn-primary" %>
<% finish %>
# app/views/feedback/_comment.html.erb
<%= content_tag :div, id: dom_id(remark), class: "card mb-3" do %>
  <div class="card-body">
    <p class="card-text"><%= remark.content material %></p>
    <p class="card-text">
      <small class="text-muted">
        Posted <%= time_ago_in_words remark.created_at %> in the past |
        Posted by <%= remark.session %>
        <% if session.id.to_s == remark.session %>
          <%= link_to "Delete", [post, comment], "data-turbo-method": :delete %>
        <% finish %>
      </small>
    </p>
  </div>
<% finish %>
# app/fashions/remark.rb
class Remark < ApplicationRecord
  belongs_to :submit
  has_rich_text :content material

  # broadcasts_to :submit
  broadcasts_refreshes_to :submit
finish
# Gemfile
gem "turbo-rails", "2.0.0-beta.2"
# package deal.json
"@hotwired/turbo-rails": "^8.0.0-beta.2",
# app/views/layouts/software.html.erb
<%# turbo_refreshes_with technique: :substitute, scroll: :reset %>
<%= turbo_refreshes_with technique: :morph, scroll: :protect %>
<%= yield :head %>
# app/views/feedback/create.turbo_stream.erb
<%= turbo_stream.substitute(
        "new_comment",
        partial: "feedback/new",
        locals: { submit: @submit, remark: Remark.new }) %>

<%= turbo_stream.append(
        "feedback",
        partial: "feedback/remark",
        locals: { submit: @submit, remark: @remark }) %>
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments