错误 + 无法在数据库中存储数据(TypeError 异常:没有将 nil 隐式转换为 String)
ROR + Failed to store data in database (TypeError Exception: no implicit conversion of nil into String)
在我的 Rails 4.1 应用程序中,我试图存储参数,但出现了奇怪的错误。可能是我缺少要添加的内容。
错误:TypeError Exception: no implicit conversion of nil into String
参数:
{"utf8"=>"✓",
"authenticity_token"=>"G9RGcjKJ/1Eb1NHe5P5bDtT96iC0tUgFdssE=",
"monthly_timing"=>
{"month_year"=>"201503",
"month_date"=>{"2"=>"1", "3"=>"3", "4"=>"34"}
},
"commit"=>"Store"}
控制器:
input_params = ActionController::Parameters.new(params)
@monthly_timing = MonthlyTiming.new
@monthly_timing.user_id = current_user.id
@monthly_timing.month = input_params.require(:month_year)
@monthly_timing.data_date_wise = input_params.require(:month_date)
if @monthly_timing.save
format.html { redirect_to "/Index", notice: 'Timing was successfully created.' }
else
format.html { redirect_to "/Error", notice: 'EROROROOROROROROR.' }
end
您的参数中没有 month_year 作为一级元素。 params[:monthly_timing][:month_year] 也许?
一般来说,约定俗成:
class MonthlyTimingsController < ApplicationController
before_action :set_monthly_timing, only: [:show, :edit, :update, :destroy]
...
def update
if @monthly_timing.update(monthly_timing_params)
# what after a save?
else
render :edit
end
end
def set_monthly_timing
@monthly_timing = MonthlyTiming.find(params[:id])
end
def monthly_timing_params
params.require(:monthly_timing).permit(:month_year, :month_date)
end
end
在我的 Rails 4.1 应用程序中,我试图存储参数,但出现了奇怪的错误。可能是我缺少要添加的内容。
错误:TypeError Exception: no implicit conversion of nil into String
参数:
{"utf8"=>"✓",
"authenticity_token"=>"G9RGcjKJ/1Eb1NHe5P5bDtT96iC0tUgFdssE=",
"monthly_timing"=>
{"month_year"=>"201503",
"month_date"=>{"2"=>"1", "3"=>"3", "4"=>"34"}
},
"commit"=>"Store"}
控制器:
input_params = ActionController::Parameters.new(params)
@monthly_timing = MonthlyTiming.new
@monthly_timing.user_id = current_user.id
@monthly_timing.month = input_params.require(:month_year)
@monthly_timing.data_date_wise = input_params.require(:month_date)
if @monthly_timing.save
format.html { redirect_to "/Index", notice: 'Timing was successfully created.' }
else
format.html { redirect_to "/Error", notice: 'EROROROOROROROROR.' }
end
您的参数中没有 month_year 作为一级元素。 params[:monthly_timing][:month_year] 也许?
一般来说,约定俗成:
class MonthlyTimingsController < ApplicationController
before_action :set_monthly_timing, only: [:show, :edit, :update, :destroy]
...
def update
if @monthly_timing.update(monthly_timing_params)
# what after a save?
else
render :edit
end
end
def set_monthly_timing
@monthly_timing = MonthlyTiming.find(params[:id])
end
def monthly_timing_params
params.require(:monthly_timing).permit(:month_year, :month_date)
end
end