Snippets Collections
<%= label_tag :search, "Type something to search", class: "sr-only" %>
<%= text_field_tag "search", params[:search] %>
<%= submit_tag "Search" %>
<%= form_for :form, html: { class: "inaccessible-form" } do |f| %>
  Product name: <%= f.text_field :product_name %>
  Description: <%= f.text_field :description %>
  In stock: <%= f.check_box :available %>
  <%= f.submit "Save" %>
<% end %>
# original code found at
# https://github.com/forem/forem/blob/8cbbc222e9a9c31f8cb8ab5e7b1df08c2109885b/app/views/admin/shared/_destroy_confirmation_modal.html.erb

<p id="confirmation-text-instructions">To confirm this update, type in the sentence: <br />
  <strong>My username is @<%= current_user.username %> and this action is 100% safe and appropriate.</strong>
</p>
<div id="mismatch-warning" class="crayons-notice crayons-notice--warning hidden" aria-live="polite">
  The confirmation text did not match.
</div>
  <input
    aria-label="Type the sentence above to confirm this update"
    aria-describedby="confirmation-text-instructions"
    type="text"
    id="confirmation-text-field"
    class="crayons-textfield flex-1 mr-2"
    placeholder="Confirmation text" />
</div>
describe SvgHelper do
  include SvgHelper

  describe '#inline_svg' do
    subject { inline_svg(svg, options) }
    let(:svg) do
      '<svg viewBox="0 0 10 20"><circle cx="50" cy="50" r="50"/></svg>'
    end
    let(:parsed_file) { Nokogiri::HTML::DocumentFragment.parse(svg) }

    before do
      allow(File).to receive(:read).and_return(parsed_file)
    end

    context "when no options are passed" do
      let(:options) { {} }

      it "returns hidden svg" do
        expect(subject).to include 'aria-hidden="true"'
      end
    end
    
    context "when options are passed" do
      let(:options) { { title: 'test', style: 'background-color:Blue' } }
      
      it "returns svg with a role of img" do
        expect(subject).to include 'role="img"'
      end

      it "adds title with correct id" do
        expect(subject).to include '<title id="svg-title">test</title>'
      end
      
      
      it "adds aria-labelledby attribute" do
        expect(subject).to include 'aria-labelledby="svg-title"'
      end

      it "updates svg styles" do
        expect(subject).to include 'style="background-color:Blue"'
      end
    end
  end
end
module SvgHelper
  def inline_svg(filename, options = {})
    parse_file(filename) if options.empty?

    svg = svg_from_file(filename)
    update_svg_attributes(svg, options)

    if options[:title].present?
      svg = build_with_title(svg, options[:title])
    else
      hide_svg(svg)
    end

    svg.to_html.html_safe
  end

  private

  def read_file(filename)
    File.read(Rails.root.join('app', 'assets', 'images', filename))
  end

  def parse_file(filename)
    file = read_file(filename)
    Nokogiri::HTML::DocumentFragment.parse(file)
  end

  def svg_from_file(filename)
    doc = parse_file(filename)
    doc.at_css('svg')
  end

  def update_svg_attributes(svg, options)
    svg['class'] = options[:class] if options[:class].present?
    svg['style'] = options[:style] if options[:style].present?
    svg['viewBox'] = options[:viewbox] if options[:viewbox].present?
  end

  def build_with_title(svg, title)
    svg['role'] = "img"
	svg['aria-labelledby'] = "svg-title"
    svg_tag = svg.to_s.match(/<svg .*/)
    svg_title = "<title id='svg-title'>#{title}</title>"
    svg_with_title = svg_tag.to_s + svg_title + svg.children.to_s + "</svg>"
    Nokogiri::HTML::DocumentFragment.parse(svg_with_title)
  end

  def hide_svg(svg)
    svg['aria-hidden'] = true
  end
end
<%= link_to "I'm technically a link", "#", class: "button" %>
<fieldset>
  <legend>Which color do you prefer?</legend>
  <%= form_for :color do |f| %>
    <%= radio_button :color, :select, :red %> 
    <%= label :color, :select_red, 'Red' %>
    <%= radio_button :color, :select, :green %> 
    <%= label :color, :select_green, 'Green' %>
    <%= radio_button :color, :select, :blue %>
    <%= label :color, :select_blue, 'Blue' %>
  <% end %>
</fieldset>
<%= f.input :username, label: 'Your username' %>
<%= f.input :username, label_html: { class: 'sr-only' } %>
 <%= form_for :product do |f| %>
  <%= f.label :product_name, "Product name" %>
  <%= f.text_field :product_name %>
  <%= f.label :description, "Description" %>
  <%= f.text_field :description %>
  <%= f.label :available, "In stock" %>
  <%= f.check_box :available %>
  <%= f.submit "Save" %>
<% end %>
<%= inline_svg 'edinburgh.svg', title: 'A drawing of the Edinburgh skyline on a purple background' %>
<%= inline_svg_tag('icon.svg', aria_hidden: true) %>
<%= inline_svg_tag('icon.svg',
    aria: true,
    title: 'An SVG',
    desc: 'This is my SVG. There are many like it. You get the picture')
%>
<%= image_tag "rails.png", alt: "Alternative text" %>
# with aria-live
<% flash.each do |msg| %>
  <p aria-live="polite" aria-atomic="true"><%= msg %></p>
<% end %>
 
# with role
<% flash.each do |msg| %>
  <p role="status"><%= msg %></p>
<% end %>
# original code found at
# https://github.com/forem/forem/blob/5e93d3a25ecc800703aa100d286c8c75c9173ddf/app/views/users/edit.html.erb#L77

<% if @user.unconfirmed_email.present? %>
  <div class="crayons-notice crayons-notice--warning mb-6" role="alert">
    <h3 class="mb-2"><%= t("views.settings.email") %></h3>
    <p><%= t("views.settings.finalize_html", email: link_to(@user.unconfirmed_email, "mailto:#{@user.unconfirmed_email}")) %></p>
  </div>
<% end %>
<%= form_for :product do |f| %>
  <%= f.label :product_name, "Product name" %>
  <%= f.text_field :product_name, required: true %>
  <%= f.submit "Save" %>
<% end %>
# original code found at
# https://github.com/forem/forem/blob/5e93d3a25ecc800703aa100d286c8c75c9173ddf/app/views/users/edit.html.erb#L63
  
 <% if current_user.email.blank? %>
  <div class="crayons-notice crayons-notice--warning mb-6" aria-live="polite">
    <h3 class="mb-2"><%= t("views.settings.complete.heading") %></h3>
    <p>
      <%= t("views.settings.complete.desc") %>
      <% if @user.twitter_username.blank? %>
        <%= t("views.settings.complete.twitter") %>
      <% elsif @user.github_username.blank? %>
        <%= t("views.settings.complete.github") %>
      <% end %>
    </p>
  </div>
<% end %>
# Importing assets into the application layout (Asset Tags)
<%= stylesheet_link_tag "your_stylesheet" %>
<%= javascript_include_tag "your_javascript" %>
<%= image_tag "happy_cat.jpg" %>

<%# comment %> ?

<%= link_to "Sample Text", whichever_path %>

# With clas
<%= link_to "Sample Text", whichever_path, class: "my-btn" %>

# Passing params
link_to "Edit this post", edit_post_path(3) # don't hardcode 3!

# Or specify more params
post_path(3, :referral_link => "/some/path/or/something")

# Rendering partials
<%= render "user_form" %>
  # this will be from a file called _user_form.html.erb in app/views/users

# Passing variables to partials (using the locals "options hash")
<%= render partial: "shared/your_partial", :locals => { :user => user } %>
# Or
<%= render "shared/your_partial", :user => user %>
# If the partial name + the variable name are the same, use this shorthand notation
<%= render user %>
  
# To change the action of a Turbo Drive link, you can use data attributes inside of your Rails link tags
<%= link_tpo "Edit Post", edit_post_path(@post), data: { turbo_action: "replace" } %>
  # This will => <a href="..." data-turbo-action="replace">Edit Article</a>

# Change the HTTP Request Method of a Turbo link
<%= link_to "Delete Post", post_pat(@post), data: { turbo_method: "delete" } %>
  # This will => <a href="..." data-turbo-method="delete">Delete Article</a>

# Disable Turbo Drive
<div data-turbo="false"> # Disabled
  <%= link_to "foo", "bar" %> # Disabled
  <%= link_to "baz", "qux", data: { turbo: "true" } %> # Enabled
</div>
  def search
    baseurl = "https://api.themoviedb.org/3/discover/movie?api_key=#{ENV['REACT_APP_TMDB_API_KEY']}&language=en-						US&sort_by=popularity.desc&with_watch_monetization_types=flatrate&include_adult=false&include_video		=false&page=${moviePageIndex}";
    urladdon = params[:urlAddon]

    require 'uri'
    require 'net/http'

    uri = URI("#{baseurl}#{urladdon}")
    res = Net::HTTP.get_response(uri)

    if res.is_a?(Net::HTTPSuccess)
      render json: res.body
    else
      render json: res.body, status: :bad_request
    end
  end
When you want an optional belongs_to:


1) Just don't add in the field's migration
#migration_file_name.rb
#PLEASE DO NOT DO
null: false 

2) In the model use
#PLEASE DO
belongs_to :community, optional: true
namespace :javascript do
  desc "Compiles each js file"
  task validate: :environment do
    JS_PATH = "app/assets/javascripts/**/*.js"
    Dir[JS_PATH].each do |file_name|
      puts "\n#{file_name}"
      puts Uglifier.compile(File.read(file_name))
    end
  end
end
file = "#{Rails.root}/public/users.csv"
headers = ["Name", "Company Name", "Email", "Role", "Team Name"]
CSV.open(file, 'w', write_headers: true, headers: headers) do |writer|
    Team.all.each do |team|
      team.users.each do |user|
        writer << [user.name, user.company_name , user.email, user.roles&.first&.name, team.name]
      end
    end
  end
# get "/articles?page=2"
request.original_url # => "http://www.example.com/articles?page=2"
star

Thu Dec 22 2022 18:55:36 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Thu Dec 22 2022 18:45:10 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Sat Dec 17 2022 10:15:00 GMT+0000 (Coordinated Universal Time) https://github.com/forem/forem/blob/8cbbc222e9a9c31f8cb8ab5e7b1df08c2109885b/app/views/admin/shared/_destroy_confirmation_modal.html.erb#L16

#ruby #rubyonrails
star

Wed Dec 14 2022 21:08:03 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Wed Dec 14 2022 21:02:50 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Tue Jul 19 2022 21:44:39 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Fri Jun 17 2022 09:38:30 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Fri Jun 17 2022 09:36:50 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Fri Jun 17 2022 09:35:26 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Fri Jun 17 2022 09:32:50 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Fri Jun 17 2022 09:31:08 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Fri Jun 17 2022 09:28:08 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Fri Jun 17 2022 09:25:48 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Fri Jun 17 2022 09:18:45 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Fri Jun 17 2022 09:16:41 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Fri Jun 17 2022 09:14:18 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Fri Jun 17 2022 09:12:40 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Fri Jun 17 2022 09:07:51 GMT+0000 (Coordinated Universal Time) https://github.com/forem/forem/blob/5e93d3a25ecc800703aa100d286c8c75c9173ddf/app/views/users/edit.html.erb#L63

#ruby #rubyonrails
star

Tue May 10 2022 13:04:42 GMT+0000 (Coordinated Universal Time)

#rubyonrails
star

Mon May 24 2021 18:07:47 GMT+0000 (Coordinated Universal Time)

#react #api #ruby #rubyonrails
star

Thu Aug 20 2020 01:16:40 GMT+0000 (Coordinated Universal Time) https://stackoverflow.com/questions/46053658/how-to-make-a-models-column-optional-in-rails5

#rb #rubyonrails
star

Thu Aug 13 2020 18:18:29 GMT+0000 (Coordinated Universal Time) https://medium.com/forest-admin/rails-migrations-tricks-guide-code-cheatsheet-included-dca935354f22

#rubyonrails #ruby
star

Thu Aug 13 2020 18:17:17 GMT+0000 (Coordinated Universal Time) https://medium.com/forest-admin/rails-migrations-tricks-guide-code-cheatsheet-included-dca935354f22

#rubyonrails #ruby
star

Mon May 18 2020 19:08:51 GMT+0000 (Coordinated Universal Time) https://stackoverflow.com/questions/37512603/rails-attach-some-data-to-head-ok-response

#ruby #rubyonrails
star

Mon May 11 2020 16:41:09 GMT+0000 (Coordinated Universal Time)

#ruby #rubyonrails
star

Mon May 11 2020 16:38:22 GMT+0000 (Coordinated Universal Time) custom

#ruby #rubyonrails
star

Tue Dec 31 2019 19:00:00 GMT+0000 (Coordinated Universal Time) https://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-original_url

#howto #rubyonrails #webdev #interviewquestions

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension