jqueryruby-on-railsajaxroutesactioncontroller

Rails Send with AJAX parameters to custom action in controller from the view


I having a trouble with an action. I want to send from view to a custom controller action, but I getting the error:

Routing Error No route matches [GET] "/send_email_representing"

In console I'm lokking:

/send_email_representing?data_1=asas%2C.cs%2Csasn&data_2=a%2Cs.a%2Cc.d.cdsmoidnndvsodsn-nqw928h3%26Siabic&data_3=33

The custom action

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

The Function on view

$("#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;

});

The route

get "send_email_representing" => "users#send_email_representing", :as => :send_email_representing

I don't know if I defined wrong the route, and I have to put the parameters, or it is not GET if not POST, or the action in controller, but I noticed that the params is sending are correct, for this in the error page:

Request
Parameters:
{"data_1"=>"asas,.cs,sasn", "data_2"=>"a,s.a,c.d.cdsmoidnndvsodsn-nqw928h3&Siabic", "data_3"=>"33"} 

Command make routes

admin_send_email_representing GET    /admin/send_email_representing(.:format)    admin/users#send_email_representing

I hope that can help me with this. Thanks.


Solution

  • Based on the output of your rake routes command, the route you're making a request to is scoped within the admin scope, so you should accordingly change your JS to:

      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({