mercredi 14 juin 2023

Updated heroku stack and now having will_paginate error

So I updated my heroku stack on my web app and am using ruby 3.2.2 with will_paginate 4.0, but am now receiving an error on one of my pagination pages. The specific error I'm getting is

2023-06-15T00:33:11.759517+00:00 app[web.1]: Rendered users/index.html.erb within layouts/application (Duration: 2.6ms | Allocations: 1025)
2023-06-15T00:33:11.759543+00:00 app[web.1]: Rendered layout layouts/application.html.erb (Duration: 2.7ms | Allocations: 1069)
2023-06-15T00:33:11.759701+00:00 app[web.1]: Completed 500 Internal Server Error in 7ms (ActiveRecord: 2.5ms | Allocations: 2352)
2023-06-15T00:33:11.760282+00:00 app[web.1]: 
2023-06-15T00:33:11.760282+00:00 app[web.1]: ActionView::Template::Error (wrong number of arguments (given 4, expected 3)):
2023-06-15T00:33:11.760282+00:00 app[web.1]: 13:   <h3><%= link_to "Create New Dude", new_user_path %></h3>
2023-06-15T00:33:11.760283+00:00 app[web.1]: 14: <% end %>
2023-06-15T00:33:11.760283+00:00 app[web.1]: 15:
2023-06-15T00:33:11.760284+00:00 app[web.1]: 16: <%= will_paginate %>
2023-06-15T00:33:11.760284+00:00 app[web.1]: 17:
2023-06-15T00:33:11.760284+00:00 app[web.1]: 18: <table>
2023-06-15T00:33:11.760284+00:00 app[web.1]: 19:   <thead>
2023-06-15T00:33:11.760284+00:00 app[web.1]: 
2023-06-15T00:33:11.760285+00:00 app[web.1]: app/views/users/index.html.erb:16

With the actual code being

<%= will_paginate %>

<table>
  <thead>
    <th></th> <!-- Icon -->
    ... a bunch of stuff you don't need to see ...
  </thead>
  <tbody>
    <%= render @users %>
  </tbody>
</table>

<%= will_paginate %>

I can't find any clear documentation on will_paginate 4.0 on why this would be throwing that specific error of wrong number of arguments (given 4, expected 3). The only thing I can find is the new basic will_paginate use here https://github.com/mislav/will_paginate. Do I need to somehow bundle my table that I used to paginate into a new structure?

Any help would be greatly appreciated.

lundi 12 juin 2023

Show image from controller in rails

I am displaying an image in view using <%= image_tag(@staff.attachment_url(@staff.image), alt: "No image", width: "100px", height: "100px") %> which is working fine.

Here attachment_url is method for creating dynamic url every time you open the image i.e. staff_profiles/images/b64f8b457dddf968712401bf07a8d08ba7f1ce3a326598ec2144f1c995309e06 and also this image is valid for 30 second only and after 30 second when you refresh or open the same url it will show Access denied.

Everything here is working fine.

Like this image i have other attachments to download/view with download link. when any user open page this attachment_url calls at the time of page load / show method but after 30 seconds this download link expires and when user click on the link it shows Access Denied. I want to call the attachment_url when user click on download link after 1 minute or 5 minutes or 10 minutes of page load.

So I need to open the image or create download link url from controller, Not like mentioned above and also want to use the method given above attachment_url which will help to create url dynamic.

Thanks

dimanche 11 juin 2023

FFMPEG gem is not generate correct bitrate value for a video in rails

I am using FFMPEG gem in rails, and I want to store bitrate value for a video in a table

I put this code in utility library/module

movie = FFMPEG::Movie.new(path)
media_detail = {duration: movie.duration, bitrate: movie.bitrate, resolution: movie.resolution}

On the show page I am using number_to_human_size rails helper method for more understandable representation

number_to_human_size(convert_kilobyte_to_byte(file.bitrate))

def convert_kilobyte_to_byte(bitrate_value)
     (bitrate_value.to_i*125)
end

FFMPEG gem generates everything as expected but in bitrate there is some problem and i really don't know why

mardi 6 juin 2023

I want to use a Datetime to save the entry and exit time, without the time being altered

Example of what I expect:

Time: 15:36 p.m.

Entrance = 12:30 pm

Exit = 15:36 p.m

Example of what I get:

Time: 15:36

Entry: 15:36

Exit: 15:36

Try with datetime.now, datetime.new

<%= fields = [:id, :email, :username, :entry, :entrada, :salida].join(";") %>
<% @users.each do |users| %>
<% id = users.id %>
<% email = users.email %>
<% username = users.username %>
<% entry = users.entry %>
<% entrada = Time.zone.now %>
<% salida = Time.zone.now %>
<%= [id.to_s, email, username, entry, entrada, salida].join(";") %> 
<% end %>    

How to debug rails application code in deployment environment

I am deploying a rails application on fly.io. The application is throwing an error on the web page but not getting details of the error on the flyctl logs (application logs). Please, how do I go about getting the error details on the application logs?

Thanks

"no implicit conversion of Array into String"

please I just developed a game in Ruby on Rails, and I am about to deploy it. Everything works fine in the development stage but keeps on getting "ActionView::Template::Error (no implicit conversion of Array into String):" in the deployment environment. The error pointed me to a particular line of code which is,

def passcode
  JSON.parse(game.passcode)
end

This is the output of my object before the JSON.parse;

"[\"ORANGE\", \"YELLOW\", \"PURPLE\", \"GREEN\"]"

The method above output;

["ORANGE", "YELLOW", "PURPLE", "GREEN"]

The passcode was used to get the game outcome after the player won or lost the game.

def game_outcome
 if !attempts.empty? && passcode == last_attempt
  return "Congratulations!"
 elsif attempts.size == guesses.size && passcode != last_attempt
  return "Sorry, you've lost! It was a bit difficult"
 end
end

I have tried to see the output of the method on my local machine and it outputs an array, which I think it's the right thing

I will appreciate your assistance.

Overriding module's private method which included to controller from external gem in Rails's Rspec test

I want to override private method as same as i wrote at title.

I have rspec controller test code, and i have to change some part of code.

I'm testing controller's method, and it is using external gem's private method.

  • Structure

External gem
┗ Module A
┗ Module B (inside Module A)
┗ private method Z
# def Z
      puts 1
      puts 2
      puts 3
end

Rails App
┗ Controller 1
┗ public action method login
┗ #login method calls A::B::Z


Rspec test
┗ Controller 1 (RSpec.describe Controller_1, type: :controller ...)
┗ test login (describe '#login' ..., context 'with wrong method..')
┗
# I WANT TO CHANGE 'method Z' IN HERE LIKE
def Z
      puts 1
      puts 100
      puts 3
end


Can i do this in Rspec in Rails application?




if i use stub like this,

  before do
    allow(controller).to receive(:method_Z) #and contains puts 1, puts 100, puts3 ...
  end

  expect(response) #contains puts 1, puts 100, puts3 ...

i thought test would not be as i expected, so i'm wondering how to override method in rspec..