lundi 24 août 2020

How to fill in the AWS data for storage.yml

I want to push a Ruby on Rails app to heroku. But it gets stuck.

remote:        Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
remote:        Some gems seem to be missing from your vendor/cache directory.
remote:        Could not find aws-eventstream-1.1.0 in any of the sources
remote:        Bundler Output: Some gems seem to be missing from your vendor/cache directory.
remote:        Could not find aws-eventstream-1.1.0 in any of the sources
remote:
remote:  !
remote:  !     Failed to install gems via Bundler.
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote:
remote:  !     Push failed
remote: Verifying deploy...
remote:
remote: !   Push rejected to pure-crag-52432.
remote:
To https://git.heroku.com/pure-crag-52432.git
 ! [remote rejected] user-microposts -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/pure-crag-52432.git'

No I am not sure in how to fill in field below for the storage.yml as given in this guide: .

amazon:
  service: S3
  access_key_id:     <%= ENV['AWS_ACCESS_KEY_ID'] %>
  secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
  region:            <%= ENV['AWS_REGION'] %>
  bucket:            <%= ENV['AWS_BUCKET'] %>

Should I just leave it as it is and will the heroku configuration automatically be linked to this file?

I tried this (of course I changed all the answers a bit):

amazon:
  service: S3
  access_key_id:     AKIAXOOFZZFFKMQD3CFD
  secret_access_key: Cj8BcL452tDer5ryTPBRlan5LHOq76WXgvTDHmvc
  region:            region=eu-east-3
  bucket:            rails-tutorial-joost

These are my Heroku configurations:

➜  sample_app git:(user-microposts) heroku config
 ›   Warning: heroku update available from 7.42.2 to 7.42.6.
=== pure-crag-52432 Config Vars
AWS_ACCESS_KEY_ID:        AKIAXOOFOGFKKMQD3CFE
AWS_BUCKET:               rails-tutorial-joost
AWS_REGION:               region=eu-east-3
AWS_SECRET_ACCESS_KEY:    Ci8BcL452tcxDer5ryTPBRlan5LHOq76WXgvTDHmvc
DATABASE_URL:             postgres://blohftwliwefcg:6cfd2ccz2cc27301f86a1fe3c4686bb77784e67a62312d55c7e5751dfd61331c156@ec2-34-192-173-173.compute-1.amazonaws.com:5432/davr7msm9qlqh1
LANG:                     en_US.UTF-8
RACK_ENV:                 production
RAILS_ENV:                production
RAILS_LOG_TO_STDOUT:      enabled
RAILS_MASTER_KEY:         ./config/credentials/production.key
RAILS_SERVE_STATIC_FILES: enabled
SECRET_KEY_BASE:          32c6c8d057cc6c61071f18429e64zxczxc337d795bfa81abd04ed3e74377eff9e6cddb024967e6a0ffb9bc7de55b408ad71291ab5e518bcfe20bb5a7b44871d570cc61
SENDGRID_PASSWORD:        bdsk6q67625964
SENDGRID_USERNAME:        appasd6786712@heroku.com

jeudi 20 août 2020

How to validate error message before update in ruby on rails

I got the error message before save. But I can't validate the error messages before update.

      def create
        # @student = Student.new
        @student = Student.new(student_params)
        
        # @student = @student.school.build(student_params)
        if @student.valid?
          @student.save
            redirect_to students_path
            # render 'new'
        else
            render 'new'
        end
      end
    
      def edit
        @schools = School.all
        @student = Student.find(params[:id])
      end
    
      def update
        @student = Student.find(params[:id])
         if @student.valid?
          @student.update(student_params)
            redirect_to students_path
        else
          render 'edit'
        end
      end

def student_params params.require(:student).permit(:status, :email, :password, :school_id, :department_id)

end

Adding values to array hash ruby at a specific postion

I have a hash response from a JAVA service which looks like this :

jsonResponse = {:json=>{"reply"=>[{"person"=>"abc", "roll_no"=>"1234", "location"=>"loc1", "score"=>"1"}, {"person"=>"def", "roll_no"=>"1235", "location"=>"loc2", "score"=>"2"},{"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "score"=>"3"}]}, :status=>200}

I have to add one key value pair at a specific position to each of these reply array objects, so that the response transforms to something like this , to make it simpler for now, lets try adding a samke key value pair at a particular position:

jsonResponse = {:json=>{"reply"=>[{"person"=>"abc", "roll_no"=>"1234","location"=>"loc1", "new_value => "new_result", "score"=>"1"}, {"person"=>"def", "roll_no"=>"1235", "location"=>"loc2","new_value => "new_result", "score"=>"2"},{"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "new_value => "new_result", "score"=>"3"}]}, :status=>200}

This is what I have tried ,I run .each through jsonResponse :

jsonResponse[:json]['reply'].each do |object|
               objectArray = object.to_a
               insert_at = objectArray.index(objectArray.assoc('score'))
               object = Hash[objectArray.insert(insert_at, ['new_value','new_result'])]
               print("\n\nTest\n\n")
               print object
      end
    print("\n\nFinal Response\n\n")
    print jsonResponse

The object which i am printing has the desired response but it does not get updated in the jsonResponse

This is the output of the above code snippet:


Test

{"person"=>"abc", "roll_no"=>"1234", "location"=>"loc1", "new_value"=>"new_result", "score"=>"1"}

Test

{"person"=>"def", "roll_no"=>"1235", "location"=>"loc2", "new_value"=>"new_result", "score"=>"2"}

Test

{"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "new_value"=>"new_result", "score"=>"3"}

Final Response

{:json=>{"reply"=>[{"person"=>"abc", "roll_no"=>"1234", "location"=>"loc1", "score"=>"1"}, {"person"=>"def", "roll_no"=>"1235", "location"=>"loc2", "score"=>"2"}, {"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "score"=>"3"}]}, :status=>200}

mercredi 19 août 2020

How to get data from database with specific combination of name

I am working on ruby on rails project and I need to get data from the database with a specific combination like c*_c*_othername * can be numbered 1,2,3 etc like c1_c1_anystring. Prefix c is fixed for all time. I am trying with following code but it's not working

Topic.where("name like ?", "%c*_c*_*%")

mardi 18 août 2020

Heroku: upgrading Ruby application from Cedar-14 to Heroku-18

I am working on upgrading the heroku stack of my Ruby application from Cedar-18 to Heroku-18. The Ruby version of my application is 2.0.0 however the Heroku-18 stack is based on Ubuntu 18.04 requires a higher version of Ruby. As my application uses an older version, upgrading is complex and time consuming. With that said,

  1. Can I still be able to run with the same older versions of Ruby, Ruby Gems and Rails on Heroku-18 stack at my own RISK? As I am still be able to run my application locally on Ubuntu 18.04
  2. Does heroku supports SSH to servers for installation of older versions and related plugins like AWS?

lundi 17 août 2020

How to set a random number before_save for a model?

I have a Produt model, that if the user left blank, I need to generate a random number. How can I achieve that?

  def self.set_random_number()
   if self.code == null
    self.code.rand(1000..99999)
   end
  end

I tried this method, and called before_save :set_random_number(). But it doesn't work. Could you guys please help me?