Ruby on rails 如何在RubyonRails中发送ajax请求?
我正在javascript homepage.js文件中发送ajax请求Ruby on rails 如何在RubyonRails中发送ajax请求?,ruby-on-rails,ruby,testing,rspec,capybara,Ruby On Rails,Ruby,Testing,Rspec,Capybara,我正在javascript homepage.js文件中发送ajax请求 function GotoHomePage() { var URL = 'homepage/1'; $.ajax({ url : URL, success : function() { }, error:function(){ } }); } 控制器代码产品\u Controller.rb文件: def homepage @val="here is query" end Rout
function GotoHomePage() {
var URL = 'homepage/1';
$.ajax({
url : URL,
success : function() {
},
error:function(){
}
});
}
控制器代码产品\u Controller.rb文件:
def homepage
@val="here is query"
end
Routes.rb文件:
match "homepage/1" => 'products#homepage'
Rspec产品\u控制器\u spec.rb文件:
describe ProductDetailsController do
render_views
describe '#homepage' do
before { xhr :get, 'homepage/1' }
it { response.status.should == 200 }
end
end
但我犯了一个错误
before { xhr :get, 'homepage/1' }
ActionController::RoutingError:
No route matches {:controller=>"products", :action=>"homepage/1"}
试着从
match "homepage/1" => 'products#homepage'
到
然后在控制器中,您可以获取参数[:id]
编辑:
也许可以将您的测试更改为:
describe 'homepage' do
it 'should be successful' do
get 'homepage',:id=>1
response.should be_success
end
end
get“homepage/:id”=>“products#homepage”
会更安全您的URL变量(在JS中)是什么homepage01/1
-也就是说,为什么01
在那里?@Dana先生,尽管如此,他在运行时还是出错了spec@ted我不是说硬编码的1
与在路线中使用:id
相比。URI的基不是仍然错误吗homepage
而不是homepage01
?@user2244199,可以肯定的是,您想要获得http://localhost:3000/homepage/1
路线,对吗?@Dana先生,是的,你说得100%对。我的意思是,这个js方法不是引发异常的方法
describe 'homepage' do
it 'should be successful' do
get 'homepage',:id=>1
response.should be_success
end
end