I was addicted to saving the model while creating the portfolio. I made a note of it because it was solved by using a gem called "pry-rails".
I made the following code and method.
records_controller.rb
class User::RecordsController < User::Base
##Omission...
def create
@record = current_app_user.records.build(record_params)
if @record.save
flash[:success] = 'Entered successfully'
redirect_to user_root_url
else
flash.now[:danger] = 'Did not enter successfully'
render :new
end
end
##Omission...
private def record_params
params.require(:record).permit(
:material,
:study_date,
:study_hour,
:study_minute,
:memo,
)
end
end
Model name: record The columns are as follows.
["id", :integer]
["material", :string] #I want to put teaching materials
["study_date", :date] #I want to put a date
["study_hour", :integer] #I want to put in study time
["study_minute", :integer] #I want to put in study time (minute)
["memo", :string] #I want to put a memo
["app_user_id", :integer]
["created_at", :datetime]
["updated_at", :datetime]
Added the following to the Gemfile
Gemfile.
group :development, :test do
gem 'pry-rails'
end
Bundle install in terminal
$bundle install
Added to Create Method
records_controller.rb
class User::RecordsController < User::Base
##Omission...
def create
binding.pry #Add this one! !!
@record = current_app_user.records.build(record_params)
if @record.save
##Omission...
Then try saving. Then the GUI goes into a standby state Command line operation is possible on the terminal. The screen below is displayed.
From: /apps/study-meter/app/controllers/user/records_controller.rb:20 User::RecordsController#create:
19: def create
=> 20: binding.pry
21: @record = Record.new(record_params)
22: if @record.save
23: flash[:success] = 'Entered successfully'
24: redirect_to user_root
25: else
26: flash.now[:danger] = 'Did not enter successfully'
27: render :new
28: end
29: end
[1] pry(#<User::RecordsController>)>
Now enter params. Then
[1] pry(#<User::RecordsController>)> params
=> <ActionController::Parameters {"authenticity_token"=>"y5Y+hutJRPbyX9VM0lpiIax4hfeF5TXoykVL35fLQV727TmH/+/f/qncyRtDaANL1h5kqIOErcrGRhfCYuYCKg==", "record"=>{"study_date"=>"2020-07-14", "memo"=>"Japanese history", "study_hour"=>"0", "study_minute"=>"3"}, "commit"=>"Post", "host"=>"study-meter.com", "controller"=>"user/records", "action"=>"create"} permitted: false>
If you look at this, you can see what value you were trying to put in which column. I was trying to put a memo in the "memo" column, and I was trying to put "Japanese history" in the material column.
Even so, Japanese history is included in the memo column, and no input is made in the material column in the first place. There, I notice that it is "funny".
Because material had a not null constraint "The material is not nil, but what does it mean to try to save it as nil?" The save probably didn't work.
・ The method should be fine -Unintended column You are trying to enter an unintended column
From this point, I was able to find out that the way to write the view form is strange. Sure enough, the code there was strange, so I fixed it and solved it.
pry-rails was a very useful library for identifying the cause of errors.
Recommended Posts