我正在对ajax进行测试(因为我对ajax非常陌生)是将数据从view welcome(index.html.erb)视图发送到控制器(welcome_controller.rb),但是我尝试的所有时间都在显示我同样的错误:
但是我不知道这是什么意思,并且在我执行请求时,视图一直向我显示警报“错误”(错误)。
这是我在视图中所做的:
<%= javascript_include_tag "application" %>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
$(document).ready(function(){
$("#petition").click(function(){
$.ajax({
url: 'http://192.168.99.100:3000/welcome_controller/index/',
type: 'POST',
dataType: 'json',
data: { max_id: {id: "5"} },
success: function (data) {
alert("success");
},
error: function(xhr, status, error) {
alert(xhr+status+error); \\to take the second photo i change this for this "alert(JSON.stringify(xhr));"
}
});
});
});
这是我的路线。rb:
Rails.application.routes.draw do
resources :welcome do
collection do
get 'index'
end
end
get 'welcome/page'
root 'welcome#index'
get 'menu/index'
end
这是我的application.js:
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require tinymce
//= require rails-ujs
//= require turbolinks
//= require_tree .
这是红宝石宝石:
# frozen_string_literal: true
source 'https://rubygems.org'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?('/')
"https://github.com/#{repo_name}.git"
end
#propiedades de usuario
gem 'devise', '~> 4.4', '>= 4.4.1'
#text editor to post with link and imagens
gem 'tinymce-rails'
#admin y moderator
gem 'cancancan'
gem 'rolify'
#votar like y dislike
gem 'acts_as_votable', '~> 0.11.1'
#consultas rails para mesclar con js
gem 'jquery-rails'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.5'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: %i[mri mingw x64_mingw]
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '~> 2.13'
gem 'selenium-webdriver'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'web-console', '>= 3.3.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
我的控制器名称是:welcome_controller.rb 并具有2个方法索引,并显示我无论如何都将放置控制器:
class WelcomeController < ApplicationController
before_action :set_welcome, only: [:show]
protect_from_forgery :except => :receive_guest
helper_method :current_or_guest_user
def index
puts params[:max_id]
end
def show
end
end
答案 0 :(得分:0)
当gem文件中包含jquery gem时 您可以使用
包括jquery库//= require jquery
在application.js中
您已经完成的操作。
您需要从视图中删除这些行
<script src="jquery-3.3.1.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
并将其替换为此行
<%= javascript_include_tag "application" %>