Rails6 Link_to Delete shows Showview

余生长醉 提交于 2021-01-29 08:53:08

问题


Yesterday everthing worked fine. Before i recover the last state I would like to understand what actually breaks jquery data-method: delete and routes it to show view. By now i dont even know if jquery is working at all. Lightbox is working well, but testing jquery like this: Testing jquery in Rails 6 gives me a hidden div.

Ive added jquery by "yarn add jquery" - no errors

Ive added to my application.js:

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("jquery")
require("channels")
require("packs/lightbox")

and this to my environment.js:

const { environment } = require('@rails/webpacker')

const webpack = require('webpack')

environment.plugins.prepend('Provide', new webpack.ProvidePlugin({

$: 'jquery',

jQuery: 'jquery',

Popper: ['popper.js', 'default']

}))

module.exports = environment

The first mysterious behavior was, that i had to create a lightbox.js file in my pack directory. It didn't work whith an simple import in application.js .

The next thing is, that my link_to for the delete-methods breaks and shows the show view:

<%= link_to 'Delete', gallery, :class => "btn btn-danger btn-sm text-white", method: :delete, data: { confirm: "Are you sure?" } %>

which gives me the correct html:

<a class="btn btn-danger btn-sm text-white" data-confirm="Are you sure? 
This action will delete all image assignments to this gallery." rel="nofollow" data-method="delete" href="/galleries/21">Delete</a>

And here is the controller-method:

  # DELETE /galleries/1
  # DELETE /galleries/1.json
  def destroy
    @gallery.destroy
    respond_to do |format|
      format.html { redirect_to galleries_path, notice: 'Gallery was successfully deleted.' }
      format.json { head :no_content }
    end
  end

But it directs to show view, so i dont even know if jquery is working because it looks like it would not and still lightbox is working. Yesterday the link_to worked fine.

Is there the possibility that somehow any manifest-files are broken?


回答1:


Add this to enviroment.js

Rails: ['@rails/ujs']



回答2:


I created a new project and added the files one by one from the old one to the new one. The new one works perfect but i only added jquery through yarn. So it seems, that if you install several versions of jquery (jquery gem / jquery-rails / jquery yarn) and switch or delete them, the basic rails/ujs looses its function in rails 6. I wasnt able to restore it in the old project, but its nice to know, that you have to be careful in rails 6 overwriting rails basic fuctionality.



来源:https://stackoverflow.com/questions/63580499/rails6-link-to-delete-shows-showview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!