如何通过 JQuery 在 Rails 中使用 FullCalendar post 附加参数?
How to post additional parameters via JQuery with FullCalendar in Rails?
我正在为我的 Rails 项目使用 Adam Shaw 的 FullCalendar 库。
目前,我拥有它,因此当您单击一个日期时,它会提示您使用 select
回调输入事件的标题(以及稍后可能的其他字段)。
为了存储事件,我使用了 SQLite 数据库模型 event
。
我想要 POST
其他字段(例如 type:string
我的模型,它们不是 FullCalendar 中 EventObject
的默认参数。
出于某种原因,这有效:
// JS file associated with the calendar div
$('#calendar').fullCalendar({
...
select: function(start, end, allDay){
var title = prompt('Event title:');
if (title){
var eventData = {
title: title,
description: '',
start: start.format(),
end: end.format(),
url: ''
};
$.ajax({
url: '/events',
type: 'POST',
data: {
title: title,
description: '',
start: start.format(),
end: end.format(),
url: '',
type: '' // <------ I can POST this when it's empty/nil
},
success: function(resp){ // <------ this is called
alert('success!');
$('#calendar').fullCalendar('renderEvent', eventData, true);
},
error: function(resp){
alert('failure!');
}
});
}
$('#calendar').fullCalendar('unselect');
},
});
但当 type:
是 non-empty 字符串时则不然:
$.ajax({
url: '/events',
type: 'POST',
data: {
title: title,
description: '',
start: start.format(),
end: end.format(),
url: '',
type: 'custom' // <------ This gives me an alert with 'failure!'
},
success: function(resp){
alert('success!');
$('#calendar').fullCalendar('renderEvent', eventData, true);
},
error: function(resp){
alert('failure!'); // <--- this is called
}
});
我将如何发布这些 non-default 字段?
这是来自 /events_controller.rb
的 POST
和 event_params
代码(注意:我使用 Devise
作为登录系统,因此 current_tutor
只是检索currently-logged 在 user/tutor 中;tutor has_many :events
和 event belongs_to :tutor
):
class EventsController < ApplicationController
...
def create
@tutor = current_tutor
@event = @tutor.events.build(event_params)
respond_to do |format|
if @event.save
format.html { redirect_to @event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: @event }
else
format.html { render :new }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
...
def event_params
params[:start_time] = params[:start]
params[:end_time] = params[:end]
params.permit(:title, :description, :start_time, :end_time, :url, :type)
end
end
作为我缺乏经验和天真的证明,我开始意识到您的数据不应该包含 ajax 请求使用的保留键。
将 type
字段更改为 post_type
,效果非常好。卫生署!
我正在为我的 Rails 项目使用 Adam Shaw 的 FullCalendar 库。
目前,我拥有它,因此当您单击一个日期时,它会提示您使用 select
回调输入事件的标题(以及稍后可能的其他字段)。
为了存储事件,我使用了 SQLite 数据库模型 event
。
我想要 POST
其他字段(例如 type:string
我的模型,它们不是 FullCalendar 中 EventObject
的默认参数。
出于某种原因,这有效:
// JS file associated with the calendar div
$('#calendar').fullCalendar({
...
select: function(start, end, allDay){
var title = prompt('Event title:');
if (title){
var eventData = {
title: title,
description: '',
start: start.format(),
end: end.format(),
url: ''
};
$.ajax({
url: '/events',
type: 'POST',
data: {
title: title,
description: '',
start: start.format(),
end: end.format(),
url: '',
type: '' // <------ I can POST this when it's empty/nil
},
success: function(resp){ // <------ this is called
alert('success!');
$('#calendar').fullCalendar('renderEvent', eventData, true);
},
error: function(resp){
alert('failure!');
}
});
}
$('#calendar').fullCalendar('unselect');
},
});
但当 type:
是 non-empty 字符串时则不然:
$.ajax({
url: '/events',
type: 'POST',
data: {
title: title,
description: '',
start: start.format(),
end: end.format(),
url: '',
type: 'custom' // <------ This gives me an alert with 'failure!'
},
success: function(resp){
alert('success!');
$('#calendar').fullCalendar('renderEvent', eventData, true);
},
error: function(resp){
alert('failure!'); // <--- this is called
}
});
我将如何发布这些 non-default 字段?
这是来自 /events_controller.rb
的 POST
和 event_params
代码(注意:我使用 Devise
作为登录系统,因此 current_tutor
只是检索currently-logged 在 user/tutor 中;tutor has_many :events
和 event belongs_to :tutor
):
class EventsController < ApplicationController
...
def create
@tutor = current_tutor
@event = @tutor.events.build(event_params)
respond_to do |format|
if @event.save
format.html { redirect_to @event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: @event }
else
format.html { render :new }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
...
def event_params
params[:start_time] = params[:start]
params[:end_time] = params[:end]
params.permit(:title, :description, :start_time, :end_time, :url, :type)
end
end
作为我缺乏经验和天真的证明,我开始意识到您的数据不应该包含 ajax 请求使用的保留键。
将 type
字段更改为 post_type
,效果非常好。卫生署!