0

I have two models: Article and Paragraph. One Article can include much Paragraphs. Article model:

class Article < ApplicationRecord
  #enum category: {wiki: "Wiki", rules: "Rules", blog: "Blog", draft: "Draft"}
  enum category: [:wiki, :rules, :blog, :draft]
  after_initialize :set_default_category, :if => :new_record?

  def set_default_category
    self.category ||= :draft
  end

  belongs_to :user
  has_many :paragraphs, dependent: :destroy
  accepts_nested_attributes_for :paragraphs, allow_destroy: true, reject_if: proc { |attr| attr['content'].blank? }
  has_one_attached :article_image
end

Paragraph model:

class Paragraph < ApplicationRecord
  belongs_to :article
  has_one_attached :pragraph_image
  has_rich_text :content
end

Article controller:

class Admin::ArticlesController < ApplicationController
  before_action :set_article, only: %i[ show edit update destroy ]

  def index
    @articles = Article.all
  end

  def show
  end

  def new
    @article = Article.new
  end

  def edit
    authorize [:admin, Article], :edit?
  end

  def create
    @article = current_user.articles.build(article_params)
    authorize [:admin, Article], :create?

    respond_to do |format|
      if @article.save
        format.html { redirect_to admin_article_path(@article), notice: "Article was successfully created." }
        format.json { render :show, status: :created, location: @article }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    authorize [:admin, Article], :update?
    respond_to do |format|
      if @article.update(article_params)
        @article.paragraphs.build
        format.html { redirect_to admin_article_path(@article), notice: "Article was successfully updated." }
        format.json { render :show, status: :ok, location: @article }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    authorize [:admin, Article], :destroy?
    @article.destroy

    respond_to do |format|
      format.html { redirect_to admin_articles_path, notice: "Article was successfully destroyed." }
      format.json { head :no_content }
    end
  end

  private
    def set_article
      @article = Article.find(params[:id])
    end

    def article_params
      params.require(:article).permit(:article_image, :name, :description, :category, :user_id,
                                      paragraphs_attributes: [:_destroy, :id, :content] )
    end
end

Paragraph controller:

class Admin::ParagraphsController < ApplicationController
  before_action :set_article
  before_action :set_paragraph, only: %i[ update destroy ]

  def update
    authorize [:admin, Article], :update?
    respond_to do |format|
      if @paragraph.update(article_params)
        format.html { redirect_to admin_article_path(@article), notice: "Article was successfully updated." }
        format.json { render :show, status: :ok, location: @article }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    authorize [:admin, Article], :update?
    @paragraph.destroy
    respond_to do |format|
      format.html { redirect_to admin_article_path(@article), notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    def set_article
      @article = Article.find(params[:article_id])
    end

    def set_paragraph
      @paragraph = @article.paragraphs.find(params[:id])
    end


    def article_params
      params.require(:paragraph).permit(:content, :article_id)
    end
end

my link in show.html.haml (view article, where I can add/edit/delete paragraphs):

= link_to 'X', admin_article_paragraph_path(@article, paragraph),
                         :method => :delete,
                         data: { "turbo-method": :delete, confirm: "Are you sure?" },
                         :class => "btn btn-danger"

Routes (admin section):

  # Admin routes
  namespace :admin do
    root to: 'dashboard#index'
    resources :users

    resources :articles do
      resources :paragraphs, only: [:update, :destroy]
    end
  end

So, when I try to delete Paragraph also deleting Article. what I see in console:

Started DELETE "/admin/articles/62/paragraphs/42" for 127.0.0.1 at 2022-02-08 16:50:10 +0300
Processing by Admin::ParagraphsController#destroy as TURBO_STREAM
  Parameters: {"article_id"=>"62", "id"=>"42"}
  User Load (0.8ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 0], ["LIMIT", 1]]
  Article Load (1.4ms)  SELECT "articles".* FROM "articles" WHERE "articles"."id" = $1 LIMIT $2  [["id", 62], ["LIMIT", 1]]
  ↳ app/controllers/admin/paragraphs_controller.rb:29:in `set_article'
  Paragraph Load (0.5ms)  SELECT "paragraphs".* FROM "paragraphs" WHERE "paragraphs"."article_id" = $1 AND "paragraphs"."id" = $2 LIMIT $3  [["article_id", 62], ["id", 42], ["LIMIT", 1]]
  ↳ app/controllers/admin/paragraphs_controller.rb:33:in `set_paragraph'
  TRANSACTION (0.2ms)  BEGIN
  ↳ app/controllers/admin/paragraphs_controller.rb:20:in `destroy'
  ActiveStorage::Attachment Load (0.6ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 42], ["record_type", "Paragraph"], ["name", "pragraph_image"], ["LIMIT", 1]]
  ↳ app/controllers/admin/paragraphs_controller.rb:20:in `destroy'
  ActionText::RichText Load (0.4ms)  SELECT "action_text_rich_texts".* FROM "action_text_rich_texts" WHERE "action_text_rich_texts"."record_id" = $1 AND "action_text_rich_texts"."record_type" = $2 AND "action_text_rich_texts"."name" = $3 LIMIT $4  [["record_id", 42], ["record_type", "Paragraph"], ["name", "content"], ["LIMIT", 1]]
  ↳ app/controllers/admin/paragraphs_controller.rb:20:in `destroy'
  ActiveStorage::Attachment Load (0.2ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3  [["record_id", 17], ["record_type", "ActionText::RichText"], ["name", "embeds"]]
  ↳ app/controllers/admin/paragraphs_controller.rb:20:in `destroy'
  ActionText::RichText Destroy (15.7ms)  DELETE FROM "action_text_rich_texts" WHERE "action_text_rich_texts"."id" = $1  [["id", 17]]
  ↳ app/controllers/admin/paragraphs_controller.rb:20:in `destroy'
  Paragraph Destroy (4.5ms)  DELETE FROM "paragraphs" WHERE "paragraphs"."id" = $1  [["id", 42]]
  ↳ app/controllers/admin/paragraphs_controller.rb:20:in `destroy'
  TRANSACTION (16.0ms)  COMMIT
  ↳ app/controllers/admin/paragraphs_controller.rb:20:in `destroy'
Redirected to http://127.0.0.1:3000/admin/articles/62
Completed 302 Found in 158ms (ActiveRecord: 55.6ms | Allocations: 11283)


Started DELETE "/admin/articles/62" for 127.0.0.1 at 2022-02-08 16:50:11 +0300
Processing by Admin::ArticlesController#destroy as TURBO_STREAM
  Parameters: {"id"=>"62"}
  User Load (13.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 0], ["LIMIT", 1]]
  Article Load (4.9ms)  SELECT "articles".* FROM "articles" WHERE "articles"."id" = $1 LIMIT $2  [["id", 62], ["LIMIT", 1]]
  ↳ app/controllers/admin/articles_controller.rb:60:in `set_article'
  TRANSACTION (0.1ms)  BEGIN
  ↳ app/controllers/admin/articles_controller.rb:50:in `destroy'
  Paragraph Load (0.3ms)  SELECT "paragraphs".* FROM "paragraphs" WHERE "paragraphs"."article_id" = $1  [["article_id", 62]]
  ↳ app/controllers/admin/articles_controller.rb:50:in `destroy'
  ActiveStorage::Attachment Load (0.8ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 62], ["record_type", "Article"], ["name", "article_image"], ["LIMIT", 1]]
  ↳ app/controllers/admin/articles_controller.rb:50:in `destroy'
  Article Destroy (25.7ms)  DELETE FROM "articles" WHERE "articles"."id" = $1  [["id", 62]]
  ↳ app/controllers/admin/articles_controller.rb:50:in `destroy'
  TRANSACTION (97.2ms)  COMMIT
  ↳ app/controllers/admin/articles_controller.rb:50:in `destroy'
Redirected to http://127.0.0.1:3000/admin/articles
Completed 302 Found in 160ms (ActiveRecord: 142.1ms | Allocations: 5243)


Started DELETE "/admin/articles" for 127.0.0.1 at 2022-02-08 16:50:11 +0300
  
ActionController::RoutingError (No route matches [DELETE] "/admin/articles"):
  
Started GET "/admin/articles/62" for 127.0.0.1 at 2022-02-08 16:50:11 +0300
Processing by Admin::ArticlesController#show as HTML
  Parameters: {"id"=>"62"}
  User Load (1.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 0], ["LIMIT", 1]]
  Article Load (7.0ms)  SELECT "articles".* FROM "articles" WHERE "articles"."id" = $1 LIMIT $2  [["id", 62], ["LIMIT", 1]]
  ↳ app/controllers/admin/articles_controller.rb:60:in `set_article'
Completed 404 Not Found in 29ms (ActiveRecord: 8.5ms | Allocations: 2156)


  
ActiveRecord::RecordNotFound (Couldn't find Article with 'id'=62):
  
app/controllers/admin/articles_controller.rb:60:in `set_article'

Where are my mistakes? Thanks

2
  • ActionController::RoutingError (No route matches [DELETE] "/admin/articles"). My guess is that paragraph is nil in that case. Also look into shallow nesting. Commented Feb 8, 2022 at 16:11
  • I think you need to send an id in your nested attributes params. Commented Feb 8, 2022 at 17:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.