摆动为强参数后,db-query没有给出任何值

问题描述 投票:0回答:2

我正在尝试让此redmine-plugin在redmine4.x中工作。

我做了my own repo以管理所有更改。

我已经设法通过replacing attr_accessible by attr_accessor运行该插件,并通过应用给定的here将其更改为强参数。

在此状态下,插件在redmine 4.x中运行没有错误

但是:现在我正在努力将内容保存到数据库。我发现由于某种原因,必须保存的参数未移交给查询:

INSERT INTO wl_user_vacations VALUES ()

这里是使用的代码:

class WlUserVacationsController < ApplicationController
[...]
  def create
    @wl_user_vacation = WlUserVacation.new(wl_user_vacations)
    @wl_user_vacation.user_id = User.current.id
    if @wl_user_vacation.save
      redirect_to action: 'index', notice: 'Vacation was successfully saved.', year: params[:year]
    else
      respond_to do |format| 
        format.html {lash[:error] = "<ul>" + @wl_user_vacation.errors.full_messages.map{|o| "<li>" + o + "</li>" }.join("") + "</ul>" render :action => 'new' }
      format.api  { render_validation_errors(@wl_user_vacation) }
    end 
  end
end

private
[...]
  def wl_user_vacations
    params.require(:wl_user_vacations).permit(:user_id,:date_from,:date_to,:comments,:vacation_type)
  end
end

使用以下命令在@wl_user_vacation.save中的create上保存失败:

Processing by WlUserVacationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"*********", "wl_user_vacations"=>{"date_from(3i)"=>"7", "date_from(2i)"=>"12", "date_from(1i)"=>"2019", "date_to(3i)"=>"7", "date_to(2i)"=>"12", "date_to(1i)"=>"2019", "comments"=>"", "vacation_type"=>""}, "year"=>"", "commit"=>"Hinzufügen"}
  Current user: boremski (id=10)
Completed 500 Internal Server Error in 20ms (ActiveRecord: 9.4ms)

ActiveRecord::NotNullViolation (Mysql2::Error: Field 'user_id' doesn't have a default value: INSERT INTO `wl_user_vacations` VALUES ()):

plugins/redmine_workload/app/controllers/wl_user_vacations_controller.rb:38:in `create'

如果需要,我可以共享更多代码,甚至可以共享一些调试信息(可变值和更多)。

有人能帮忙或给我提示吗?

ruby-on-rails strong-parameters redmine-plugins
2个回答
0
投票

此处未将值传递到保存功能。您可以共享用于创建的表单吗?


0
投票

要求应该是:wl_user_vacation,而不是:wl_user_vacations

您还可以使用tap ...将当前用户ID添加到您的强参数中>

 def wl_user_vacations

   params.require(:wl_user_vacation).permit(:user_id, :date_from, :date_to,:comments, :vacation_type).tap do |p|
     p[:user_id] = User.current.id
   end

 end
© www.soinside.com 2019 - 2024. All rights reserved.