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

最佳答案

尝试从更改路线

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

关于ruby-on-rails - 如何在Ruby on Rails中发送Ajax请求?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15881835/

10-10 02:12