Delete link not working phoenix

三世轮回 提交于 2019-12-09 00:28:58

问题


I've used Phoenix's built in gen.HTML to generate a simple view but it's not working

<%= link "Delete", to: event_path(@conn, :delete, event), method: :delete, data: [confirm: "Are you sure?"], class: "btn btn-danger btn-xs" %>

And on the page it looks like it suppose to but it just append # to address

Generated structure:

<form action="/event/1" class="link" method="post">
   <input name="_method" type="hidden" value="delete">
   <input name="_csrf_token" type="hidden" value="BwUSGQcDO1MwPzw0HBgqLnshHn8HNgAAnCTjuMt0viFshobX4XM/dQ==">
   <a class="btn btn-danger btn-xs" data-confirm="Are you sure?" data-submit="parent" href="#">Delete</a>
</form>

Am i missing a js import of sort? I can also this being downloaded by the browser:

//This is being downloaded as phoenix_html.js
// Although ^=parent is not technically correct,
// we need to use it in order to get IE8 support.
var elements = document.querySelectorAll('[data-submit^=parent]')
var len = elements.length

for (var i=0; i<len; ++i) {
  elements[i].addEventListener('click', function(event){
    var message = this.getAttribute("data-confirm")
    if(message === null || confirm(message)){
      this.parentNode.submit()
    };
    event.preventDefault()
    return false
  }, false)
}

回答1:


Edit: This happens to be known issue. Here is the fix:

In brunch-config.js
autoRequire: {
  "js\\app.js": ["web/static/js/app"]
}

Source




回答2:


While not the particular solution to OP's issue, this error can also arise if you place

<script src="<%= static_path(@conn, "/js/app.js") %>"></script>

in the the wrong place in app.html.eex. (For example inside <head> instead of at the end of the body)




回答3:


For anyone else hitting this I had a completely different problem that manifested itself in the same way, will drop this here in case it helps:

It turns out I accidentally deleted import "phoenix_html" from the app.js file mistaking it for being part of the generated boilerplate, adding it back fixed my issue.




回答4:


I recently had the same issue, it turns out that I had accidentally deleted the <script src="<%= static_path(@conn, "/js/app.js") %>"></script> from my layout.html.eex file. Adding it back fixed the issue.




回答5:


For later versions of Phoenix, if you view the app.js file within priv/static/js/app.js you'll notice a comment that references a link to a raw github js file. This small snippet of Javascript contains important code to ensure that the delete link helpers actually work correctly.



来源:https://stackoverflow.com/questions/34272706/delete-link-not-working-phoenix

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