I have an Event app, where I am using a calendar. I have created 2 external routes and controller action to #save and #load data and I have 2 templates for them. One is save.json.erb other one is load.json.erb. For now, I have hardcoded the load data from load.json.erb, looks like;
{"2015-05-06": {"available":"1",
"bind":0,
"info":"",
"notes":"",
"price":"20",
"promo":"",
"status":"available"
},
"2015-05-07": {"available":"1",
"bind":0,
"info":"",
"notes":"",
"price":"20",
"promo":"",
"status":"available"
}}
So, it shows the hardcoded data on the calendar.
My problem is, when I press submit, it does not give an error but it does not save it to database. I could not figure out the strong params either. I am using permit! for now to see it saves to database. Maybe my Event Model and its columns are wrong. Because I could not figure it out what it posts (integer, string etc..)
Here is my events_controller
....
def load
end
def save
#THIS IS WRONG I SHOULD PUT SMTH ELSE
#event = Event.new
#event.save
end
def event_params
params.require(:event).permit!
end
when I select data from calendar press submit it sends this;
HEADER:
event_id:0
event:{"2015-05-06":{"available":"1","bind":0,"info":"","notes":"","price":"20","promo":"","status":"available"},"2015-05-07":{"available":"1","bind":0,"info":"","notes":"","price":"20","promo":"","status":"available"},"2015-05-27":{"available":"1","bind":0,"info":"demo","notes":"demodemodemo","price":"15","promo":"2","status":"available"}}
Also from the log; Is this data JSON?, or I have to use JSON.parse somewhere?
Started POST "/events/save" for ::1 at 2015-05-02 18:39:42 +0300
Processing by EventsController#save as */* #HERE IT SENDS THE PARAMETERS, BUT I COULD NOT GET THEM
Parameters: {"event_id"=>"0", "event"=>"{\"2015-05-06\":{\"available\":\"1\",\"bind\":0,\"info\":\"\",\"notes\":\"\",\"price\":\"20\",\"promo\":\"\",\"status\":\"available\"},\"2015-05-07\":{\"available\":\"1\",\"bind\":0,\"info\":\"\",\"notes\":\"\",\"price\":\"20\",\"promo\":\"\",\"status\":\"available\"},\"2015-05-27\":{\"available\":\"1\",\"bind\":0,\"info\":\"demo\",\"notes\":\"demodemodemo\",\"price\":\"15\",\"promo\":\"2\",\"status\":\"available\"}}"}
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "events" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2015-05-02 15:39:42.823906"], ["updated_at", "2015-05-02 15:39:42.823906"]]
(2.4ms) commit transaction
Rendered events/save.json.erb (0.1ms)
Completed 200 OK in 21ms (Views: 12.9ms | ActiveRecord: 3.0ms)
When I try;
def save
#event = Event.new(event_params)
#event.save
end
def event_params
params.require(:event).permit!
end
It gives an error of ;
NoMethodError in EventsController#save
undefined method `permit!' for #<String:0x007fcbb99d24c0>
So I think it is posted as a string instead of a hash.
For me, there is something wrong with the data it sends to database or the database can not configured for these datas. Because of #save action has #events = Event.new all the time it creates new event in the database and saves it.So I have to get the POSTed data but I could not get those parameters. I think I have to take the POSTed data and with for loops I have to take those data and save it to database. Could not find a way. Hope someone helps
Here is the full HEADER:
Cache-Control:no-cache
Connection:Keep-Alive
Content-Length:0
Content-Type:application/json; charset=utf-8
Date:Sat, 02 May 2015 18:42:54 GMT
Server:WEBrick/1.3.1 (Ruby/2.2.1/2015-02-26)
Set-Cookie:_demo_session=Snh1Nm5CNWJzTHNITXp5SkRDejNmZzRXRkdoVm5SeEkwUW82WmE5QkFndThkeUdyTEdBK2s3ZVR1aEUxQXpGclpqR202WTIzSGkzNWtIQUdWZXFFeUtmSVpBbnZZNmJnN090YUxXS2c5eFFPVzZOaXJjTGJndGRsS0xBcUhaU0VlMFAvRVdtT05RMHpLcUYrSy9wSElnPT0tLUI2SWFiOHZXRlpqMmpteVloV3Zsb2c9PQ%3D%3D--3533fcf1e966b5c1e766dcc099caa6df4b5c284f; path=/; HttpOnly
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-Request-Id:7863cf16-0162-4760-8719-d155202eca7a
X-Runtime:0.030455
X-Xss-Protection:1; mode=block
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:3126
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:_Paint_session=NXN5NVUvemhXcHF2UnF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VzVXTVFoYmdPbTczOE02Wjk4N2hsaTJDRFdLY2pRc1NxYVRNVFBHdkFHbmEvb1U4aEN4T2FOVDZWZGlUMnpJMU0yNkdHNW1acVpYN3NzRkxwQkN2TzRzS0ZBNlMwMGtIMGJ5bWN2QVp5Q3cxQUNIVnExajFDSldsZWhJek1zUjJxVzJyRHRsUDBjQnJsdlB1RFE9PS0tSjViSGdocSs5blRLdTNnUk1GWk9qZz09--068ded1e78c2d70739361e662b995b5470a059c4; _raddit_session=Vy9USHZBa1FodGxON1BMOWFZOHBIazk3eWVSOXh0OW9yUjRmSjJ5Tmg1OXpsMnI2Tm5ZTGQwOHdEMVJnUWhFNG5jRVZEcVhLL3ZCSVdsM1g5S2FTMjYvZWo4U1ovT0xNdkZVT2dJS0VWTkRMcXdRQ0lzTlFRWC9EZ1ZxbDRtYmtLQ1dkZzM3THltSEFvdzJTN010MHVnYytJMzE1R2F0cUVkQlh2bERBRUM4c0NVNnFsTFdmbVB1cURJeDVHUkhKOWZUOG5pUlhFZkxOSkJ3c01uU2paTW50WXRrRW8zRmJtTTBrUm5GWlJvbz0tLUh4ckZqdnA3WGQzRjdmNnAzMXdMcmc9PQ%3D%3D--622d3f9adb9afb871e118d0715921aaf986f82d2; _Cal_session=cjRlSlc0Q2FITHJJTmlTbU5TZjk5aFNFaTczTHRQZW9TR0h1MG4xYVovZG9xaUxyNittWENjbXdib3FLc1J6cXh3dHA5YitaVkZYbFVtaFI2bGxUKzJyM1kvRzJlRFV4b0JibzJ1TTdQS0ZkdVE2eVJJSlZQVXY0Q0thS1NBRzhGUzlaQ1ZRaHppYzJES2V2eXRmVWtBPT0tLWEzVWFIcWl0WnJBRVdnanJ0em1Renc9PQ%3D%3D--03a65bd6c5338108d2d3db62295843f8b3e3eaaa; _fullcalendar_and_rails_example_session=M1h0eFJieVY1ek16OWRtR1JRUGc1eCtsdFZnTDdqWkd3NndWQTFuUzFKdHJpOHlRYSsweHZ6UGZubTV1eEdJZm14NVZxbE1IUHZzRG1id2NVeENERzFlaFF3NzFNWHk0RlltbS8yNmc2aEtnYjJDcGZmY3pZWG0vU0RsU29ST005NDVpdVpYSWtNTURkM3M2RmVvZU8vUStwdUxFNk1TaVJTRDY1Z2FKcEd4QkxZMlpGeUlsUGl5UlNCVWhPNk5qR082T25lZlZ4NkIxY012MjRJaVVTKzFqdUZFMUVjc1VxVnA0UXFwSVFEbW1acE9KMWw1N0xzQWJlYUd6R200Q1VCdkJjUWpTbElrbFhlZ2ZBdnY2WnB1NHdHU2tBQTlQandzekR5SmJkbnVhU08vNWpLNjFuY1UyVUxuZWt2ZWotLTY3TU9jZklCcVR6ZXZQRy9UUCtHTmc9PQ%3D%3D--9bcb70f5196a063e7915b11622953f7935d9166c; _takvim_session=WVJmQVpEbEJUOE9KeVdLK3ZyODRpejI0dzA2aWJPd2dqOGRCaysxOU5VT3lhVkNnNzNIZytvYlpqL2FRZGhTWXZFcWxLc3JYVmoyUVRibGFuelpCczl4eFVjaFVLdkZiRURpUC84cysyUldFbC9VT295SURnMkpDQnFtdldLdUhHeDhRL3NGOG90WS9mWUI1WitocndBPT0tLVp6Mk1VS2VaT0hmMGtwcnhNcnpYMkE9PQ%3D%3D--a215e9c8f635c5c638ccdcd55105c3bdc452760e; SQLiteManager_currentLangue=2; request_method=GET; _Last_session=VnErWEFwazE3dWFBdm94WUVhMXdYWFhKVnViUmtQWUFja0xENjNDTmYxWVAxbjVyVDZrK3pxRlVjUnp0MGQ3R240UWZSK2lNcld5bDVKM2ZYb3JwMTJ1QXpPa0p1cFJOYXFXUllIakVrdnBzVHh2V09YQlZGTHNjTWtNLzFDOXNTdUhYZGNXei80enRwclZkdzBCUzZFcUtEQVFqeDFock15anMrVnVIUHRRPS0tci9lSEMvNFBtVWJ5eW1BSTQ0VUpadz09--46ddca06b8e604506dcfff84e7cc8111c7a1b679; _demo_session=TzE2bi93R2E3ZXVNSHdkZ0MvVnRCTm5BeG1VQ0luMVQrWFJ5a1FhbTc4amNCSGFNazJYTVhVYlRxbEhGejlnREowTTJDVFByYkoveHRpeEx6SlZvRnpaQ3R3UEdWaVRlK25UK29SQmx5Nmplbk9Gb1NNZ0pYdm9PQ3FLeTFKZ25CdkF4USt0clpGN1dMUkxrekxTM3lRPT0tLTZoQ1F4UHg3VExsa3JaMWRxQ1IvTFE9PQ%3D%3D--a430ecd15a69103694b06ec430875e21b66f5e62
Host:localhost:3000
Origin:http://localhost:3000
Referer:http://localhost:3000/events
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36
X-CSRF-Token:GT/gFRykJMorJu4yPLmLwwGz1EZIwlVSp8XWeO1SOvIVYITeJLAPa1YcYkJOswrkXqMUUFb/jk/DP8JtvLhQYQ==
X-Requested-With:XMLHttpRequest
Form Data
view source
view URL encoded
event_id:0
event:{"2015-05-06":{"available":"1","bind":0,"info":"","notes":"","price":"20","promo":"","status":"available"},"2015-05-07":{"available":"1","bind":0,"info":"","notes":"","price":"20","promo":"","status":"available"},"2015-05-13":{"available":"1","bind":0,"info":"","notes":"","price":"35","promo":"","status":"available"},"2015-05-14":{"available":"1","bind":0,"info":"","notes":"","price":"35","promo":"","status":"available"},"2015-05-15":{"available":"1","bind":0,"info":"","notes":"","price":"35","promo":"","status":"available"},"2015-05-16":{"available":"1","bind":0,"info":"","notes":"","price":"35","promo":"","status":"available"},"2015-05-17":{"available":"1","bind":0,"info":"","notes":"","price":"35","promo":"","status":"available"},"2015-05-20":{"available":"","bind":0,"info":"","notes":"","price":"","promo":"","status":"booked"},"2015-05-21":{"available":"","bind":0,"info":"","notes":"","price":"","promo":"","status":"booked"},"2015-05-22":{"available":"","bind":0,"info":"","notes":"","price":"","promo":"","status":"booked"},"2015-05-23":{"available":"","bind":0,"info":"","notes":"","price":"","promo":"","status":"booked"},"2015-05-24":{"available":"","bind":0,"info":"","notes":"","price":"","promo":"","status":"booked"},"2015-05-28":{"available":"1","bind":0,"info":"","notes":"","price":"25","promo":"","status":"available"},"2015-05-29":{"available":"1","bind":0,"info":"","notes":"","price":"25","promo":"","status":"available"},"2015-05-30":{"available":"1","bind":0,"info":"","notes":"","price":"25","promo":"","status":"available"},"2015-05-31":{"available":"1","bind":0,"info":"","notes":"","price":"25","promo":"","status":"available"}}
and Here is my database schema;
ActiveRecord::Schema.define(version: 20150502145455) do
create_table "events", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "start_time"
t.datetime "end_time"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "status"
t.float "price"
t.float "promo"
t.integer "available"
t.text "info"
t.text "notes"
t.boolean "bind"
t.string "time"
t.string "p"
t.string "event_id"
end
end
EDIT 1:
Here is the Jquery request;
<script>
$(document).ready(function() {
$('#backend').DOPBackendBookingCalendarPRO({
'DataURL': '/events/load', #TO LOAD DATA
'SaveURL': '/events/save' #TO SAVE DATA
});
});
</script>
I have shared load.js.erb file as it is harcoded right now. But save.js.erb file is empty. When I created the url, rails asked me to define template. But #create action does not have a template,right?.
This calendar was written for php but I am trying to use it on RoR. The guy who wrote it, I do not know why, they defined 2 post urls one to retrieve data one to send data. Normally, one of them should be get, right?. That is why I created 2 post urls in routes.rb.Anyway. If you can help, I can send the calendar project I do not know how, but you see their backend.js file. Maybe I should change something there. Thank you
Here at the top of the backend.js file, I have ;
Schedule = {},
And there is this function in backend..js file to load data
parseCalendarData:function(){
$.post(DataURL, {event_id:ID}, function(data){
console.log(typeof data); //THIS RETURNS OBJECT
if(typeof data === 'object') {
Schedule = data;
}
else {
data = $.trim(data).replace(/\\/gi, '');
if (data != ''){
Schedule = JSON.parse(data);
}
}
methods.initCalendar();
});
},
Before save data, here is the code to set submitted data, probably the problem is there;
setData:function(){// Set submited data.
var y, m, d, noDays, key,
startDate, sYear, sMonth, sDay,
endDate, eYear, eMonth, eDay,
fromMonth, toMonth, fromDay, toDay,
availableValue = $('#DOPBCP_available').val(),
bindValue = 0,
infoValue = $('#DOPBCP_info').val().replace(/\n/gi, '<br />'),
notesValue = $('#DOPBCP_notes').val().replace(/\n/gi, '<br />'),
priceValue = $('#DOPBCP_price').val() != undefined ? $('#DOPBCP_price').val():'',
promoValue = $('#DOPBCP_promo').val() != undefined ? $('#DOPBCP_promo').val():'',
statusValue = $('#DOPBCP_status').val();
startDate = dayStartSelection < dayEndSelection ? dayStartSelection.split('_')[1]:dayEndSelection.split('_')[1];
endDate = dayStartSelection < dayEndSelection ? dayEndSelection.split('_')[1]:dayStartSelection.split('_')[1];
sYear = parseInt(startDate.split('-')[0], 10);
sMonth = parseInt(startDate.split('-')[1], 10);
sDay = parseInt(startDate.split('-')[2], 10);
eYear = parseInt(endDate.split('-')[0], 10);
eMonth = parseInt(endDate.split('-')[1], 10);
eDay = parseInt(endDate.split('-')[2], 10);
if (Schedule[methods.previousDay(startDate)] != undefined){
if (Schedule[methods.previousDay(startDate)]['bind'] == 1){
Schedule[methods.previousDay(startDate)]['bind'] = 0;
}
else if (Schedule[methods.previousDay(startDate)]['bind'] == 2){
Schedule[methods.previousDay(startDate)]['bind'] = 3;
}
}
if (Schedule[methods.nextDay(endDate)] != undefined){
if (Schedule[methods.nextDay(endDate)]['bind'] == 2){
Schedule[methods.nextDay(endDate)]['bind'] = 1;
}
else if (Schedule[methods.nextDay(endDate)]['bind'] == 3){
Schedule[methods.nextDay(endDate)]['bind'] = 0;
}
}
for (y=sYear; y<=eYear; y++){
fromMonth = 1;
if (y == sYear){
fromMonth = sMonth;
}
toMonth = 12;
if (y == eYear){
toMonth = eMonth;
}
for (m=fromMonth; m<=toMonth; m++){
noDays = new Date(y, m, 0).getDate();
fromDay = 1;
if (y == sYear && m == sMonth){
fromDay = sDay;
}
toDay = noDays;
if (y == eYear && m == eMonth){
toDay = eDay;
}
for (d=fromDay; d<=toDay; d++){
key = y+'-'+prototypes.timeLongItem(m)+'-'+prototypes.timeLongItem(d);
if ($('#DOPBCP_group').is(':checked')){
if (key == startDate){
bindValue = 1;
}
else if (key == endDate){
bindValue = 3;
}
else{
bindValue = 2;
}
}
//HERE IS THE PROBLEM I GUESS
Schedule[key] = {"available": availableValue,
"bind": bindValue,
"info": infoValue,
"notes": notesValue,
"price": priceValue,
"promo": promoValue,
"status": statusValue};
}
}
}
methods.saveData();
},
Lastly the save function in js file;
saveData:function(){// Save data.
var today = new Date(),
dd = prototypes.timeLongItem(today.getDate()),
mm = prototypes.timeLongItem(today.getMonth()+1),
yyyy = today.getFullYear();
for (var day in Schedule){
if (day < yyyy+'-'+mm+'-'+dd){
delete Schedule[day];
}
}
methods.hideForm();
methods.generateCalendar(StartYear, dayStartSelectionCurrMonth);
$.post(SaveURL, {event_id:ID, event:JSON.stringify(Schedule)}, function(data){});
},
PS: I have tried this in the events controller;
def save
respond_to do |format|
#event = Event.new(event_params)
#event.save
format.html
format.json { render :json => #event, :content_type => 'application/json' }
end
end
But content type did not change.
EDIT 2:
Sorry for giving you so much trouble but I am really trying to learn. I have changed $.post to $.ajaxand I changed the controller as you suggested. Now it try to post array and I am planing to iterate over the hash to get datas and save it. As you said I can then loop over params[:event] as it contains all the data, right?.Because of the multiple data it gives an nil error, that is why I would like to loop over them(I changed the #create as you said);
NoMethodError in EventsController#create
undefined method `new' for nil:NilClass
I think this is because Schedule; (key is the selected date)
Schedule[key] = {"date": key,
"available": availableValue,
"bind": bindValue,
"info": infoValue,
"notes": notesValue,
"price": priceValue,
"promo": promoValue,
"status": statusValue};
}
This is what comes from the Network tab;
Connection:Keep-Alive
Content-Length:94953
Content-Type:text/plain; charset=utf-8
Date:Sun, 03 May 2015 15:26:09 GMT
Server:WEBrick/1.3.1 (Ruby/2.2.1/2015-02-26)
X-Request-Id:1d59e6cd-726f-4eb0-afdf-92205c8a0974
X-Runtime:0.390420
Request Headers
view source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:1687
Content-Type:application/x-www-form-urlencoded; charset=U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tM25Rb1hLMkhUWm5XTElZS1JUejAxSmFvcnE3VzVXTVFoYmdPbTczOE02Wjk4N2hsaTJDRFdLY2pRc1NxYVRNVFBHdkFHbmEvb1U4aEN4T2FOVDZWZGlUMnpJMU0yNkdHNW1acVpYN3NzRkxwQkN2TzRzS0ZBNlMwMGtIMGJ5bWN2QVp5Q3cxQUNIVnExajFDSldsZWhJek1zUjJxVzJyRHRsUDBjQnJsdlB1RFE9PS0tSjViSGdocSs5blRLdTNnUk1GWk9qZz09--068ded1e78c2d70739361e662b995b5470a059c4; _raddit_session=Vy9USHZBa1FodGxON1BMOWFZOHBIazk3eWVSOXh0OW9yUjRmSjJ5Tmg1OXpsMnI2Tm5ZTGQwOHdEMVJnUWhFNG5jRVZEcVhLL3ZCSVdsM1g5S2FTMjYvZWo4U1ovT0xNdkZVT2dJS0VWTkRMcXdRQ0lzTlFRWC9EZ1ZxbDRtYmtLQ1dkZzM3THltSEFvdzJTN010MHVnYytJMzE1R2F0cUVkQlh2bERBRUM4c0NVNnFsTFdmbVB1cURJeDVHUkhKOWZUOG5pUlhFZkxOSkJ3c01uU2paTW50WXRrRW8zRmJtTTBrUm5GWlJvbz0tLUh4ckZqdnA3WGQzRjdmNnAzMXdMcmc9PQ%3D%3D--622d3f9adb9afb871e118d0715921aaf986f82d2; _Cal_session=cjRlSlc0Q2FITHJJTmlTbU5TZjk5aFNFaTczTHRQZW9TR0h1MG4xYVovZG9xaUxyNittWENjbXdib3FLc1J6cXh3dHA5YitaVkZYbFVtaFI2bGxUKzJyM1kvRzJlRFV4b0JibzJ1TTdQS0ZkdVE2eVJJSlZQVXY0Q0thS1NBRzhGUzlaQ1ZRaHppYzJES2V2eXRmVWtBPT0tLWEzVWFIcWl0WnJBRVdnanJ0em1Renc9PQ%3D%3D--03a65bd6c5338108d2d3db62295843f8b3e3eaaa; _fullcalendar_and_rails_example_session=M1h0eFJieVY1ek16OWRtR1JRUGc1eCtsdFZnTDdqWkd3NndWQTFuUzFKdHJpOHlRYSsweHZ6UGZubTV1eEdJZm14NVZxbE1IUHZzRG1id2NVeENERzFlaFF3NzFNWHk0RlltbS8yNmc2aEtnYjJDcGZmY3pZWG0vU0RsU29ST005NDVpdVpYSWtNTURkM3M2RmVvZU8vUStwdUxFNk1TaVJTRDY1Z2FKcEd4QkxZMlpGeUlsUGl5UlNCVWhPNk5qR082T25lZlZ4NkIxY012MjRJaVVTKzFqdUZFMUVjc1VxVnA0UXFwSVFEbW1acE9KMWw1N0xzQWJlYUd6R200Q1VCdkJjUWpTbElrbFhlZ2ZBdnY2WnB1NHdHU2tBQTlQandzekR5SmJkbnVhU08vNWpLNjFuY1UyVUxuZWt2ZWotLTY3TU9jZklCcVR6ZXZQRy9UUCtHTmc9PQ%3D%3D--9bcb70f5196a063e7915b11622953f7935d9166c; _takvim_session=WVJmQVpEbEJUOE9KeVdLK3ZyODRpejI0dzA2aWJPd2dqOGRCaysxOU5VT3lhVkNnNzNIZytvYlpqL2FRZGhTWXZFcWxLc3JYVmoyUVRibGFuelpCczl4eFVjaFVLdkZiRURpUC84cysyUldFbC9VT295SURnMkpDQnFtdldLdUhHeDhRL3NGOG90WS9mWUI1WitocndBPT0tLVp6Mk1VS2VaT0hmMGtwcnhNcnpYMkE9PQ%3D%3D--a215e9c8f635c5c638ccdcd55105c3bdc452760e; SQLiteManager_currentLangue=2; request_method=GET; _Last_session=VnErWEFwazE3dWFBdm94WUVhMXdYWFhKVnViUmtQWUFja0xENjNDTmYxWVAxbjVyVDZrK3pxRlVjUnp0MGQ3R240UWZSK2lNcld5bDVKM2ZYb3JwMTJ1QXpPa0p1cFJOYXFXUllIakVrdnBzVHh2V09YQlZGTHNjTWtNLzFDOXNTdUhYZGNXei80enRwclZkdzBCUzZFcUtEQVFqeDFock15anMrVnVIUHRRPS0tci9lSEMvNFBtVWJ5eW1BSTQ0VUpadz09--46ddca06b8e604506dcfff84e7cc8111c7a1b679; _demo_session=R1dIMnpjMU1BTkszczFzZTR2Zmk3cFMyUVU2Yk12eEwyZ2poR2RWUzhQWEtPbEM5UkMrQXRJb21oc1grWkFNNlpVM2VwMElvMnI1YUN2UlFVeXR2KytqYzJyN3AxeS9lM3gxVlNhLysxQzNLMklQOGszdUZBaEREMGQwZVMzc09BNFVndjRZaGQzY25KZGwrR0wyME5nPT0tLUEreS9acGZTWUhzdXdOc0hrVW5WYVE9PQ%3D%3D--9a636d832705858099e968c44c959b12980b6e99
Host:localhost:3000
Origin:http://localhost:3000
Referer:http://localhost:3000/events
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36
X-CSRF-Token:BaHKI75O/nhl2kbiQDxSvJeZHiUAlTttTPGd0b/usf4J/q7ohlrV2RjgypIyNtObyIneMx6o4HAoC4nE7gTbbQ==
X-Requested-With:XMLHttpRequest
Form Data
view source
view URL encoded
event[2015-05-06][available]:1
event[2015-05-06][bind]:0
event[2015-05-06][info]:
event[2015-05-06][notes]:
event[2015-05-06][price]:20
event[2015-05-06][promo]:
event[2015-05-06][status]:available
event[2015-05-07][available]:1
event[2015-05-07][bind]:0
event[2015-05-07][info]:
event[2015-05-07][notes]:
event[2015-05-07][price]:20
event[2015-05-07][promo]:
event[2015-05-07][status]:available
event[2015-05-20][date]:2015-05-20
event[2015-05-20][available]:1
event[2015-05-20][bind]:0
event[2015-05-20][info]:
event[2015-05-20][notes]:
event[2015-05-20][price]:
event[2015-05-20][promo]:
event[2015-05-20][status]:available
event[2015-05-21][date]:2015-05-21
event[2015-05-21][available]:1
event[2015-05-21][bind]:0
event[2015-05-21][info]:
event[2015-05-21][notes]:
event[2015-05-21][price]:
event[2015-05-21][promo]:
event[2015-05-21][status]:available
event[2015-05-22][date]:2015-05-22
event[2015-05-22][available]:1
event[2015-05-22][bind]:0
event[2015-05-22][info]:
event[2015-05-22][notes]:
event[2015-05-22][price]:
event[2015-05-22][promo]:
event[2015-05-22][status]:available
event[2015-05-23][date]:2015-05-23
event[2015-05-23][available]:1
event[2015-05-23][bind]:0
event[2015-05-23][info]:
event[2015-05-23][notes]:
event[2015-05-23][price]:
event[2015-05-23][promo]:
event[2015-05-23][status]:available
EDIT 3:
h = {"event"=>{"2015-05-06"=>{"available"=>"1", "bind"=>"0", "info"=>"", "notes"=>"", "price"=>"20", "promo"=>"", "status"=>"available"}, "2015-05-07"=>{"available"=>"1", "bind"=>"0", "info"=>"", "notes"=>"", "price"=>"20", "promo"=>"", "status"=>"available"}, "2015-05-18"=>{"date"=>"2015-05-18", "available"=>"1", "bind"=>"0", "info"=>"", "notes"=>"", "price"=>"30", "promo"=>"", "status"=>"available"}, "2015-05-19"=>{"date"=>"2015-05-19", "available"=>"1", "bind"=>"0", "info"=>"", "notes"=>"", "price"=>"30", "promo"=>"", "status"=>"available"}, "2015-05-20"=>{"date"=>"2015-05-20", "available"=>"1", "bind"=>"0", "info"=>"", "notes"=>"", "price"=>"30", "promo"=>"", "status"=>"available"}, "2015-05-21"=>{"date"=>"2015-05-21", "available"=>"1", "bind"=>"0", "info"=>"", "notes"=>"", "price"=>"30", "promo"=>"", "status"=>"available"}, "2015-05-22"=>{"date"=>"2015-05-22", "available"=>"1", "bind"=>"0", "info"=>"", "notes"=>"", "price"=>"30", "promo"=>"", "status"=>"available"}, "2015-05-23"=>{"date"=>"2015-05-23", "available"=>"1", "bind"=>"0", "info"=>"", "notes"=>"", "price"=>"30", "promo"=>"", "status"=>"available"}}}
h.each do |key, value|
puts key
value.each do |key2, value2|
puts key2
value2.each do |k, v|
puts k
puts v
end
end
end
Something like the code works fine to get the key and values, however I do not know if that works if I put it to controller.Then I should save it to database. I surely think there must be a easier way. But I am dependent on the calendar js code that the person wrote. Sorry again asking too much. If you can guide me I appreciate. Thank you again
In my controller #create action I am able to iterate over key and values;
Now I have to figure out how to save them
def create
h = params[:event]
h.each do |key, value|
puts key
value.each do |key2, value2|
puts key2
puts value2
end
end
end
You haven't detailed how you are actually sending the JSON request but two things are going wrong:
Your save action expects a hash with the key event with:
{
event: {
title: "foo"
# more attributes ...
}
}
But you're trying to send the parameters for several dates at the same time.
event:{
"2015-05-06": {"available":"1","bind":0,"info":"","notes":"","price":"20","promo":"","status":"available"}
#... more events
}
Also you sending the request with the wrong `Content-Type
Your request is sent with:
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Not:
Content-Type: application/json; charset=utf-8;
differences in application/json and application/x-www-form-urlencoded
Added in response to edit of the question:
Use jQuery.ajax instead of jQuery.post and it will stringify the data for you - that way jQuery will take care of browsers which don't have native JSON support.
$.ajax(SaveURL, {
method: 'POST',
data: { event: Schedule },
dataType: 'json'
});
Now that you are sending the correct request type jQuery will merge the post data into params.
But this still doesn't fix the issue that Schedule contains the attributes for multiple Events. I would start by creating a simple route which accepts the parameters for a single event. In Rails conventions we call that action create not save.
class EventsController
# ...
# POST /events
def create
#event.new(params[:event])
if (#event.save)
render json: #event, status: :created
else
# ... what do we do if validation fails?
end
end
# ...
private
def create_params
params.permit(:event).allow(:available, :bind, :info, :notes, :price, :promo, :status)
end
end
You can generate a full set of restful routes for events by adding this to your config/routes.rb:
resources :events
which generates the following routes:
Prefix Verb URI Pattern Controller#Action
events GET /events(.:format) events#index
POST /events(.:format) events#create
new_event GET /events/new(.:format) events#new
edit_event GET /events/:id/edit(.:format) events#edit
event GET /events/:id(.:format) events#show
PATCH /events/:id(.:format) events#update
PUT /events/:id(.:format) events#update
DELETE /events/:id(.:format) events#destroy
At your skill level I would not bother trying to create a controller action which creates several Events at once, and I'm not going to instruct how to do it here since it would be really lengthy.