Ruby on rails Rails在使用ajax表单时突然出现错误
我制作了一个按钮,单击该按钮可创建一个Ruby on rails Rails在使用ajax表单时突然出现错误,ruby-on-rails,ajax,routes,Ruby On Rails,Ajax,Routes,我制作了一个按钮,单击该按钮可创建一个shopping\u cart\u项目,然后重定向到显示删除该shopping\u cart\u项目的按钮的同一页面。这辆车很好用。当我尝试使用ajax时,“从购物车中删除”按钮工作正常,但当我单击“添加到购物车”按钮时,突然出现以下错误: Started POST "/shopping_cart_items" for 127.0.0.1 at 2014-01-26 17:04:04 +0100 Processing by ListsController#c
shopping\u cart\u项目
,然后重定向到显示删除该shopping\u cart\u项目的按钮的同一页面。这辆车很好用。当我尝试使用ajax时,“从购物车中删除”按钮工作正常,但当我单击“添加到购物车”按钮时,突然出现以下错误:
Started POST "/shopping_cart_items" for 127.0.0.1 at 2014-01-26 17:04:04 +0100
Processing by ListsController#create as JS
Parameters: {"utf8"=>"✓", "course_id"=>"10", "listtype"=>"shopping_cart_items"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = '7c8d45b38c4739022112db6ec137f31f60fada60' LIMIT 1
(0.2ms) BEGIN
SQL (0.8ms) INSERT INTO "shopping_cart_items" ("course_id", "created_at", "updated_at", "user_id") VALUES ($1, $2, $3, $4) RETURNING "id" [["course_id", 10], ["created_at", Sun, 26 Jan 2014 16:04:04 UTC +00:00], ["updated_at", Sun, 26 Jan 2014 16:04:04 UTC +00:00], ["user_id", 1]]
(0.7ms) COMMIT
Rendered browse_courses/_uncart.html.erb (2.1ms)
Rendered lists/create.js.erb (3.9ms)
Completed 500 Internal Server Error in 15ms
ActionView::Template::Error (No route matches {:action=>"destroy", :listtype=>"shopping_cart_items", :controller=>"lists"}):
1: <%= form_tag({:controller => 'lists', :action => 'destroy', listtype: 'shopping_cart_items'}, method: 'delete', remote: true) do %>
2: <div><%= hidden_field_tag :course_id, params[:id] %></div>
3:
4: <%= button_tag(name: nil, class: 'btn btn-primary') do %>
app/views/browse_courses/_uncart.html.erb:1:in `_app_views_browse_courses__uncart_html_erb__2389702037921073695_70286519726020'
app/views/lists/create.js.erb:1:in `_app_views_lists_create_js_erb___3945578670069953364_70286487293260'
app/controllers/lists_controller.rb:6:in `create'
Rendered /Users/Jaap/.rvm/gems/ruby-2.0.0-p353@mprog1/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.5ms)
Rendered /Users/Jaap/.rvm/gems/ruby-2.0.0-p353@mprog1/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.4ms)
Rendered /Users/Jaap/.rvm/gems/ruby-2.0.0-p353@mprog1/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (15.6ms)
列表控制器:
listtype_regexp = Regexp.new([:shopping_cart_items, :currently_taking_items, :recently_viewed_items].join("|"))
resources :lists, only: [:create, :destroy], path: ':listtype', constraints: { listtype: listtype_regexp }
class ListsController < ApplicationController
def create
addToList!(params[:course_id], current_user.shopping_cart_items)
respond_to do |format|
format.html { redirect_to controller: 'browse_courses', action: 'show', type: 'courses', id: params[:course_id] }
format.js
end
end
def destroy
removeFromList!(params[:course_id], current_user.shopping_cart_items)
respond_to do |format|
format.html { redirect_to controller: 'browse_courses', action: 'show', type: 'courses', id: params[:course_id] }
format.js
end
end
end
我的假设正确吗?您描述为“用于向购物车添加项目的表单”的文件是browse\u courses/cart?此外:内部服务器错误是“/shopping\u cart\u items”post请求的一部分吗?你有没有试过“耙路”来看看到底有什么路?是的,这是正确的!我不知道你说的是什么意思:“内部服务器错误是“/shopping\u cart\u items”上post请求的一部分吗?我确实运行了“rake routes”查看上面的编辑我只是想了解问题:)在将ajax添加到应用程序并单击“添加到购物车”按钮时,会出现错误,对吗?您也可以在没有AJAX控件的情况下发布页面吗?:)
listtype_regexp = Regexp.new([:shopping_cart_items, :currently_taking_items, :recently_viewed_items].join("|"))
resources :lists, only: [:create, :destroy], path: ':listtype', constraints: { listtype: listtype_regexp }
class ListsController < ApplicationController
def create
addToList!(params[:course_id], current_user.shopping_cart_items)
respond_to do |format|
format.html { redirect_to controller: 'browse_courses', action: 'show', type: 'courses', id: params[:course_id] }
format.js
end
end
def destroy
removeFromList!(params[:course_id], current_user.shopping_cart_items)
respond_to do |format|
format.html { redirect_to controller: 'browse_courses', action: 'show', type: 'courses', id: params[:course_id] }
format.js
end
end
end
$("#cart_form").html("<%= escape_javascript(render('browse_courses/uncart')) %>")
$("#cart_form").html("<%= escape_javascript(render('browse_courses/cart')) %>")
Prefix Verb URI Pattern Controller#Action
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
lists POST /:listtype(.:format) lists#create {:listtype=>/shopping_cart_items|currently_taking_items|recently_viewed_items/}
list DELETE /:listtype/:id(.:format) lists#destroy {:listtype=>/shopping_cart_items|currently_taking_items|recently_viewed_items/}
browse_courses GET /:type(.:format) browse_courses#index {:type=>/courses|institutes|programmes|staff/}
browse_course GET /:type/:id(.:format) browse_courses#show {:type=>/courses|institutes|programmes|staff/}
root GET / static_pages#home
signup GET /signup(.:format) users#new
signin GET /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact