gpt4 book ai didi

ruby-on-rails - 如何在 Ruby on Rails 中发送 ajax 请求?

转载 作者:行者123 更新时间:2023-11-28 20:56:23 25 4
gpt4 key购买 nike

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

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

Controller 代码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' 

然后在 Controller 中你可以获取参数[: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com