控制器更新中的ActionController :: ParameterMissing(参数缺失或值为空)

时间:2019-04-09 15:52:35

标签: ruby-on-rails

我是Rails的新手,正在努力为父母创建一个向那里的孩子们执行任务(任务)的应用程序。

但是当我尝试通过更改状态来更新任务时,我得到:缺少参数或值为空:任务

任务:

create_table "missions", force: :cascade do |t|
    t.string "name"
    t.integer "point"
    t.date "start_at"
    t.date "end_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.bigint "user_id"
    t.integer "status", default: 0
    t.index ["user_id"], name: "index_missions_on_user_id"
end

型号:

class Mission < ApplicationRecord
  belongs_to :user
  enum status: [:to_do, :pending, :done]
end

我的控制器:

class MissionsController < ApplicationController

  before_action :set_mission, only: [:show, :edit, :update, :destroy]

  def show
  end

  def edit
  end

  def update
    # @mission.update(params[:id])
    @mission.update(mission_params)
  end

private

  def set_mission
    @mission = Mission.find(params[:id])
    authorize @mission
  end

  def mission_params
    params.require(:mission).permit(:user_id, :start_at, :end_at, :point, :name, status: [:to_do, :pending, :done])
  end

我的任务显示

  <%= simple_form_for @mission do |f| %>
    <% f.input :status, value: 1 %>
    <%= f.button :submit, class: 'btn btn-primary' %>
  <% end %>

这是我的错误消息:

ActionController::ParameterMissing in MissionsController#update 
param is missing or the value is empty: mission
@mission
=> <Mission id: 2, name: "guest room", point: 3, start_at: "2019-03-16", end_at: "2019-05-16", created_at: "2019-04-09 12:12:25", updated_at: "2019-04-09 12:12:25", user_id: 5, status: "to_do">


params
=> <ActionController::Parameters {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"FYn7UAPJ7u+zQCcd+nNXUlEc2+NAWrdBWHhtfN2UPmex0kcG3VrgPUcIjDxdsmmA54DO/ee0Ks1XbGAtiR7tHw==", "commit"=>"Update Mission", "controller"=>"missions", "action"=>"update", "id"=>"2"} permitted: false>

我已经尝试了所有可以找到的解决方案,但是似乎没有一个适合我。 谢谢

1 个答案:

答案 0 :(得分:0)

您的表格没有任何输入。改为这样做:

 <%= f.input :status, value: 1 %>