又简短又甜蜜,我完全按照this guide 进行了新的Rails部署(5.2.2),使用rails g scaffold thing
生成了该应用程序。
但是,在指南中的目标更改段下进行了部分_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)
答案 0 :(得分:0)
您需要在项目中添加jquery。将此行添加到application.html.erb
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
或者您可以使用宝石jquery-rails
答案 1 :(得分:0)
正如您在控制台输出中看到的那样,未定义“ $”。 “ $”是jQuery的常用别名,因此当您这样做时:
$("#target-for-change").html("<%= j render(partial: 'show') %>");
您正在告诉jQuery查找某个标识符。问题是您没有在项目中包括该库。一种方法是在您的gemfile中添加以下行:
gem 'jquery-rails'
然后在您的资产/javascripts/application.js中
//= require jquery3