how can I use AJAX to pass JS variables to the controller?
I'm using Ruby 2.0 and Rails 4.
I need to pass the value of certain JS variable obtained by a JS function to a controller to add it as a record in DB, I'm currently using several datatables to display information, the user selects a row in a datatable and pressing the next button then get the id of the selected rows and add it to a table in the DB as a row and another datatable shown, the thing is I do not want neither to use forms nor that the page is refreshed, and that all information that is presented is loaded at startup in view, I do a "presave" of selection in each step (datatable) without the user knowing.
now I can get the values of the selected rows in the view and present then in a Hidden_field, i need to get the value that hidden_field in the controller
i have read than AJAX can do the work, but i've not worked with ajax before.
some code:
script.js (in coffee)
leaveAStepCallback =(obj, context) ->
alert("Leaving step " + context.fromStep + " to go to step " + context.toStep);
ret=false
if (context.fromStep==1 and context.toStep==2)
document.getElementById('selec1').value= fnGetIdsOfSelectedRows fnGetSelected(oTable1), oTable1 #//get selected rows
ret=true
in some_controller.rb need some like:
def pre_save
if params[:step]=='1' #hidden_Field with step number
@selrow=params[:selec1].to_s.split(',') #selected rows as '1,2,5,7'
i=0
while i<@selrow.length
[email protected](i).to_i #selected row number
addp=PreSaveTable.new #table to record each selected row
addp.id_row=pr
addp.save #save as row in table
i=i+1
end
end
end
some help?