lundi 31 juillet 2023

Can't able to create a simple login page in Rails

routes.rb

Rails.application.routes.draw do
  get '/login' , to: 'sessions#new'
  post '/login', to: "sessions#create"
end

sessions_controller.rb

class SessionsController < ApplicationController
  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      log_in user
      redirect_to user
    else
      flash.now[:danger]='Invalid login id'
      render 'new'
    end
  end

  def destroy
    log_out
    redirect_to root_url
  end
end

views/sessions/new.html.erb

<%= form_with url: login_path, local: true do |form| %>
    <%= form.label :email %>
    <%= form.email_field :email %>
    <%= form.label :password %>
    <%= form.password_field :password %>
    <%= form.submit "LogIn"%>
<% end %>

Error: NoMethodError in SessionsController#create undefined method `[]' for nil:NilClass Extracted source (around line #6): 5.def create 6. user = User.find_by(email: params[:session][:email].downcase) 7. if user && user.authenticate(params[:session][:password])

I still can't understand why is this happening.

tried everything to solve on my own but can't.

mercredi 26 juillet 2023

I'm working on implementing a search feature in my Ruby on Rails with Diacritic Search Matching Exact Word

 if params[:name]
      name_normalized = params.unicode_normalize(:nfkd).gsub("_", "\\_")
      brands = brands.where("name LIKE ?", "%#{name_normalized}%")
 end

When I search for the diacritic character "š" in the database, the search query does not return any results, even though there is a record with the name "Tešt" containing that character.

If I modify the query to make it diacritic, it returns results like "testing," "Test," "Tešt," and "demo_Test" when searching for "š." However, my desired result is only the exact match "Tešt" because I was specifically searching for this particular word.

Despite all this, the search still doesn't fetch the desired results. I suspect there might be an issue with the query or how I'm handling the search term.

I have used .gsub("_", "\\_") to handle cases involving underscores in my query, and it is working as expected. If I use it in the query alone, it functions correctly.

Can anyone please guide me on how to implement a diacritic search properly using Ruby on Rails and SQL? Any insights or suggestions would be greatly appreciated. Thank you!

mardi 25 juillet 2023

Ruby Datetime Error no implicit conversion of nil into String

I am getting the error "TypeError: no implicit conversion of nil into String" in Datetime method (ruby) and I wonder why my code is not working accordingly if I already have exception implemented. Any explanation about this or how to get rid of it?

      def get_parsed_datetime(raw_payload)
        parse_datetime(raw_payload[:datetime])
      rescue ArgumentError
        raise Base::UnprocessableContentError, I18n.t('invalid_datetime_error')
      end

      def parse_datetime(datetime_value)
        {
          :date => Date.parse(datetime_value).strftime('%d/%m/%Y'),
          :hour => Time.parse(datetime_value).hour
        }
      end

how to delete a record and go to delete page in rails 7 after installing ujs?

I am using Rails 7 and also install ujs but whenever i click on delete button Get request call rather than destroy request and i go to view record page rather then delete employee page. This is the code of delete link:

<%= link_to "Delete", employee_path(employee), method: :delete, class: "btn btn-danger", data: { confirm: "Are You sure to delete this employee" } %>

and this is the code of delete action from controller file:

def destroy
    @employee = Employee.find(params[:id])
    if @employee.destroy
      redirect_to employees_path, notice: "Employee has been deleted"
    end
  end

samedi 22 juillet 2023

Working on ruby on rails and caught in a situation where i can't determine why is it doing like that

I am working on a project called foodlog where the person can enter the food they eating the number of calories ,protiens, carbs, fats etc. The issue i am facing is if i create 2 entries in it , the display will show 4 of them if i place 3 entries in it, then 9 and so on . it's just reshowing the entries after each iteration and i can't determine why's that.

I have uploaded it on my github if anyone has time pls help. https://github.com/s-chuck/git_test in my opinion the problem is most probably on views folder and in views/_entry.html.erb file.

i have tried doing everything and i am expecting the entries should only be showed once in my website.

dimanche 16 juillet 2023

active admin gem error message just following the instructions

I am using ruby 3.1.3, rails 7.0.5 and activeadmin 3.0.

I just followed the installation instructions from the activeadmin website, but I got the following error:

ActionView::Template::Error (Ransack needs AdminUser attributes explicitly allowlisted as
searchable. Define a `ransackable_attributes` class method in your `AdminUser`
model, watching out for items you DON'T want searchable (for
example, `encrypted_password`, `password_reset_token`, `owner` or
other sensitive information). You can use the following as a base:

ruby
class AdminUser < ApplicationRecord

  # ...

  def self.ransackable_attributes(auth_object = nil)
    ["created_at", "email", "encrypted_password", "id", "remember_created_at", "reset_password_sent_at", "reset_password_token", "updated_at"]
  end

  # ...

end

):
    1: # frozen_string_literal: true
    2: insert_tag renderer_for(:index)

I tried to follow the instructions but I got the same error but now the array inside of the function is empty.

vendredi 14 juillet 2023

Trying to install Ruby 3.2.2 but keep getting error during installation

Please bear with me as this is my first time coding with Ruby, but I've been trying to install it for a while now but each time I keep getting the following error. Does anyone have any advice on how to fix this? I am currently on MacOS Ventura. picture of the error message in my terminal