Таким образом, форма не сохраняется в БД. Не уверен, почему, но он обрабатывается как: CheckpointsController#new as HTML
Вот форма:
<div class="card">
<div class="card-header" style='background-color: #3b3a39; color: whitesmoke;'>
Checkpoint
</div>
<div class="card-body">
<form>
<%= form_with(model: checkpoint, local: true, remote: true) do |form| %>
<% if checkpoint.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(checkpoint.errors.count, "error") %> prohibited this checkpoint from being saved:</h2>
<ul>
<% checkpoint.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= form.label :section_id %>
<%= form.collection_select :section_id, Section.order(:name), :id, :name, include_blank: true, input_html: { class: "dropdown" } %>
</div>
<div class="form-group">
<%= form.label :title %>
<%= form.text_field :title, class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :description %>
<%= form.text_area :description, class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :time_to_complete, "Time to complete as percentage of section" %>
<%= form.number_field :time_to_complete, class: "form-control" %>
</div>
<div>
<%= form.label :content %>
<%= form.rich_text_area :content %>
</div>
<br />
<div class="form-group">
<%= form.button :submit, class: "btn btn-outline-success pull-right" %>
</div>
<% end %>
</form>
</div>
</div>
Вот контроллер:
class CheckpointsController < ApplicationController
before_action :set_checkpoint, only: [:show, :edit, :update, :destroy]
# GET /checkpoints
# GET /checkpoints.json
def index
@checkpoints = Checkpoint.all
end
# GET /checkpoints/1
# GET /checkpoints/1.json
def show
end
# GET /checkpoints/new
def new
@checkpoint = Checkpoint.new
@sections = Section.all
end
# GET /checkpoints/1/edit
def edit
@sections = Section.all
end
# POST /checkpoints
# POST /checkpoints.json
def create
@checkpoint = Checkpoint.new(checkpoint_params)
@section = Section.find(checkpoint_params[:section_id])
@course = @section.course
respond_to do |format|
if @checkpoint.save
format.html { redirect_to @course, notice: 'Checkpoint was successfully created.' }
format.json { render :show, status: :created, location: @checkpoint }
else
format.html { render :new }
format.json { render json: @checkpoint.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /checkpoints/1
# PATCH/PUT /checkpoints/1.json
def update
respond_to do |format|
if @checkpoint.update(checkpoint_params)
format.html { redirect_to @checkpoint, notice: 'Checkpoint was successfully updated.' }
format.json { render :show, status: :ok, location: @checkpoint }
else
format.html { render :edit }
format.json { render json: @checkpoint.errors, status: :unprocessable_entity }
end
end
end
# DELETE /checkpoints/1
# DELETE /checkpoints/1.json
def destroy
@checkpoint.destroy
respond_to do |format|
format.html { redirect_to checkpoints_url, notice: 'Checkpoint was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_checkpoint
@checkpoint = Checkpoint.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def checkpoint_params
params.require(:checkpoint).permit(:title, :section_id, :description, :time_to_complete, :content)
end
end
Вотстек вывода:
Started GET "/checkpoints/new?authenticity_token=GfQx6u6Jx%2FPgIIgt2EMqV0nRxk%2FCVENQqbqZiFU13UmIPqAR0tR3XxRZ5yUvRIhSLDimLMqhcZ7hpJU17qBaTw%3D%3D&checkpoint%5Bsection_id%5D=1&checkpoint%5Btitle%5D=Some+stuff&checkpoint%5Bdescription%5D=gsdffsdfgfdgdf&checkpoint%5Btime_to_complete%5D=10&checkpoint%5Bcontent%5D=%3Cdiv%3Edsfsdfsdfsdfsdfsdfds%3C%2Fdiv%3E&button=" for ::1 at 2019-11-03 15:21:35 -0500
Processing by CheckpointsController#new as HTML
Parameters: {"authenticity_token"=>"GfQx6u6Jx/PgIIgt2EMqV0nRxk/CVENQqbqZiFU13UmIPqAR0tR3XxRZ5yUvRIhSLDimLMqhcZ7hpJU17qBaTw==", "checkpoint"=>{"section_id"=>"1", "title"=>"Some stuff", "description"=>"gsdffsdfgfdgdf", "time_to_complete"=>"10", "content"=>"<div>dsfsdfsdfsdfsdfsdfds</div>"}, "button"=>""}
Rendering checkpoints/new.html.erb within layouts/application
Section Load (0.3ms) SELECT "sections".* FROM "sections" ORDER BY "sections"."name" ASC
↳ app/views/checkpoints/_form.html.erb:22
Rendered checkpoints/_form.html.erb (Duration: 3.9ms | Allocations: 2786)
Rendered checkpoints/new.html.erb within layouts/application (Duration: 4.4ms | Allocations: 3083)
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/views/layouts/application.html.erb:16
Completed 200 OK in 17ms (Views: 15.8ms | ActiveRecord: 0.6ms | Allocations: 19710)
Я обнаружил, когда перетаскиваю изображение в редактор trix, которое оно сохраняет. Но форма не обрабатывается созданием в контроллере. Не уверен, почему это происходит. Любая помощь будет очень полезна.