0

Ok, so I'm trying to delete some Posts in my Ruby app. Sorry, I am only learning Ruby, so I might not see simple problems in code.

Here is my link_to from index.html.erb:

<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>

It doesn't work. My browser just opens the Post then. BUT, when I change to button_to like this:

<td><%= button_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>

it does work like it should. The same problem with deleting comments, but the problem is that browser identifies it as "show" action (which I surely don't have).

I did some research. And the problem might be that jquery_ujs wasn't uploaded properly, but I checked it — nothing's wrong. I really don't understand this case :((

I'll also add my code from other files here:

routes.rb:

Rails.application.routes.draw do

  get 'welcome/splash' 
  get 'landing/about'
  get 'posts/index'

  root 'landing#about'

  resources :posts do
    resources :comments
  end

  resources :subscribers

end

application.js:

    // This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.

import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
import "channels"

//= require jquery
//= require jquery_ujs

Rails.start()
Turbolinks.start()
ActiveStorage.start()

post_controller:

    class PostsController < ApplicationController
  before_action :set_post, only: %i[ show edit update destroy ]

  # GET /posts or /posts.json
  def index
    @posts = Post.all
  end

  # GET /posts/1 or /posts/1.json
  def show
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
  end

  # POST /posts or /posts.json
  def create
    @post = Post.new(post_params)

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

  # PATCH/PUT /posts/1 or /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: "Post was successfully updated." }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1 or /posts/1.json
  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: "Post was successfully destroyed." }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def post_params
      params.require(:post).permit(:name, :title, :description, :tag, :image)
    end
end

I also have manifest.json file in public/packs folder.

dbugger
  • 15,868
  • 9
  • 31
  • 33
  • Have you tried like this : `<%= link_to 'Destroy', post, data: { confirm: 'Are you sure?' , method: :delete} %>` – Emilien Baudet Oct 22 '21 at 17:09
  • Also as you are using webpack, you have to delete in `application.js` the `//= require` lines. – Emilien Baudet Oct 22 '21 at 17:20
  • Do you see any error in the browser's console? `button_to` works because it renders a form element that does NOT depend on javascript, while `link_to` needs rails-ujs javascript to do the `method: :delete` part (it creates a form with javascript on the fly). You probably have some issue in your javascript that's preventing rails-ujs to be initialized properly – arieljuod Oct 23 '21 at 18:46
  • One more thing, you already have `@rails/ujs` there, don't use jquery_ujs, rails-ujs is a more modern non-jquery version of it and it's the default now – arieljuod Oct 23 '21 at 18:48
  • What version of rails do you use? – installero Feb 10 '22 at 15:52

0 Answers0