jeudi 22 août 2019

Scope of POROs in Rails

It seems like when using only class methods on a PORO like a Service in a Rails app and called by controllers, that there is only one instance of the Service object created across all requests.

I know Rails creates new instances of Controllers and Models per request, but haven't seen anything documented about how Rails treats your own POROs.

What's your understanding?

mercredi 21 août 2019

Extracting image index from array in order to insert image into a bootstrap carousel

I have an array of images which I can iterate over and display on my page however this is not what I want. I would like to find the index of each image in my array so that (on each loop) I can insert one of the images into my bootstrap carousel slides.

Will I need Javascript to do this? I am unsure and hope someone can point me in the right direction.

To start I tried to do a for each with index loop and extract the index but I was not able to :(

This is the bootstrap carousel. So far you can see that I have put my iteration into the first slide which means all 3 images are showing on the first slide:

<div class="carousel-inner" role="listbox">
    <div class="carousel-item active">
     <!--  <img class="d-block w-100" src="https://mdbootstrap.com/img/Photos/Slides/img%20(88).jpg"
       alt="First slide"> -->
      <!-- app/views/posts/show.html.erb -->
     <% if @service.images.attached? %>
      <% @service.images.each do |image| %>
        <%= image_tag(image) %>
        <% end %>
     <% end %>
    </div>
     <div class="carousesl-item">
   <!--   <img class="d-block w-100" src="https://mdbootstrap.com/img/Photos/Slides/img%20(121).jpg"
    alt="Second slide"> -->
    </div>
    <div class="carousel-item">
   <!--    <img class="d-block w-100" src="https://mdbootstrap.com/img/Photos/Slides/img%20(31).jpg"
     alt="Third slide"> -->
    </div>
  </div>
  <!--/.Slides-->
  <!--Controls-->
  <a class="carousel-control-prev" href="#carousel-thumb" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carousel-thumb" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
  <!--/.Controls-->
  <ol class="carousel-indicators">
    <li data-target="#carousel-thumb" data-slide-to="0" class="active">
      <img src="https://mdbootstrap.com/img/Photos/Others/Carousel-thumbs/img%20(88).jpg" width="100">
    </li>
    <li data-target="#carousel-thumb" data-slide-to="1">
      <img src="https://mdbootstrap.com/img/Photos/Others/Carousel-thumbs/img%20(121).jpg" width="100">
    </li>
    <li data-target="#carousel-thumb" data-slide-to="2">
      <img src="https://mdbootstrap.com/img/Photos/Others/Carousel-thumbs/img%20(31).jpg" width="100">
    </li>
  </ol>
</div>
<!--/.Carousel Wrapper-->
</div>

mardi 20 août 2019

How to fix 'Please run rails webpacker: install Error: No such file or directory @ rb_sysopen"?

I am attempting to create my first Ruby on Rails application following the documentation outlined on the website. When I attempted to run the server with 'rails server' from the cmd line I receive this error...

C:/Ruby/rubyinstaller-2.5.5-1-x64/rubyinstaller-2.5.5-1-x64/lib/ruby/gems/2.5.0/gems/webpacker-4.0.7/lib/webpacker/configuration.rb:91:in `rescue in load': Webpacker configuration file not found C:/Users/{username}/Desktop/Projects/Ruby on Rails/Blog Application/blog/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen - C:/Users/{username}/Desktop/Projects/Ruby on Rails/Blog Application/blog/config/webpacker.yml

OS: Windows 10
Yarn version: 1.17.3
Node.js version - v10.15.3
Rails version - 6.0.0
Ruby version - Ruby 2.5.5p157 (2019-03-15 revision 67260) [x64-mingw32]
sqlite3 version - 3.22.0

I expect for the server to run successfully and to open localhost:3000 to Yay! You're on rails

sort ruby hash by specific key

I have a ruby hash like this

 {"id"=>62, "name"=>"Wine and Spirits"}
 {"id"=>63, "name"=>"Tobacco"}
 {"id"=>64, "name"=>"Printing"}
 {"id"=>65, "name"=>"Information Services"}
 {"id"=>66, "name"=>"Business Supplies and Equipment"}

How do I sort this by name? I tried

 categories.sort_by {|_key, value, _key1, value1| value1}

But that did not work

Ruby : How to add a specific line from a document.txt to a comment field with specific id and then move to next line?

How can i add a specific line from my document.txt to youtube comment field with specific id and then move to next line on my document.txt . The id of youtube comment field is 'contenteditable-root' . I have created this code but the text that is been added on the youtube comment field show in brackets for example ["Hello"]

Or in second example it shows nothing

Example 1:

file = 'comments.txt'
File.readlines(file).each do |i|
            files = [i]
            files.each { |val| 

browser.execute_script("document.getElementById('contenteditable-root').innerHTML = '#{files}';")

}
end

Example 2:

line_number = 1 
loop do
comments = IO.readlines('comments.txt')[line_number-1]
browser.execute_script("document.getElementById('contenteditable-root').innerHTML = '#{comments}';")
line_number += 1 
end

lundi 19 août 2019

Image not showing for multiple image upload with active storage

I have followed a tutorial on how to upload multiple images using active record. I have installed active record, added my associations to my model and when I create a Service (my model) everything seems to be working when I upload 3 images i.e no Active Record errors however I cannot see my images on the FE, all I see is a broken images.

When I go into Rails c and type Service.images.last I can see that images is nil. Does anyone know why this is?

Is there anything else you need to know or see from me?

Thank you

My code to show the image:

<%=@service.images.each do  |img| %>
  <%= cl_image_tag @service.images, crop: :fill, class: 'card-image', class: 'card-image-show'%>
 <%end %>


Rails c

  Service Load (59.6ms)  SELECT  "services".* FROM "services" ORDER BY "services"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> #<ActiveStorage::Attached::Many:0x00007fffc3ff08a0 @name="images", @record=#<Service id: 14, name: "Test Carpet cleaning", description: "Lorem Ipsum is simply dummy text of the printing a...", picture_url: nil, video: nil, category: "carpet cleaning", created_at: "2019-08-19 12:32:35", updated_at: "2019-08-19 12:32:35", photo: nil, images: nil>, @dependent=:purge_later>

Service model

class Service < ApplicationRecord
  has_many_attached :images
  mount_uploader :photo, PhotoUploader
end


I would like my 3 images to display on the FE

samedi 17 août 2019

How to filter out one method call from many others with `expect().to receive()` in RSpec

I have such a code:

class ClassB
  def print_letter(arg)
  end
end

class ClassA
  def self.my_method
    ClassB.print_letter("a")
    ClassB.print_letter("b")
  end
end

RSpec.describe ClassA do
  describe "self.my_method" do
    it "prints a" do
      allow(ClassB)
      expect(ClassB).to receive(:print_letter).once.with("a")
      described_class.my_method
    end
  end
end

And I'm getting a failure:

#<ClassB (class)> received :print_letter with unexpected arguments
  expected: ("a")
       got: ("b")

Can I do anything with that? Is there any way to force the receive method to analyze all method calls and pick the one where arguments match, not only the last one? BTW, this behavior seems confusing to me.