RahulOnRails
RahulOnRails

Reputation: 6542

ROR + Failed to store data in database (TypeError Exception: no implicit conversion of nil into String)

In my Rails 4.1 application, I am trying to store parameters but I am getting strange error. May be I am missing something to add.

Error: TypeError Exception: no implicit conversion of nil into String

Parameters:

{"utf8"=>"✓", 
 "authenticity_token"=>"G9RGcjKJ/1Eb1NHe5P5bDtT96iC0tUgFdssE=", 
 "monthly_timing"=>
       {"month_year"=>"201503",
        "month_date"=>{"2"=>"1", "3"=>"3",  "4"=>"34"}
       },
 "commit"=>"Store"}

Controller:

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

Upvotes: 0

Views: 80

Answers (1)

railsdog
railsdog

Reputation: 1501

There is no month_year as a first-level element in your params. params[:monthly_timing][:month_year] perhaps?

Generally speaking, convention would say that:

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

Upvotes: 2

Related Questions