vendredi 30 juin 2023

(Ruby on Rails validates_confirmation_of) How to switch error message to the confirmation field?

In Ruby on Rails 3.2, I am trying to validate password confirmation with this:

validates_confirmation_of :password

Rails will add the validation error to the :password field. How do I make the validation error be added to the :password_confirmation field instead?

samedi 24 juin 2023

AI websites explore now

I collected the best 26 AI websites that can help marketers, Developers, Editors, Designers

Artificial intelligence (AI) is a rapidly growing field that has been making waves in various industries. Notably, AI is being used to automate and optimize different processes, making them more efficient and accurate.

jeudi 22 juin 2023

I can't update it

The entry is not updated, it should change between true and false

class AsistenciaController < ApplicationController
  def asistencia
    @asistencia = User.all
  end

  def update
    @username = User.find(params[:username])
    @entry = User.find(params[:entry])
    if @asistencia.update(asistencia_params)
      redirect_to asistencia_path, notice: "Asistencia guardada correctamente"
    else 
      render :edit, status: :unprocessable_entity
    end
  end
end

Started POST "/guardar_asistencia" for 127.0.0.1 at 2023-06-22 10:56:34 -0500
Processing by AsistenciaController#update as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "user_username"=>"", "username"=>"1", "entry"=>"false", "commit"=>"Registrar"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/asistencia_controller.rb:7:in `update'
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", nil], ["LIMIT", 1]]
  ↳ app/controllers/asistencia_controller.rb:8:in `update'
Completed 404 Not Found in 19ms (ActiveRecord: 0.4ms | Allocations: 4468)



ActiveRecord::RecordNotFound (Couldn't find User with 'id'=false):

I tried with strong parameters and with patch but it doesn't work

I was hoping that by inserting the username I could change the entry between true and false

mercredi 21 juin 2023

How to render filter in index page in active admin

I filter the index page using the filter. Then I go to the view page and come back to the index page. How can I retain the currently applied filter until the clear filter is clicked?

Can someone please help with this? Thanks in advance.

samedi 17 juin 2023

Rails calculating a virtual column using 2 other virtual columns

I need help converting the following MySQL query into an ActiveRecord Query that will produce a relation with IP records as object. To explain this in a better way, How to convert the following query in How to sum two virtual column into third column in sql in ActiveRecord query ?

select a_id
u_c,
i_c,
(u_c + i_c) as t_c
from (
    select distinct a.id as a_id,
    (   SELECT count(b.id) FROM UP b
        WHERE b.i_p_id = a.id
        AND b.role = "L"
    ) as u_c,
    (   SELECT count(b.id) from UP b
        WHERE b.institution_package_id = a.id
        AND b.role = "I"
    ) as i_c
    from IP a
    left outer join UP b on b.IP = a.id       
) sub 

Let me explain a litte more. I have 2 tables where I am querying into the 1st table and calculating 3 virtual columns. The 3rd column will be the sum of the other 2 virtual columns. Thanks for the help.

jeudi 15 juin 2023

Gem devise_auth_token: How to utilize the remember_created_at field?

I'm using the gem devise_token_auth for authentication in my Ruby on Rails web application. I noticed that there is a remember_created_at field created as part of the gem's functionality. However, I'm unsure about the purpose or intended use of this remember_created_at field.

I tried making a login request with the following details:

POST http://localhost:3000/auth/sign_in
{
    "email": "....",
    "password": "...",
    "remember_me": true
}

But I observed that the remember_created_at field remains unset and has a value of null.

Could someone please clarify the purpose of the remember_created_at field in devise_token_auth and how it should be properly utilized? Am I missing any additional configurations or steps to ensure the value is set correctly when remember_me is set to true?

Thank you in advance for any insights or guidance.

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