![]() |
In this walk-through I'll show you how little time it takes to set up a simple application with 2 interacting grids that provide the UI to 2 associated (one-to-many) Rails models. As both grids will be powered by the Netzke::Basepack::Grid component, you may be surprised with all the features like sorting, pagination, search, multi-line editing, etc that this app will provide out of the box.
So, let's get started.
Setting things up
Create a new Rails application:
- $ rails new netzke_bookshelf && cd netzke_bookshelf
- gem 'netzke'
- $ bundle
- $ echo MOST PROBABLY NO COPY-PASTING HERE
- $ ln -s ~/code/extjs/ext-5.1.0 public/extjs
- $ mkdir public/images
- $ ln -s ~/assets/famfamfam-silk public/images/icons
- # config/routes.rb
- NetzkeTaskManager::Application.routes.draw do
- netzke
- end
Creating Rails models
We'll build a little personal bookshelf app, let's start with generating the 2 models for it:
- $ rails g model author name
- $ rails g model book author:references title exemplars:integer completed:boolean
- $ rake db:migrate
- # app/models/author.rb
- class Author < ActiveRecord::Base
- validates :name, presence: true
- has_many :books, dependent: :delete_all
- end
- # app/models/book.rb
- class Book < ActiveRecord::Base
- validates :title, presence: true
- belongs_to :author
- end
Creating the grids
Netzke components are Ruby classes, so, let's create one in the app/components/authors.rb (you'll need to add the app/components directory first):
- # app/components/authors.rb
- class Authors < Netzke::Basepack::Grid
- def configure(c)
- super
- c.model = "Author"
- end
- end
The netzke-testing gem, which is a part of the Netzke bundle, provides convenient routes for testing components in isolation, so, we'll make use of it. Fire up your Rails app:
- $ rails s
Here's what you should see:
![]() |
However, a more interesting example to play with would be our books grid, as it will have more columns of different types:
- # app/components/books.rb
- class Books < Netzke::Basepack::Grid
- def configure(c)
- super
- c.model = "Book"
- end
- end
![]() |
As you may expect, our grids are highly configurable, and many configuration options can be found in the documentation. Let us, for instance, make a few changes into the Books grid, customizing the columns and the button bar:
- class Books < Netzke::Basepack::Grid
- def configure(c)
- c.columns = [
- # you may configure columns inline like this:
- { name: :author__name, text: "Author" },
- :title,
- :exemplars,
- :completed
- ]
- c.model = "Book"
- # which buttons to show in the bottom toolbar
- c.bbar = [:add, :edit, :del, '->', :apply]
- super
- end
- # you may also use DSL to configure columns individually
- column :completed do |c|
- c.width = 100
- end
- end
![]() |
If you found this post interesting, follow and support us.
Suggest for you:
The Complete Ruby on Rails Developer Course
Rails 5: Learning Web Development the Ruby Way
Learn Ruby on Rails from Scratch
Ruby Scripting for Software Testers
Python, Ruby, Shell - Scripting for Beginner




No comments:
Post a Comment