嘿,大家我有一個問題來設置我的應用程序。它使用shopify API,基本上它是通過視圖獲取一些數據並將其發送到控制器,但我有問題將其傳遞給控制器中的另一個方法以使用API來保存數據。Rails Ajax調用:將JSON傳遞給單獨的控制器方法
這裏是我的代碼:
控制器
class BuilderController < ShopifyApp::AuthenticatedController
def index
urlval = request.fullpath
@urlCheck = urlval.split('/').last
end
def show
url = request.fullpath
@urlID = url.split('/').last
@customers = ShopifyAPI::Customer.search(query: "id:"+ @urlID)
#need to get a way to retrieve the ajax call info here to pass into the update
end
def updateCustomer(notes)
@customers.each do |cus|
cus.note = notes
cus.save()
end
end
def new
notes = params[:notes]
updateCustomer(notes)
render json: notes
end
end
查看
<button id="test">TEST</button>
<script>
var butt = document.getElementById('test');
butt.addEventListener("click",function(){
$.ajax({
url: "/builder/new",
type: "GET",
data: {
"notes": [
"test",
"test2"
]
},
success: function(data,text,xhr) {
console.log(text);
console.log(xhr);
console.log(data);
alert('successfully');
},
error: function(data,error){
console.log(data);
console.log(error);
alert("help");
}
});
});
</script>
的動作是我updatecustomer方法實現? – dpat