mercredi 9 février 2022

how to change a profile pic hash value in ruby on rails

when we change the profile pic the hash key value is not changing remaining same for all the changed images, due to this cache issue profile pic is not getting change immediately.

def link_to_profile_header(user)
  if user.avatar.exists?
    image_tag(user.avatar.url(:medium), :alt => user.display_name, :class=>"img-circle nav-profile-pic")
  else
    content_tag(:div, user.first_name[0,1].capitalize + "" + user.last_name[0,1].capitalize, :class=>"circle-new #{random_class_name_for_user_default_image}")
  end
end

mardi 8 février 2022

Issue running my Rails server: Error in reactor loop escaped: System error: Success - 0

When I launch my rail server and then go to localhost:3000, My localhost just keeps loading without any error messages. If I try to reload the page, i get Error in reactor loop escaped: System error: Success - 0 (Puma::MiniSSL::SSLError) What is this error and how can i fix it?

lundi 7 février 2022

rails console cannot load such file but with one letter capitalized it works?

ruby version: 2.7.3

Hi! I am very puzzled...

I have a file path company/service/v1/my_proto_pb.rb. When I tried to require it in rails console it gives the cannot load such file error:

2.7.3 :005 > require "company/service/v1/my_proto_pb"
Traceback (most recent call last):
/usr/local/rvm/gems/ruby-2.7.3/gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:34:in `require': cannot load such file -- company/service/v1/my_proto_pb (LoadError)

HOWEVER when I capitalize the letter v to V it works:

2.7.3 :006 > require "company/service/V1/my_proto_pb"
 => true

But the .rb and .rbi file both exist in the v1 folder. Why??? I cannot change the letter in code as it's auto-generated. Where exactly does the require look for and how can i check if what's being required exists or not?

Thank you!

Ruby POST request with an external file attached

Since a week ago, I've been facing this issue, but I've not found the solution. When I send a POST request to an API, it returns

{"errors":[],"message":"A file or fileKey is required","status":400,"type":"BadRequestError"}

This is what I am trying to do:

    url = URI("https://xxx.xxx/v2/attachments")

    https = Net::HTTP.new(url.host, url.port)
    https.use_ssl = true

    request = Net::HTTP::Post.new(url)
    request["X-Application"] = "IDENTIFICATION"
    form_data = [['name', 'my-file.pdf'],['parcelId', '123'],['file', Net::HTTP.get(URI("http://www.africau.edu/images/default/sample.pdf"))]]
    request.set_form form_data, 'multipart/form-data'
    response = https.request(request)
    puts response.read_body

Is something wrong with the way I attach the file?

dimanche 6 février 2022

Add label to select_tag ruby

i have using label like this in the first input

f.input :title, label: 'Título

enter image description here

in the second input i have

select_tag 'front_dashboard_formula[layout_type]', options_for_select(FrontDashboardFormula.layouts), :include_blank => true

how to use label, equal to the first input

Rails : ActionController::UnknownFormat when adding image in ajax

hi i am trying to upload a form via ajax , the form works fine when i send without a attachment but when i attach it gives ActionController::UnknownFormat , I am using activestorage migration for this

@items_list= Item.where(:category_id => params[:item][:category_id])
  respond_to do |f|
    f.js
  end```

Ruby Mastermind White Pegs Count

I'm a newbie to Ruby programming. It is my second OOP project. I have trouble with counting white pegs under certain condition. And Wikipedia says that:

-A black key peg is placed for each code peg from the guess which is correct in both colour and position.
-A white key peg indicates the existence of a correct colour code peg placed in the wrong position.

I believe that my black peg condition is true, but I can not count my white pegs correctly. Here is the function code piece:

def check_guess(code, guess)
    @white_peg = 0
    @black_peg = 0
    index = 0
    while index < code.length
      if code[index] == guess[index]
        @black_peg += 1
        puts "Black => #{@black_peg}"
      elsif guess.any? { |c| code.include?(c) } && code[index] != guess[index]
        @white_peg += 1
        puts "White => #{@white_peg}"
      end
      index += 1
    end
    puts "There are #{@white_peg} white peg and #{@black_peg} black peg"
    is_win?
  end
end

I'm invoking above function inside this condition

  def codebreaker_attempt
    guess_array = @codebreaker.guess_code
     @codebreaker.check_guess(@codemaker.code, guess_array)
  end

And lastly my game method is here for stop the game

def play
    codemaker_choose
    (1..12).each do |i|
      puts "#{i}. GUESS\n"
     #puts "Guess array is #{guess}"
      break if codebreaker_attempt == true
    end

I'm leaving here repl.it repo of all my codes if you want to inspect. https://replit.com/@Burakkepuc/Mastermind#main.rb