Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在RubyonRails中发送ajax请求?_Ruby On Rails_Ruby_Testing_Rspec_Capybara - Fatal编程技术网

Ruby on rails 如何在RubyonRails中发送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

我正在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
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