Rails 使用 AJAX 参数从视图发送到控制器中的自定义操作
Rails Send with AJAX parameters to custom action in controller from the view
我在操作时遇到问题。我想从视图发送到自定义控制器操作,但出现错误:
Routing Error No route matches [GET] "/send_email_representing"
在控制台中,我在看:
/send_email_representing?data_1=asas%2C.cs%2Csasn&data_2=a%2Cs.a%2Cc.d.cdsmoidnndvsodsn-nqw928h3%26Siabic&data_3=33
自定义操作
def send_email_representing
@message = params[:data_1]
@subject = params[:data_2]
@user_representing = User.find(params[:data_3])
SendMessageToUserMailer.email(@user_representing, @message, @subject).deliver_now
render html: 'Good'.html_safe
end
视图中的函数
$("#sendEmailtoRepresenting").click(function() {
var subject = $("#form1").val();
var message = $("#form7").val();
alert(subject+" "+message);
var representing_id_1 = $("#hiddenEmailInput").val();
var data_value = [subject,message,representing_id_1];
console.log(JSON.stringify(data_value));
if ((subject != null && subject != "") && (message != null && message != "") && (representing_id_1 != null && representing_id_1 != ""))
{
url = "/send_email_representing";
$.ajax({
type: "GET",
//dataType: "json",
//http://0.0.0.0:3000/admin/send_email_representing/1,subject,message
url: url,
data: {data_1: data_value[0], data_2:data_value[1], data_3:data_value[2] },
contentType: "application/json",
success: function (data) {
if (data == "Good"){
displayEmailSuccesMenssage("Email has been sent");
}
},
error: function (data) {
console.log("Error: "+data);
alert("Error");
}
});
} else {
alert("Error");
}
return false;
});
路线
get "send_email_representing" => "users#send_email_representing", :as => :send_email_representing
不知道是不是我定义错了route,必须要放参数,不然不是GET if not POST,或者controller里面的action,但是我注意到params正在发送是正确的,为此在错误页面:
Request
Parameters:
{"data_1"=>"asas,.cs,sasn", "data_2"=>"a,s.a,c.d.cdsmoidnndvsodsn-nqw928h3&Siabic", "data_3"=>"33"}
命令制作路线
admin_send_email_representing GET /admin/send_email_representing(.:format) admin/users#send_email_representing
我希望这能帮到我。谢谢。
根据您的 rake routes 命令的输出,您发出请求的路由在 admin
范围内,因此您应该相应地将您的 JS 更改为:
if ((subject != null && subject != "") && (message != null && message != "") && (representing_id_1 != null && representing_id_1 != ""))
{
url = "<%= admin_send_email_representing_path %>"; //<-- add admin scope here
$.ajax({
我在操作时遇到问题。我想从视图发送到自定义控制器操作,但出现错误:
Routing Error No route matches [GET] "/send_email_representing"
在控制台中,我在看:
/send_email_representing?data_1=asas%2C.cs%2Csasn&data_2=a%2Cs.a%2Cc.d.cdsmoidnndvsodsn-nqw928h3%26Siabic&data_3=33
自定义操作
def send_email_representing
@message = params[:data_1]
@subject = params[:data_2]
@user_representing = User.find(params[:data_3])
SendMessageToUserMailer.email(@user_representing, @message, @subject).deliver_now
render html: 'Good'.html_safe
end
视图中的函数
$("#sendEmailtoRepresenting").click(function() {
var subject = $("#form1").val();
var message = $("#form7").val();
alert(subject+" "+message);
var representing_id_1 = $("#hiddenEmailInput").val();
var data_value = [subject,message,representing_id_1];
console.log(JSON.stringify(data_value));
if ((subject != null && subject != "") && (message != null && message != "") && (representing_id_1 != null && representing_id_1 != ""))
{
url = "/send_email_representing";
$.ajax({
type: "GET",
//dataType: "json",
//http://0.0.0.0:3000/admin/send_email_representing/1,subject,message
url: url,
data: {data_1: data_value[0], data_2:data_value[1], data_3:data_value[2] },
contentType: "application/json",
success: function (data) {
if (data == "Good"){
displayEmailSuccesMenssage("Email has been sent");
}
},
error: function (data) {
console.log("Error: "+data);
alert("Error");
}
});
} else {
alert("Error");
}
return false;
});
路线
get "send_email_representing" => "users#send_email_representing", :as => :send_email_representing
不知道是不是我定义错了route,必须要放参数,不然不是GET if not POST,或者controller里面的action,但是我注意到params正在发送是正确的,为此在错误页面:
Request
Parameters:
{"data_1"=>"asas,.cs,sasn", "data_2"=>"a,s.a,c.d.cdsmoidnndvsodsn-nqw928h3&Siabic", "data_3"=>"33"}
命令制作路线
admin_send_email_representing GET /admin/send_email_representing(.:format) admin/users#send_email_representing
我希望这能帮到我。谢谢。
根据您的 rake routes 命令的输出,您发出请求的路由在 admin
范围内,因此您应该相应地将您的 JS 更改为:
if ((subject != null && subject != "") && (message != null && message != "") && (representing_id_1 != null && representing_id_1 != ""))
{
url = "<%= admin_send_email_representing_path %>"; //<-- add admin scope here
$.ajax({