Как должны выглядеть View и Controller для создания экземпляра объекта с типом DateTime - PullRequest
0 голосов
/ 23 декабря 2018

Я застрял и сейчас не знаю, как написать правильный 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"}

I

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...