[Rails] "pry-rails" that can be used when saving with the create method

Background

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".

controller

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

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]

Solution procedure

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.

Conclusion

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

Recommended Posts

[Rails] "pry-rails" that can be used when saving with the create method
Create a page control that can be used with RecyclerView
Organize methods that can be used with StringUtils
Ruby array methods that can be used with Rails (other than each)
About the matter that hidden_field can be used insanely
[ERROR message display] A simplified version that can be used at any time with the rails partial template.
Firebase-Realtime Database on Android that can be used with copy
Ruby on Rails 5 quick learning practice guide that can be used in the field Summary
Syntax and exception occurrence conditions that can be used when comparing with null in Java
Learning Ruby with AtCoder Beginners Selection [Some Sums] Increase the methods that can be used
SwiftUI View that can be used in combination with other frameworks
Set the access load that can be changed graphically with JMeter (Part 2)
Simple slot machine implementation that can be used with copy and paste
Set the access load that can be changed graphically with JMeter (Part 1)
Settings that should be done when operating a production environment with Rails
Performance analysis and failure diagnostic tools that can be used with OpenJDK
[Ruby On Rails] In the create action and destroy action, emergency measures when redirect_to action:: show cannot be (cannot be used)
[Android] I want to create a ViewPager that can be used for tutorials
[Rails] How to operate the helper method used in the main application with Administrate
The story that the port can no longer be used in the Spring boot sample program
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
Range where variables can be used with ruby [Scope]
Scala String can be used other than java.lang.String method
Addicted to the webpacker that comes standard with Rails 6
Convenient shortcut keys that can be used in Eclipse
[Spring Data JPA] Can And condition be used in the automatically implemented method of delete?
[Java 8] Sorting method in alphabetical order and string length order that can be used in coding tests
[RSpec] When I tested the view file that used the helper method, I got ActionView :: Template :: Error :. ..
[Rails] What to do when the view collapses when a message is displayed with the errors method
Why can I use the rails command installed with gem? ??
Create a jar file that can be executed in Gradle
The case that @Autowired could not be used in JUnit5
Be careful of initialization timing when using MessageEncryptor with Rails 5.2 / 6.0
[Question] Can nullif be used in the count function in JPQL?
Create a method that can retrieve characters from any location
[rails] Problems that cannot be registered / logged in with devise
Avoid the problem that session cannot be acquired when canary release when upgrading from Rails 4.2.x to 5.0.x
Let's create a Docker container that can connect to CentOS 8 with the minimum configuration by SSH
Static analysis tool that can be used on GitHub [Java version]
Build an environment where pip3 can be used with CentOS7 + Python3
Summary of ORM "uroboroSQL" that can be used in enterprise Java
I made a question that can be used for a technical interview
Power skills that can be used quickly at any time --Reflection
About the range and scope where Day16 variables can be used
Summary of JDK that can be installed with Homebrew (as of November 2019)
Pg_resetwal can be used to start the PostgreSQL Docker container when WAL is broken and cannot be started.
I implemented the multiple image upload function in Rails so that multiple images can be slid for the post
[OpenJDK11 & JavaFX13] Build a javaFX app with IntelliJ + gradle and create a package that can be distributed with a lightweight JRE
[Spring Boot] List of validation rules that can be used in the property file for error messages
I separated the same description as a method under private in Rails controller and implemented it so that it can be called by before_action