1

I'm trying to pass a parameter through a url in an Ajax request that's triggered by a confirmation dialogue. I'd like to fetch the value of that parameter in my Rails controller given a successful request but I haven't been able to do it.

I've tried so far the following code:

Here my Ajax request where I've been adding the param in the URL plus other params in data

function continueSave() {
  var name = $('#leader_name').val();
  var persisted_time = $('#leader_time').val();

  $.ajax({
    type: "POST",
    url: "/leaderboards/1/?test=1",
    data: { leader: { name: name, time: time } },
    success: success

  });
}

Here the dialogue-related JS

function nameAlert() {
  return confirm("Name already exists. Would you like to continue?");
};

(function() {
  if (nameAlert()) {
    continueSave();
  }
else {
    //something else here
   }
})();

Although the request successfully reaches the controller there's params[:test] is nil in the controller.

I've also tried to pass the test param in data, but it is not working either.

Would appreciate some help.

The relevant controller action (leaders_controller.rb)

  def create
    @leader = Leader.new(leader_params)
    @leader.leaderboard_id = @leaderboard.id
    @name_repeated = [email protected]_by(name: @leader.name).nil?
    @check = params[:test]

     if params[:test].nil? && @name_repeated == true
       render :template => 'leaders/repeated_name.js.erb'
     else
      @leader.save
      @rank = @leader.rank_in(@leaderboard)

      respond_to do |format|
        if @leader.save
          format.html { redirect_to root_path }
          format.js { }
        else
        end
      end
    end
  end

Note: 1.- 'leaders/repeated_name.js.erb' contains the code for the Ajax request 2.- In routes Leader resource is nested within Leaderboard resource

5
  • try with request headers - headers : { "Accept" : "application/json", "Content-Type" : "application/json" } Commented Dec 13, 2016 at 8:31
  • Thanks Ruben. I've just started to experiment with Ajax so I'm afraid I wouldn't even know how to do that. If you have a couple of minutes to spare could you provide further guidance? Commented Dec 13, 2016 at 8:40
  • you just need to add that property to your request object: $.ajax({ type: "POST", url: "/leaderboards/1/?test=1", headers : { "Accept" : "application/json", "Content-Type" : "application/json" }, data: { leader: { name: name, time: time } }, success: success }); Commented Dec 13, 2016 at 8:42
  • Maybe you can use type: "GET" and pass all your data by GET? Show us your controller code related to this, please. Commented Dec 13, 2016 at 8:48
  • Thanks Ruben I've tried the headers but had the same result. Post request goes through but no params in url. Oskar I've posted the relevant bit of my controller as requested. The GET request wouldn't go through since I'm posting data, right? Commented Dec 13, 2016 at 8:59

2 Answers 2

1

Sorry guys I found the mistake. It was a dumb one. I have shallow nested routes so that leaders is nested in leaderboards, therefore I was using the incorrect path for the request. It should be:

  $.ajax({
    type: "POST",
    url: "/leaderboards/1/leaders?test=1",
    data: { leader: { name: name, time: time } },
    success: success

  });

I should have caught that before sorry for wasting your time.

Sign up to request clarification or add additional context in comments.

Comments

0

I'll post here - my comments are getting too long.

Try adding { type: 1} to your data and changing type to GET. Rails params contain both GET and POST data - you don't even have to change your controller.

function continueSave() {
    var name = $('#leader_name').val();
    var persisted_time = $('#leader_time').val();

    $.ajax({
        type: "GET",
        url: "/leaderboards/1/",
        data: { leader: { name: name, time: time }, test: 1 },
        success: success
    });
}

2 Comments

Hi Oskar, thanks I just tried that without success. I'll give it a second try
Actually Oskar I just noticed the request is not going through even without the param in the URL. I checked if it was reaching the controller with Pry and was sure it was reaching the controller, but maybe I was wrong. Could you see any issues with the request even without the test param

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.