我在oorder中使用_form部分来创建/编辑帖子。这个表单有一个下拉菜单,用于设置发布状态,所以如果用户将状态设置为Scheduled
,那么datetimepicker显示为了选择一个日期,否则这应该默认隐藏(即当状态是“草稿”或“发布时间“)。Turbolinks - 仅在预定帖子中显示datetimepicker - 默认情况下隐藏
# app/views/posts/_form.html.haml
= simple_form_for ["admin", @post] do |f|
= f.input :title
= f.input :status, collection: ["Draft", "Published", "Scheduled"], selected: status_for(@post)
.published-field
= f.input :published_at, as: :date_time_picker
...
# app/helpers/posts_helper.rb
module PostsHelper
def status_for(post)
if post.published_at?
if post.published_at > Time.zone.now
"Scheduled"
else
"Published"
end
else
"Draft"
end
end
end
# app/assets/javascripts/posts.coffee
changeVisibility = (status) ->
if status == "Scheduled"
$(".published-field").show()
else
$(".published-field").hide()
jQuery ->
changeVisibility $("#post_status :selected").text()
$("#post_status").on "change", (e) ->
changeVisibility $(this).find(":selected").text()
问题是当用户访问该页面第一次,所以status
默认情况下(如预期)设置为Draft
,但是的DateTimePicker可见这是不应该(remeber?这应该仅在用户选择安排帖子时才可见)。如果您在查看文件时刷新页面,则日期选择器将消失。
原来的问题也可以在这里https://gorails.com/episodes/scheduling-posts
发现这个问题可能造成的,因为turbolinks的。我也尝试使用jquery-turbolinks
宝石,但没有设法解决问题。任何想法如何解决这个问题?
您的代码不包含turbo链接 –
大多数代码不会,因为Turbolinks会更改页面之间正常链接的行为方式。刷新然后加载“正常”。 –