[Ruby] [Rails] pry-rails that can be used when you cannot save by create method

2 minute read

Background

I was addicted to saving the model while creating the portfolio. I can solve it by using a gem called “pry-rails” so I will write it down.

controller

I had 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] ='Successfully entered'
      redirect_to user_root_url
    else
      flash.now[:danger] ='The input was not successful'
      render :new
    end
  end

## Omission...

  private def record_params
    params.require(:record).permit(
      :material,
      :study_date,
      :study_hour,
      :study_minute,
      :memo,
    )
  end
end

model

Model name: record The columns are as follows.

["id", :integer]
["material", :string] #I want to insert teaching materials
["study_date", :date] #I want to put the date
["study_hour", :integer] #I want to enter the study time (hour)
["study_minute", :integer] #I want to enter the study time (minute)
["memo", :string] #I want to put a memo
["app_user_id", :integer]
["created_at", :datetime]
["updated_at", :datetime]

Resolution procedure

Add the following to Gemfile

Gemfile.


group :development, :test do
  gem'pry-rails'
end

Bundle install at the terminal

$bundle install

Add to create method

records_controller.rb


class User::RecordsController <User::Base

## Omission...

  def create
    binding.pry # Add this guy! !!
    @record = current_app_user.records.build(record_params)
    if @record.save

## Omission...
  

Then try saving. Then the GUI goes into standby Command line operation is possible on the terminal. The screen below appears.

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] ='Successfully entered'
    24: redirect_to user_root
    25: else
    26: flash.now[:danger] ='The input was not successful'
    27: render :new
    28: end
    29: end
[1] pry(#<User::RecordsController>)>

Here, enter params. Then

[1] pry(#<User::RecordsController>)> params
=> <ActionController::Parameters {"authenticity_token"=>"y5Y+hutJRPbyX9VM0lpiIax4hfeF5TXoykVL35fLQV727TmH/+/f/qncyRtDaANL1h5kqIOErcrGRhfCYuYCKg==", "record"=0""">"""""" >"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 “Japanese history” in the material column.

However, Japanese history is included in the memo column, and no input is made in the material column. There, I notice “funny”.

material had a not null constraint With the flow of “What is the meaning of trying to save as nil even if material is not nil?” The save probably didn’t work.

・Method should be fine ・Unintended column You are trying to enter an unintended column

From this point, I was able to conclude that the form of the view was wrong. As expected, the code there was wrong, so I fixed it and solved it.

Conclusion

pry-rails was a very useful library for identifying the cause of an error.