Rails link_to with remote: true processing html instead of js after page refresh

前端 未结 4 1009
自闭症患者
自闭症患者 2021-02-04 07:40

I have a search page in my app, where there is an ajax search form. The search form works properly, passing parameters to the model to filter the search, and the model returning

相关标签:
4条回答
  • 2021-02-04 08:18

    Does your app/assets/javascripts/application.js contain

    //= require jquery
    //= require jquery_ujs 
    

    ?

    And your erb contains <%= javascript_include_tag "application" %>?

    I was just struggling with a problem like this for HOURS and the last of those two points fixed it; I saw the first point mentioned in some other questions so I'll repeat it here.

    Hope that helps.

    (Credit where credit's due)

    0 讨论(0)
  • 2021-02-04 08:29

    What solver it for me was adding :format => "js"

    So in your case:

    <%=link_to "Message", message_user_path(:id => user.id, :format => "js"), :remote => true %>
    
    0 讨论(0)
  • 2021-02-04 08:33

    In general, when you use link_to on a particular button or so, when you press the button, as js request is send to the controller, but also searches for the respective .js.erb file and so on.

    0 讨论(0)
  • 2021-02-04 08:38

    My solution was to replace

    format.json do
    

    with

    format.js do
    

    you can troubleshoot the request by setting a breakpoint (i use pry) in the controller and then look at the variable

    request.format
    
    0 讨论(0)
提交回复
热议问题