Skip to content

Instantly share code, notes, and snippets.

View ahmad19's full-sized avatar
:electron:

Ahmad hamza ahmad19

:electron:
View GitHub Profile
@ahmad19
ahmad19 / _form.html.erb
Last active November 1, 2024 17:28
Render model validation errors inline by adding a simple presenter class
<%= form_with(model: post) do |form| %>
<% error_handler = ::InlineErrorHandler.new(post, self) %> # Initialize the error handler presenter here, or consider doing this in the controller
<div>
<%= form.label :title, style: "display: block" %>
<%= form.text_field :title %>
<%= error_handler.error_message_for(:title) %> # Display inline error for the title field here
</div>
<div>
<%= form.label :body, style: "display: block" %>
@ahmad19
ahmad19 / data.json
Created December 24, 2023 11:01
repo_json
{
"action": "created",
"installation": {
"id": 42355905,
"account": {
"login": "justinvoicit",
"id": 86925994,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjg2OTI1OTk0",
"avatar_url": "https://avatars.githubusercontent.com/u/86925994?v=4",
"gravatar_id": "",
@ahmad19
ahmad19 / gmail_password_create.txt
Created August 24, 2022 13:02
gmail app password creation
Account with 2-step Verification
If your Gmail account uses 2-step verification, you will need to get an app password and use that instead of your regular password. If you don't use 2-step verification, I recommend turning it on to avoid getting the emails blocked by Google.
To create an app password, go to your Google account settings and navigate to the "Security" tab. Under "Signing in to Google", click on the "App passwords" menu item (this will only be available if you have 2-step verification turned on). Next, select Other (Custom name) under the "Select app" dropdown and enter the name of your app or something else useful. Click "Generate" and your new app password will appear on the screen. Make sure you copy it before closing the window, or you won't be able to see the password again.
Now, in your Rails app mailer settings, replace the <gmail_password> with the new app password instead.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ahmad19
ahmad19 / _form.html.erb
Last active November 1, 2024 17:06
Render inline errors in Rails forms after submit
<%= form_with(model: post) do |form| %>
<% error_handler = ::InlineErrorHandler.new(post, self) %>
<div>
<%= form.label :title, style: "display: block" %>
<%= form.text_field :title %>
<%= error_handler.error_message_for(:title) %>
</div>
<div>
<%= form.label :body, style: "display: block" %>
@ahmad19
ahmad19 / floating_label.html
Last active March 19, 2021 23:56
Bootstrap 5's floating label with input group buttons
<div class="btn-toolbar mb-2" role="toolbar" aria-label="Toolbar with button groups">
<div class="col-md-10">
<div class="form-floating">
<input type="email" class="form-control" id="floatingInput" placeholder="[email protected]">
<label for="floatingInput">Search Customer</label>
</div>
</div>
<div class="col-md-2 text-center">
<div class="btn-group mt-1" role="group" aria-label="First group">
<button class="btn btn-light btn-lg btn-outline-primary" type="button">
@ahmad19
ahmad19 / config.rb
Last active February 9, 2021 11:18
Steps to allow email sending on heroku through sidekiq.
# Create Procfile which is required by heroku.
web: bin/rails server -p $PORT
worker: bundle exec sidekiq -c 2 -e staging
# REDIS
# REDISTOGO_URL for local is bydefault: redis://0.0.0.0:6379/0
# REDISTOGO_URL for heroku
# Use Redistogo heroku addon
# Addon will provide REDISTOGO_URL which should be added in the heroku config.
@ahmad19
ahmad19 / rspec_matchers_cheatsheet.rb
Last active August 3, 2022 14:31
RSpec matchers cheatsheet
# To match only few attributes of an object
# have_attributes
expect(person).to have_attributes(:name => "Jim", :age => 32)
# To match attributes of a collection
expect(notes)
.to match_array([
have_attributes(
notable_id: talent.id,
@ahmad19
ahmad19 / grouped_options.rb
Created December 29, 2020 11:43
Rails example of an ajax form with a select_tag of grouped options
<%= form_tag(submit_path, remote: true, method: :get, id: 'add_line_item') do %>
<div class="col">
<div class="form-row">
<div class="col-md-10">
<%= grouped_collection_select(:line_item, :product_id, current_user.categories, :products, :name, :id, :name, { prompt: '--Select Product--' }, { class: 'form-control form-control-sm' }) %>
</div>
<div class="col text-center">
<%= submit_tag 'Add', class: 'btn btn-outline-secondary btn-sm'%>
</div>
@ahmad19
ahmad19 / match_only_few_attributes.rb
Created December 21, 2020 18:46
Rspec expect to match only few attributes in the response body
expect(response.parsed_body['data']).to(include(
'active' => true,
'name' => 'some group',
'once_per_user' => true
))