View和Controller应该如何创建具有DateTime类型的对象实例

时间:2018-12-23 03:14:48

标签: ruby-on-rails-5

我陷入了困境,现在我不知道如何编写正确的Controller和View来创建和更新包含DateTime类型属性的对象实例。

这是我当前的代码:

控制器:     类LessonsController

def show
  @lesson = Lesson.find(params[:id])
end

def new
  @lesson = Lesson.new
end

def edit
  @lesson = Lesson.find(params[:id])
end

def create
 @lesson = Lesson.new(lesson_params)

  if @lesson.save
    redirect_to admin_lesson_path(@lesson), notice: 'Lesson created 
    successfully.'
  else
    render :new
  end
end

def update
  @lesson = Lesson.find(params[:id])
  if @lesson.update_attributes(lesson_params)
    redirect_to admin_lesson_path(@lesson), notice: 'Lesson updated 
    successfully.'
  else
    render :edit
  end
end

def destroy
  @lesson = Lesson.find(params[:id])
  @lesson.destroy

  redirect_to admin_lessons_path, notice: 'Lesson successfully deleted.'
end

private
   def lesson_params
   params.require(:lesson).permit(:start_at, :end_at, :durration, 
   :course_id)
  end
end

查看(_form):

<%= simple_form_for [:admin, @lesson] do |f| %>
  <%= f.error_notification %>

<div class="form-inputs">
  <%= f.datetime_select(:start_at) %>
  <p><%= f.collection_select(:course_id, Course.all, :id, :title) %></p>
</div>

<div class="form-actions">
  <%= f.button :submit %>
</div>

< %end %>

提交表单后得到的参数:

Parameters: {"utf8"=>"✓","authenticity_token"=>"tJxf67shkneMA170uvdJyGF1T7fzqctrmIp6F6IoPMmVf2V5LpxVOlnHChPsY3qNCmkG24lbt9spkWPStZQcIg==", "lesson"=>{"start_at(1i)"=>"2018","start_at(2i)"=>"12", "start_at(3i)"=>"23", "start_at(4i)"=>"02", "start_at(5i)"=>"30", "course_id"=>"6"}, "commit"=>"Create Lesson"}

0 个答案:

没有答案