Ruby on Rails data listing gem with built-in sorting, filtering and in-place editing.
SmartListing - data listing gem for Rails with built-in sorting, filtering and in-place editing | Sology Showcase
I'm using the SmartListing gem with my Ruby (2.1.2p95) on Rails (4.0.0) project to create a sortable/paginating table from an ActiveRecord group of records, let's call them Orders. Each Order can have many Items, each of which has a Cost field on their table. I need to be able to sort by Order Number (field in Order table), number of Items (class method on Order model), and total cost (class method on Order model).
Is there a way, using explicit sort_attributes, to call the method (i.e. order.item_total or order.number_of_items) instead of using a field in the MySQL table, which doesn't exist? There are no 'item_totals' columns or 'number_of_items' columns in the 'orders' table, both of those calls are an aggregate of fields/counts in the 'items' table.
@orders = smart_listing_create(:orders, @user.orders, partial: 'orders/order', sort_attributes: [[:id, 'id'],[:item_total, '???'],[:number_of_items, '???']]
Thanks in advance for any help and let me know if I can clarify anything as well.
-Dave
Source: (StackOverflow)
I'm trying to show and create a list based on a search.
Presently my url is like /section/4/item where "4" represents the :id of the section.
I have added a hidden field to my form to pass this information on when adding a new record like
= f.hidden_field :section, :value => @section.id
Problem is, when I click new record the id from the url isn't passed into the new javascript function this attributing this to the hidden field.
Some code
section.rb controller
def index
smart_listing_create :section,
Section.find(params[:id]),partial: "section/list"
end
def new
@topic_admin = Post.new
end
index.html.haml section view
= smart_listing_render(:topic_admin_post)
_list.html.haml
%table.table.table-striped
%thead
%th= "Comment"
%th
%tbody
- unless smart_listing.empty?
- smart_listing.collection.each do |o|
%tr.editable{data: {id: o.id}}
= smart_listing.render object: o, partial: "section/section", locals: {object: o}
= smart_listing.item_new colspan: 2, link: section_path
= smart_listing.paginate
= smart_listing.pagination_per_page_links
- else
= smart_listing.item_new colspan: 2, link: new_section_path
_section.html.haml
%td= object.comment
%td.actions= smart_listing_item_actions [
{name: :edit, url: edit_section_path(object)},
{name: :destroy, url: section_path(object),
confirmation: "Are you sure you want to delete this?"}]
_form.html.haml
%td{colspan: 2}
= simple_form_for object, html: { class: 'form-horizontal' } do |f|
= f.error_notification
.form-inputs
Post:
= f.text_field :comment, required: true
= f.hidden_field :id, :value => @topic.id
.form-actions
.col-sm-offset-4
= f.button :submit, class: 'btn-primary'
%button.btn.btn-link.cancel Cancel
index/new/update.js.erb
<%# index.js.erb %>
<%= smart_listing_update(:section) %>
<%# new.js.erb %>
<%= smart_listing_item :section, :new, @section, "section/form" %>
<%# update.js.erb %>
<%= smart_listing_item :section_post, :update, @section,
@section.valid? ? "section_post/section" : "section/form" %>
route.rb
resources :section, :except => [:index]
match '/section/:id/view' => 'section#index', :as => 'section', :via => [:get, :post], :defaults => {id: 0}
Most of this has come from the Smart_Listing examples,
the changes are in the routing and index controller.
I'm happy to change more of the routes, but looking for a easier way to pass the :id via the javascript when new.js is called.
Is there a way to do this with Smart_Listing gem?
Source: (StackOverflow)