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
type: "GET"and pass all your data byGET? Show us your controller code related to this, please.