Reputation: 953
I have advertisement and message models with forms. Both forms submits using Ajax (remote => true).
Message form submits perfect. It stays in the same page and handles response, but advertisement form does not. Both form,controller and script code is the same. Just changes from message to advertisement..
Advertisement form.
<%= form_for(@advertisement, :html => {"data-parsley-validate" => true},remote: true, format: :json) do |f| %>
....
#lot of code here
....
<%end%>
Advertisement controller:
def create
@advertisement = Advertisement.new(advertisement_params.merge(service_ids: params[:service_ids]))
respond_to do |format|
if @advertisement.save
format.html { redirect_to @advertisement, notice: 'advertisement was successfully sent.' }
format.json { render json: @advertisement }
else
format.html { render action: 'new' }
format.json { render json: @advertisement.errors.full_messages, status: :unprocessable_entity }
end
end
end
Script:
$(document).ready(function() {
return $("#new_advertisement").on("ajax:success", function(e, data, status, xhr) {
alert("ok");
}).on("ajax:error", function(e, xhr, status, error) {
alert("Error registration");
});
});
After submit I am redirected to this link:
http://localhost:3000/lv/advertisements.json
And browser displays all parameters that has been sent:
{"id":45,"name":"dsad","subname":"dasd","country_id":1,"region_id":3,"age":18,"height":144,"phone_number":"26266262","weight":42,"description":"das","created_at":"2015-03-04T18:50:23.924+02:00","updated_at":"2015-03-04T18:50:23.924+02:00","in_blacklist":false,"admin_confirmed":false,"vip":false,"identifier":"42179","expiration":"2015-04-29T18:50:23.926+03:00","smsidentifier":null,"highlight":"2015-03-04T18:50:23.926+02:00","recomend":"2015-03-04T18:50:23.927+02:00","vip_highlight":"2015-03-04T18:50:23.927+02:00","vip_recomend":"2015-03-04T18:50:23.927+02:00","prolong":null,"description_ru":"dasdad","work1":"2","work2":"3","work3":"7","work4":"17","email":"[email protected]","can_add_review":true,"can_show_recomended":true,"paid":false,"token":"","user_id":null}
I checked, advertisement is created succesfully, but why this weird redirect happens only for advertisement ? Not messages too ?
Any help would be great. Thanks in advance.
Upvotes: 0
Views: 248
Reputation: 586
I would catch the success and error events with jquery on your script I would try something like this:
if @advertisement.save
format.html { redirect_to @advertisement, notice: 'advertisement was successfully sent.' }
format.js
else
format.html { render action: 'new' }
format.json { render status: :500 } //Set the error right now so you can catch it later on your script
Depending on your needs you can also do
if @advertisement.save
format.html { redirect_to @advertisement, notice: 'advertisement was successfully sent.' }
format.js, alert: "your success message"
else
format.html { render action: 'new' }
format.json { render nothing: true }, alert: "your error message" // so you don't have to catch any errors on the script
And on your script just use jquery to manage AJAX
$.ajax({
type: "POST",
url: "THE URL YOU ARE POSTING TO",
dataType: "json",
});
Upvotes: 1