如何在 Rails 6.1 中使用 AJAX 请求执行带有属性的 ruby 函数?
How to execute ruby function with attributes using AJAX request in Rails 6.1?
我有以下家庭控制器:
class HomeController < ApplicationController
def index
@data = EmergencyFriend.all
@jsonData = JSON.pretty_generate(@data.as_json)
end
def about
end
def alertEmergencyContant
account_sid = "my id"
auth_token = "my token"
@client = Twilio::REST::Client.new(account_sid, auth_token)
@client.messages.create(
to: "+number 1",
from: "+number 2",
body: "hello world !"
)
end
end
基本上,在我的 home/index.html.erb
中只有一个按钮。当按下按钮时,它会显示一条警告消息,允许用户 select 选择发送短信的目标。
我想做的是在我的家庭控制器中调用 alertEmergencyContant
方法,以便我可以发送消息。我还想将 phone_number 作为该请求的参数传递。有人建议为此我应该使用 AJAX。我在我的 rails 项目中成功安装了 jquery 和 ajax 并且按预期工作。我无法理解的是如何将其创建为 POST 请求。
我的主目录路由列表是:
root GET / home#index
root GET /home/about(.:format) home#about
但是 alertEmergencyContant
上什么也没有。如何在路由中声明它并将其作为 POST 请求?如何使用 AJAX?
将属性从 JavaScript 传递到 ruby
这是我目前的 ajax 请求(有效):
$.ajax({
url: '/',
type: 'GET',
success: function(event){
alert("sending Message");
}
});
更新:
def about
@thisNumber = params[:phone_number]
puts "helllloooooooooooooo " + @thisNumber
end
function ajaxRequest(){
$.ajax({
url: 'home/about/?phone_number:1244211',
type: 'GET',
success: function(event){
alert("passed");
},
failed: function(){
alert("has failed")
},
done: function(){
alert("after")
}
});
}
您需要为您的操作添加路线
# routes.rb
post 'some_url' => 'home#alert_emergency_contact'
您现在可以在 javascript
中使用它
$.ajax({
url: '/some_url', // This needs to match what you choose in routes.rb
type: 'POST',
success: function(event){
alert("sending Message");
}
});
PS:动作名称是 Ruby 中的 always_snake_case,不是驼峰式
我有以下家庭控制器:
class HomeController < ApplicationController
def index
@data = EmergencyFriend.all
@jsonData = JSON.pretty_generate(@data.as_json)
end
def about
end
def alertEmergencyContant
account_sid = "my id"
auth_token = "my token"
@client = Twilio::REST::Client.new(account_sid, auth_token)
@client.messages.create(
to: "+number 1",
from: "+number 2",
body: "hello world !"
)
end
end
基本上,在我的 home/index.html.erb
中只有一个按钮。当按下按钮时,它会显示一条警告消息,允许用户 select 选择发送短信的目标。
我想做的是在我的家庭控制器中调用 alertEmergencyContant
方法,以便我可以发送消息。我还想将 phone_number 作为该请求的参数传递。有人建议为此我应该使用 AJAX。我在我的 rails 项目中成功安装了 jquery 和 ajax 并且按预期工作。我无法理解的是如何将其创建为 POST 请求。
我的主目录路由列表是:
root GET / home#index
root GET /home/about(.:format) home#about
但是 alertEmergencyContant
上什么也没有。如何在路由中声明它并将其作为 POST 请求?如何使用 AJAX?
这是我目前的 ajax 请求(有效):
$.ajax({
url: '/',
type: 'GET',
success: function(event){
alert("sending Message");
}
});
更新:
def about
@thisNumber = params[:phone_number]
puts "helllloooooooooooooo " + @thisNumber
end
function ajaxRequest(){
$.ajax({
url: 'home/about/?phone_number:1244211',
type: 'GET',
success: function(event){
alert("passed");
},
failed: function(){
alert("has failed")
},
done: function(){
alert("after")
}
});
}
您需要为您的操作添加路线
# routes.rb
post 'some_url' => 'home#alert_emergency_contact'
您现在可以在 javascript
中使用它$.ajax({
url: '/some_url', // This needs to match what you choose in routes.rb
type: 'POST',
success: function(event){
alert("sending Message");
}
});
PS:动作名称是 Ruby 中的 always_snake_case,不是驼峰式