1

我在 javascript homepage.js 文件中发送 ajax 请求

function GotoHomePage() {
var URL = 'homepage/1';
$.ajax({
    url : URL,
    success : function() {
    },
    error:function(){
    }
});
}

控制器代码 products_controller.rb 文件:

def homepage
@val="here is query"
end

路线 routes.rb 文件:

match "homepage/1" => 'products#homepage' 

Rspec products_controller_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"}
4

1 回答 1

0

尝试更改路线

match "homepage/1" => 'products#homepage' 

match "homepage/:id" => 'products#homepage' 

然后在控制器中你可以获取 params[:id]

编辑:

也许将您的测试更改为:

describe 'homepage' do
  it 'should be successful' do
    get 'homepage',:id=>1
    response.should be_success
  end
end
于 2013-04-08T14:34:37.193 回答