I'm a software developer with 1.5 years of experience in Ruby on Rails. My question is: how can I become a master in Ruby on Rails? I need to learn advanced features like Hotwire, Elasticsearch, Datatables, and many more. Any guidance or resources would be greatly appreciated!
I have tried many resources to learn but found that they all teach things differently, especially for Elasticsearch and Datatables. In my current company, they have a specific way of implementing these features, which I haven't been able to find in external resources. Here’s an example of how we set up Datatables with "import command."
Below is the index method in Admins::TrustBudgetsController:
ruby
class Admins::TrustBudgetsController < Admins::BaseController before_action :set_trust_budget, only: [:show, :approve, :deny, :approve_request] def index @trust_budgets = UserTrustBadge.all respond_to do |format| format.html format.json { render json: TrustBudgetsDatatable.new(view_context, recordset: @trust_budgets) } end endend
Here is the Datatable class in app/datatable/user_budgets_datatable.rb:
class TrustBudgetsDatatable include Pagy::Backend include ApplicationHelper delegate :content_tag, :params, :link_to, :admins_trust_budget_path, to: :@view def initialize(view, options = {}) @view = view @options = options end def as_json(options = {}) { sEcho: params[:sEcho].to_i, iTotalRecords: @options[:recordset].count, iTotalDisplayRecords: total_count, aaData: data } end private def data trust_budgets.map do |p| ["#{p.user.user_name} request for #{p.user.user_trust_badge.category_name} badge", content_tag(:span, p.permission_status, class: status_color(p.permission_status)), content_tag(:div) do content = '' content += "<span class='text-uppercase p-1'>" content += link_to "View", admins_trust_budget_path(p.user.user_trust_badge.id), class: "text-red-gradient", data: { turbo_frame: "modal_content" } content += '</span>' content.html_safe end ] end end def trust_budgets @trust_budgets ||= fetch_trust_budgets end def fetch_trust_budgets trust_budgets = @options[:recordset] if params[:sSearch].present? trust_budgets = trust_budgets.joins(:user, :post) .where("users.user_name ILIKE :search OR CAST(posts.number AS TEXT) ILIKE :search OR CAST(total_amount AS TEXT) ILIKE :search OR CAST(permission_status AS TEXT) ILIKE :search", search: "%#{params[:sSearch]}%") end if params[:iDisplayLength] == '-1' trust_budgets = trust_budgets.reorder("#{sort_column} #{sort_direction}") else @pagy, trust_budgets = pagy(trust_budgets.reorder("#{sort_column} #{sort_direction}"), items: per_page, page: page) end trust_budgets end def total_count trust_budgets.count @pagy ? @pagy.count : trust_budgets.count end def page params[:iDisplayStart].to_i / per_page + 1 end def per_page params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10 end def sort_column columns = %w[id permission_status] sorting_column_index = params[:iSortCol_0].to_i columns[sorting_column_index] end def sort_direction params[:sSortDir_0] == "asc" ? "asc" : "desc" end def status_color(status) case status when 'Pending''text-warning' when 'Approved''text-success' when 'Denied''text-danger' else'text-secondary' end endend
index.html.erb
"> Search in Trust Badges requests datatable#customSearch"> Search in Trust Badges requests North America Last 7 Days All datatable#destroyBeforeRender"> Request Status Action