gpt4 book ai didi

javascript - Rails 4 x simple_calendar gem : create new record on click in calendar cell

转载 作者:行者123 更新时间:2023-11-30 16:31:11 26 4
gpt4 key购买 nike

我有一个带有 Post 模型和 Calendar 模型的 Rails 4 应用程序: post belongs_to a calendar 和一个calendar has_many posts.

Post 模型有一个自定义的 :date 属性。

帖子以两种不同的 View 显示:

  • 作为标准Calendars#Show View 中的列表
  • 作为自定义 Calendars#Calendar_view View 中的日历

我想要实现的是,让用户只需单击 Calendars#Calendar_view View 中的日历单元格即可创建新帖子,也就是说转到Posts#New View ,其中新帖子的 :date 字段将预先填充单元格中的日期。

我正在使用 simple_calendar gemcalendar_view View 中显示帖子,代码如下:

<%= month_calendar events: @posts do |date,posts| %>
<%= content_tag :div, class: "new-event", data: {date: date} do %>
<%= date.strftime('%e') %>
<% posts.each do |post| %>
<div class="single_event">
<%= link_to post.subject, post_path(post) %>
</div>
<% end %>
<% end %>
<% end %>

我在我的 posts.coffee 文件中使用此代码来检测 on click 事件并从单元格中获取日期:

$(".new-event").on "click", (e) ->
date = $(this).data("date")
window.location = "/posts/new?post[date]=#{date}"

然后,在我的 Posts#New 操作中,我使用 @post = @calendar.posts.new(post_params) 从单元格中预加载日期在表格中。

问题是:当我在 calendar_view View 中单击一个单元格时,没有任何反应。

我检查了 Chrome 中的控制台,没有出现错误。

知道这里出了什么问题,或者至少我应该从哪里开始调试它?

最佳答案

查看此示例:https://github.com/excid3/clickable-simple_calendar

您可以使用此 JS 来触发重定向。我错了位置重定向,这就是你想要的:

jQuery ->
$(".calendar-day").on "click", ->
date = $(this).data("date")
location.href = "/meetings/new?meeting[start_time]=#{date}"

这一切都与日历的 HTML Hook :

<%= month_calendar do |day| %>
<%= content_tag :div, class: "calendar-day", data: {date: day} do %>
<%= day %>
<%# Whatever else you want in here %>
<% end %>
<% end %>

Meetings 新操作有一点变化,它使用 strong_params 使用参数预填充它(如果它丢失也不会抛出错误)。

这应该可以解决问题!

关于javascript - Rails 4 x simple_calendar gem : create new record on click in calendar cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33302539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com