Rails Rails 5.2.2 jQuery 用不了?

yan32768 · March 14, 2019 · Last by yan32768 replied at March 15, 2019 · 1701 hits

比如 jquery 的选择器$(#id) 不能使用,得到的是一堆的代码? 有一年多没用过 rails 了,求教高手。项目使用 rails 默认配置。应用模板是这样的 <!DOCTYPE html>

xxxxx <%= csrf_meta_tags %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> 。。。

页面内嵌入的 js,还是 coffee 里写的,选择器都没办法正确识别带 id 的元素。

require './rails/app/models/user'

task :create_user do i = 1 while i != 2 ::User.create(:name=>"Cathy") ++ i end end

task :create_comment do

end

rake aborted!
LoadError: cannot load such file -- ./rails/app/models/user
C:/Users/ThinkPad/Desktop/Demo-test/rails/lib/tasks/user.rake:1:in `require'
C:/Users/ThinkPad/Desktop/Demo-test/rails/lib/tasks/user.rake:1:in `<top (required)>'
C:/Users/ThinkPad/Desktop/Demo-test/rails/Rakefile:6:in `<top (required)>'
(See full trace by running task with --trace)

请问这个咋回事?不好 i 意思 我这边没法发布

引用 railsapp 的模型,应该这么写 require "/config/environment.rb"

Reply to yan32768

改成

require "/config/environment.rb"
task :create_user do
  i = 1
  while i != 2
    ::User.create(:name=>"Cathy")
    ++ i
  end
end

task :create_comment do

end

还是会出现:

C:\Users\ThinkPad\Desktop\Demo-test\rails>bundle exec rake create_user
rake aborted!
LoadError: cannot load such file -- /config/environment.rb
C:/Users/ThinkPad/Desktop/Demo-test/rails/lib/tasks/user.rake:1:in `require'
C:/Users/ThinkPad/Desktop/Demo-test/rails/lib/tasks/user.rake:1:in `<top (required)>'
C:/Users/ThinkPad/Desktop/Demo-test/rails/Rakefile:6:in `<top (required)>'
(See full trace by running task with --trace)

5.2 没有默认安装 jquery gem 你需要安装 gem 然后在 application.js 里引用才可以用

Reply to yfscret

gemfile 里有 jquery-rails (4.3.3) app js 里有 //= require jquery //= require rails-ujs //= require turbolinks 请问需要单独再引用 jquery 吗? 感谢你提醒,我去查一下使用说明

Reply to yfscret

jquery-rails 这个 gem 的说明里,已经包含了 jquery,里面只是提到 5.1 以上版本,不需要写//= require rails-ujs 这一行。

You need to Sign in before reply, if you don't have an account, please Sign up first.