【发布时间】:2019-01-03 09:47:29
【问题描述】:
简短而甜蜜,我在新的 Rails 部署 (5.2.2) 上关注 this guide 完全,使用 rails g scaffold thing 生成应用程序。
但是,在指南中的Targeted Changes段落下进行部分_show.html.erb后,单击“更改下方”link_to在测试时不会呈现部分。我没有收到错误消息,Rails 告诉我一切正常,但是当我单击链接时网站上没有任何反应。
我错过了什么?
代码:
controllers/things_controller
class ThingsController < ApplicationController
def index
@things = Thing.all
end
def show
end
end
config/routes.rb
Rails.application.routes.draw do
root 'things#index'
resources :things
end
things/index.html.erb
<%# /app/views/things/index.html.erb %>
<%= link_to 'Change Below', thing_path(42), remote: true %>
<div id="target-for-change">
Now you see "ME"!
</div>
things/show.js.erb
<%# /app/views/things/show.js.erb %>
$("#target-for-change").html("<%= j render(partial: 'show') %>");
things/_show.html.erb
<%# /app/views/things/_show.html.erb %>
Now you don't!
浏览器控制台输出:
Uncaught ReferenceError: $ is not defined
at <anonymous>:1:1
at processResponse (rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:268)
at rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:196
at XMLHttpRequest.xhr.onreadystatechange (rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:251)
【问题讨论】:
-
什么是响应浏览器控制台?
-
你在使用
turbolinks吗?如果是,请在删除turbolinks后尝试 -
@TalhaJunaid,我已经添加了浏览器控制台输出。
-
@G.B,删除 turbolinks 并不能解决问题,同样的行为。
-
@tmo 我已经添加了答案,请查看这个
标签: ruby-on-rails ajax ruby-on-rails-5 partial-views