我希望在按下按钮时,将AJAX调用发送到我的events_controller#check
操作。这是我的代码:
事件\ new.html.erb :
<button id="check-button" type="button">Check</button>
的application.js:
$(document).on('click', "#check-button", function(){
var checkList = []; //creates array to store customer ids
...
});
$.ajax({
url: '/events/check',
data:checkList
}
)
});
events_controller#检查
def check
checkList = params[:checkList]
...
end
的routes.rb
post 'events_controller/check' => 'events#check'
我在控制台上点击按钮时出现此错误:
Started GET "/events/check?undefined=" for 127.0.0.1 at 2016-02-03 11:36:33 +0000
AbstractController::ActionNotFound (The action 'show' could not be found for EventsController):
actionpack (4.1.8) lib/abstract_controller/base.rb:131:in `process'
actionview (4.1.8) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.8) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678:in `call'
...etc
为什么要尝试找到'show'而不是'check'?我该如何解决?感谢
答案 0 :(得分:1)
2个问题:
- 你的可能在你宣布的路线之前得到
show
(为什么会去POST
)- 您尚未调用
醇>#config/routes.rb resources :events do post :check, on: :collection #-> url.com/events/check end #app/assets/javascripts/application.js $(document).on('click', "#check-button", function(){ var checkList = []; //creates array to store customer ids ... $.ajax({ url: '/events/check', data: checkList, method: "POST" }); });
方法
您应该拥有以下内容:
<ul>
<li><a href="#c1">Tab 1</a></li>
<li><a href="#c2">Tab 2</a></li>
<li><a href="#c3">Tab 3</a></li>
<li><a href="#c4">Tab 4</a></li>
</ul>
<div id="c1">CONTENT TAB 1</div>
<div id="c2">CONTENT TAB 2</div>
<div id="c3">CONTENT TAB 3</div>
<div id="c4">CONTENT TAB 4</div>
答案 1 :(得分:0)
在我看来,如果请求中缺少类型,则需要发布请求,因此它会将GET
请求作为默认请求方法。
您需要将type
添加到您的ajax:
type:'post',
答案 2 :(得分:0)
1检查您的页面中是否正确导入了Jquery文件。
module RSpecMixin
include Rack::Test::Methods
def app() Sinatra::Application end
end
RSpec.configure do |config|
config.include RSpecMixin
end