【问题标题】:Ruby on rails form doesn't write into databaseRuby on rails 表单不写入数据库
【发布时间】:2018-02-18 15:43:46
【问题描述】:

我很痛苦,因为我不明白为什么我的表单没有将数据写入我的数据库。如果我提交表单,我会在数据库中得到一个新行,其中只包含“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

现在是日志文件的 sn-p

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)

【问题讨论】:

  • 可能,您的表单应该包含要更新的 ID?您能否查看您的浏览器网络选项卡以查看保存时触发了哪个路由?

标签: ruby-on-rails database forms


【解决方案1】:

您没有正确使用 strong_params。应该是这样的:

def institute_params
  params.require(:institute).permit(:name, :professors, ...)
  #     ^^^^^^^^^^^^^^^^^^^^
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 2011-10-02
    相关资源
    最近更新 更多