我有一个可以创建新事件的web应用程序,ruby将它们绑定到一天。 所以我有一个belongs_to has_many关系
要创建新事件,我制作了自己的模板:
newform.html.erb
<% provide(:title, "Event Optionen") %>
<%= form_for(@event) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.label :title, "Titel" %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description, "Beschreibung" %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :day_id, "Tag"%><br>
<%= f.collection_select :day, @dates, :to_s, :to_s, include_blank: false %>
</div>
<div class="field_small">
<%= f.label :start %><br>
<%= f.time_select :start, :minute_step => 15 %>
</div>
<div class="field_small">
<%= f.label :end, "Ende" %><br>
<%= f.time_select :end, :minute_step => 15 %>
</div>
<div class="actions">
<%= f.hidden_field :day_id, :value => :day%>
<%= f.submit "Event erstellen", class: "btn btn-large btn-primary" %>
</div>
<% end %>
events_controller.rb
class EventsController < ApplicationController
include SessionsHelper
before_action :set_event, only: [:show, :edit, :update, :destroy]
before_filter :admin_authenticate
def index
@events = Event.all
end
def show
end
def new
@event = Event.new
lager = Lager.find(params[:id])
@dates = []
for i in lager.start..lager.end
@dates << i
end
end
def edit
@day = Day.find(params[:id]).date
end
def create
event_params = params[:event]
date = Date.new event_params["start(1i)"].to_i, event_params["start(2i)"].to_i, event_params["start(3i)"].to_i
start_time = Time.new event_params["start(1i)"].to_i, event_params["start(2i)"].to_i, event_params["start(3i)"].to_i, event_params["start(4i)"].to_i, event_params["start(5i)"].to_i
end_time = Time.new event_params["end(1i)"].to_i, event_params["end(2i)"].to_i, event_params["end(3i)"].to_i, event_params["end(4i)"].to_i, event_params["end(5i)"].to_i
title = event_params[:title]
description = event_params[:description]
day = event_params[:day]
day_id = Day.find_by(date: "#{day}")
@event = Event.new(start: start_time, end: end_time, title: title, description: description, day_id: day_id)
respond_to do |format|
if @event.save
format.html { redirect_to root_path, notice: 'Event wurde erfolgreich erstellt.' }
format.json { head :no_content }
else
format.html { render action: 'new' }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @event.update(event_params)
format.html { redirect_to @event, notice: 'Event was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
def destroy
@event.destroy
respond_to do |format|
format.html { redirect_to events_url }
format.json { head :no_content }
end
end
private
def set_event
@event = Event.find(params[:id])
end
end
所以在创建此事件后,它应该重定向到root_path,否则渲染回 new.html.erb 。
每次出现此错误:/我不知道如何处理它。
答案 0 :(得分:1)
我认为您收到错误是因为您未在方法@dates
中设置create
。我认为您使用无效参数发布了表单,@event
无法保存,而您使用new
呈现@dates
。
答案 1 :(得分:0)
问题是你正在为nil对象尝试map方法,所以试着找到对象。我想新的事件@dates
值将是nil,所以注释行
<%= f.collection_select :day, @dates, :to_s, :to_s, include_blank: false %>
并重新加载页面。