【问题标题】:creating recurring events with ice_cube gem使用 ice_cube gem 创建重复事件
【发布时间】:2015-10-24 20:12:36
【问题描述】:

我正在尝试使用 ice_cube 和 recurring_select gem 创建重复事件。

这是我的 _form.html.erb 代码:

<%= simple_form_for(@event) do |f| %>
  <div class="form-inputs">
    <%= f.select_recurring :day, [IceCube::Rule.daily]  %>
    <%= f.input :start_time %>
    <%= f.input :end_time %>
  </div>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

在我的控制器中,我有(除其他外):

def new
   @event = Event.new
end

def create
  @event = Event.new(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.require(:event).permit(:day, :start_time, :end_time, :reserved)
end

如您所见,我想为一周中的每一天创建相同的事件,但实际上,如果我提交此表单,我的 :day 列仍然为空。

您能提供一些反馈吗?不知道怎么回事

【问题讨论】:

    标签: ruby-on-rails ruby recurring-events ice-cube


    【解决方案1】:

    您的escape_params 似乎有误,应该是event_params,正如您在update 操作中使用的那样:

    private
      def event_params
        params.require(:event).permit(:day, :start_time, :end_time, :reserved)
      end
    

    更新:

    查看recurring_select gem 后,它发送到服务器的数据是这样的:

    event[:day]: {"interval":1,"until":null,"count":null,"validations":null,"rule_type":"IceCube::DailyRule"}
    

    所以它不是一个可以存储在单个字段中的简单单值参数。

    这里有两个选择,要么序列化该值并将其存储在单个字段中,要么为数据库中的每个参数创建单独的字段。

    由于您在day 字段中的数据是一个哈希,permit 函数根本无法处理它。您可以在Rails issue tracker 上查看更多信息。

    【讨论】:

    • 抱歉,复制代码时出现类型错误。我现在更正了。不幸的是,这不是问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 2020-11-17
    • 2018-05-11
    • 2012-08-14
    相关资源
    最近更新 更多