Generate ruby on rails index page with turbo frame. I should see yabel with sorting and…
- User
Generate ruby on rails index page with turbo frame. I should see yabel with sorting and indexing
- Assistant
Here's the implementation for a Ruby on Rails index page with Turbo Frame, sorting, and indexing:
<!-- app/views/items/index.html.erb --> <h1>Items</h1> <%= turbo_frame_tag "items_table" do %> <table class="table"> <thead> <tr> <th>#</th> <th> <%= link_to "Name", items_path(sort: 'name', direction: get_direction('name')) %> <%= sort_indicator('name') %> </th> <th> <%= link_to "Description", items_path(sort: 'description', direction: get_direction('description')) %> <%= sort_indicator('description') %> </th> <th> <%= link_to "Price", items_path(sort: 'price', direction: get_direction('price')) %> <%= sort_indicator('price') %> </th> <th> <%= link_to "Created At", items_path(sort: 'created_at', direction: get_direction('created_at')) %> <%= sort_indicator('created_at') %> </th> </tr> </thead> <tbody> <% @items.each_with_index do |item, index| %> <tr> <td><%= index + 1 %></td> <td><%= item.name %></td> <td><%= item.description %></td> <td><%= item.price %></td> <td><%= item.created_at %></td> </tr> <% end %> </tbody> </table> <% end %> <style> .table { width: 100%; border-collapse: collapse; } .table th, .table td { border: 1px solid #ddd; padding: 8px; text-align: left; } .table th { background-color: #f2f2f2; } .table tr:nth-child(even) { background-color: #f9f9f9; } </style># app/helpers/items_helper.rb module ItemsHelper def get_direction(column) if params[:sort] == column params[:direction] == 'asc' ? 'desc' : 'asc' else 'asc' end end def sort_indicator(column) if params[:sort] == column if params[:direction] == 'asc' '↑' else '↓' end else '' end end end# app/controllers/items_controller.rb class ItemsController < ApplicationController SORTABLE_COLUMNS = %w[name description price created_at].freeze def index @items = Item.all if params[:sort].present? && SORTABLE_COLUMNS.include?(params[:sort]) direction = params[:direction] == 'desc' ? 'desc' : 'asc' @items = @items.order(params[:sort] => direction) end end endThis implementation includes a table with sorting capabilities on the columns "Name," "Description," "Price," and "Created At." The table is wrapped in a Turbo Frame named "items_table," which allows for dynamic updates. The index column displays the row numbers. The sorting direction is indicated by arrows (↑ for ascending, ↓ for descending). The styling is included directly in the view for simplicity, but you might want to move it to a separate CSS file in a real application.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.