简体   繁体   中英

Routing error in ajax on ruby on rails

I have this in my .html.erb code:

 $.ajax({
 url: "/timeMachineEdit",
 data: {editTimeMachine: newArray},
 type: 'POST',
 success: function (res) {
     if (res.result === 'ok') {
     alert('Data saved');
     } else {
     alert('Save error');
     }
},
error: function () {
     alert('Save error.');
     }
});

This in my datasets_controller.rb

def timeMachineEdit 
    @dataset = current_user.dataset
    @dataset.machine_time = params[:editTimeMachine]
end

And in my routes.rb:

match "/timeMachineEdit", to: "datasets#timeMachineEdit"

But when is submited shows:

POST http://localhost:3000/timeMachineEdit 500 (Internal Server Error) 

Where is the problem here? is the routes in the ajax url or something else?

The problem is in your route definition....

try match "/timeMachineEdit", to: "datasets#timeMachineEdit"

I think, it will still not work because of the nature of format..In the datasets_controller try the following code...

def timeMachineEdit 
  @dataset = current_user.dataset
  @dataset.machine_time = params[:editTimeMachine]
  respond_to do |format|
    format.js 
  end
end

Also change the dataType of your AJAX request to "script" in order to correctly match it with format.js else the format will be / which will select the first format that you will specify in the respond_to block..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM