我很痛苦,因为我不知道为什么我的表单不会将数据写入我的数据库。如果我提交表单,我会在数据库中获得一个新行,其中只包含“id”,“created at”和“updated at”。未提交所有其他参数。在日志文件中,我收到“未经许可的参数”消息。我在哪里可以改变这个?
我很乐意帮忙。非常感谢!
以下是“新学院”页面的视图,其中包含表格。
<% provide(:title, 'Institut erstellen') %>
<div class="small_jumbotron jumbotron">
<h1>Institut erstellen</h1>
<div class="row">
<div class="Links">
<%= form_for(@institute) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<br>
<%= f.label :Institutsname %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :Professorenanzahl %>
<%= f.number_field :professors, class: 'form-control' %>
<%= f.label "Anzahl wissenschaftlicher Mitarbeiter" %>
<%= f.number_field :employees, class: 'form-control' %>
<%= f.label "Anzahl an Masterarbeiten" %>
<%= f.number_field :master_theses, class: 'form-control' %>
<%= f.label "Anzahl Lehrveranstaltungen" %>
<%= f.number_field :classes, class: 'form-control' %>
<%= f.label "Minimale Bachelorarbeitenzuordnung" %>
<%= f.number_field :min_workload, class: 'form-control' %>
<%= f.label "Überkapazitätsbereitschaft" %>
<%= f.number_field :overload, class: 'form-control' %>
<%= f.label "Aversion gegen Überkapazitäten" %>
<%= f.number_field :overload_aversion, class: 'form-control' %>
<%= f.label "Kapazität" %>
<%= f.number_field :capacity, class: 'form-control' %>
<br>
<%= f.submit "Erstelle das Institut", class: "btn btn-primary" %>
<% end %>
</div>
</div>
</div>
这是研究所的控制者
class InstitutesController < ApplicationController
before_action :set_institute, only: [:show, :edit, :update, :destroy]
# GET /institutes
# GET /institutes.json
def index
@institutes = Institute.all
end
# GET /institutes/1
# GET /institutes/1.json
def show
end
# GET /institutes/new
def new
@institute = Institute.new
end
# GET /institutes/1/edit
def edit
end
# POST /institutes
# POST /institutes.json
def create
@institute = Institute.new(institute_params)
respond_to do |format|
if @institute.save
format.html { redirect_to @institute, notice: 'Das Institut wurde erstellt.'}
format.json { render :show, status: :created, location: @institute }
else
format.html { render :new }
format.json { render json: @institute.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /institutes/1
# PATCH/PUT /institutes/1.json
def update
respond_to do |format|
if @institute.update(institute_params)
format.html { redirect_to @institute, notice: 'Die Institutsdaten wurden aktualisiert.' }
format.json { render :show, status: :ok, location: @institute }
else
format.html { render :edit }
format.json { render json: @institute.errors, status: :unprocessable_entity }
end
end
end
# DELETE /institutes/1
# DELETE /institutes/1.json
def destroy
@institute.destroy
respond_to do |format|
format.html { redirect_to institutes_url, notice: 'Das Institut wurde gelöscht.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_institute
@institute = Institute.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def institute_params
params.permit(:name, :professors, :employees, :master_theses, :classes, :min_workload, :overload, :overload_aversion, :capacity)
end
end
现在是日志文件的片段
Started POST "/institutes" for 127.0.0.1 at 2018-02-18 16:15:02 +0100
Processing by InstitutesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ufyuHL2iXZpQg1ZmfG1pleuY7aL3JIl+eGn36UJeibML8U5R1sOgw8jd6geBB60XchttET2T3I1SndFgGsR2uA==", "institute"=>{"name"=>"wsd", "professors"=>"", "employees"=>"4", "master_theses"=>"", "classes"=>"", "min_workload"=>"", "overload"=>"", "overload_aversion"=>"", "capacity"=>""}, "commit"=>"Erstelle das Institut"}
Unpermitted parameters: :utf8, :authenticity_token, :institute, :commit
[1m[35m (0.5ms)[0m [1m[36mbegin transaction[0m
[1m[35mSQL (0.6ms)[0m [1m[32mINSERT INTO "institutes" ("created_at", "updated_at") VALUES (?, ?)[0m [["created_at", "2018-02-18 15:15:02.619018"], ["updated_at", "2018-02-18 15:15:02.619018"]]
[1m[35m (17.0ms)[0m [1m[36mcommit transaction[0m
Redirected to http://localhost:3000/institutes/12
Completed 302 Found in 30ms (ActiveRecord: 18.1ms)
答案 0 :(得分:1)
您没有正确使用strong_params。应该是这样的:
def institute_params
params.require(:institute).permit(:name, :professors, ...)
# ^^^^^^^^^^^^^^^^^^^^
end