вот простой пример вложенных маршрутов с smart_listing. Я думаю, что это должно охватывать этот вопрос. я Rails 4.2
, ruby 2.2.0
, smart_listing 1.1.2
конфиг/routes.rb
resources :users do
resources :bios
end
root 'users#index'
модели/user.rb
class User < ActiveRecord::Base
has_one :bio
accepts_nested_attributes_for :bio, allow_destroy: true
scope :like, ->(args) { where("email like '%#{args}%' OR name like '%#{args}%' OR surname like '%#{args}%'")}
end
модели/bio.rb
class Bio < ActiveRecord::Base
belongs_to :user
end
Контроллеры/users_controller.rb
class UsersController < ApplicationController
include SmartListing::Helper::ControllerExtensions
helper SmartListing::Helper
before_action :set_user, only: [:update, :destroy]
def index
users_scope = User.all.includes(:bio)
users_scope = users_scope.like(params[:filter]) if params[:filter]
# @users = smart_listing_create :users, users_scope, partial: "users/list", page_sizes: [5, 7, 13, 26]
@users = smart_listing_create(:users, users_scope, partial: 'users/list',
sort_attributes: [
[:name, 'name'],
[:surname, 'surname'],
[:email, 'email'],
[:city, 'bio.city'],
[:birthday, 'bio.birthday']
],
default_sort: { start_at: 'desc' }
)
end
def new
@user = User.new
@user.build_bio
end
def create
@user = User.new(user_params)
@user.save
end
def edit
@user = User.includes(:bio).find(params[:id])
@user.bio.build if @user.bio.nil?
end
def update
@user.update(user_params)
end
def delete
end
def destroy
@user.destroy
end
private
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:name, :surname, :email, :bio_attributes => [:birthday, :city])
end
end
просмотров/пользователей/index.html.haml
= smart_listing_controls_for(:users, {class: "form-inline text-right"}) do
.form-group.filter.input-append
= text_field_tag :filter, '', class: "search form-control",
placeholder: "Search...", autocomplete: :off
= smart_listing_render :users
просмотров/пользователей/_list.html.haml
- unless smart_listing.empty?
%table.table.table-striped
%thead
%th= smart_listing.sortable "Name", :name
%th= smart_listing.sortable "Surname", :surname
%th= smart_listing.sortable "Email", :email
%th= smart_listing.sortable "City", :city
%th= smart_listing.sortable "Birthday", :birthday
%tbody
- smart_listing.collection.each do |o|
%tr.editable{data: {id: o.id}}
= smart_listing.render object: o, partial: "users/user", locals: {object: o}
= smart_listing.item_new colspan: 6, link: new_user_path
= smart_listing.paginate
= smart_listing.pagination_per_page_links
- else
%p.warning No users
просмотров/пользователей/_user.html.haml
%td= object.name
%td= object.surname
%td= object.email
%td= object.bio.city
%td= object.bio.birthday
%td.actions= smart_listing_item_actions [ {name: :edit, url: edit_user_path(object)}, {name: :destroy, url: user_path(object), confirmation: "Are you sure you want to delete this?"}]
просмотров/пользователей/_form.html.haml
%td{colspan: 6}
= form_for object, url: object.new_record? ? users_path : user_path(object),
remote: true, html: {class: "form-horizontal"} do |f|
%p
Name:
= f.text_field :name
%p
Surname:
= f.text_field :surname
%p
Email:
= f.text_field :email
= f.fields_for :bio do |ff|
%p
Birthday
= ff.date_field :birthday
%p
City
= ff.text_field :city
= f.submit "Save", class: "btn btn-primary"
%button.btn.btn-link.cancel Cancel
просмотров/пользователей/create.js.Еврорадио
<%= smart_listing_item :users, :create, @user, @user.persisted? ? "users/user" : "users/form" %>
просмотров/пользователей/edit.js.erb
<%= smart_listing_item :users, :edit, @user, "users/form" %>
просмотров/пользователей/destroy.js.erb
<%= smart_listing_item :users, :destroy, @user %>
просмотров/пользователей/index.js.erb
<%= smart_listing_update(:users) %>
просмотров/пользователей/new.js.erb
<%= smart_listing_item :users, :new, @user, "users/form" %>
просмотров/пользователей/update.js.erb
<%= smart_listing_item :users, :update, @user, @user.valid? ? "users/user" : "users/form" %>
Красивый пример :) !! – Kam
Было ли это полезно? Вы получили его для работы в своем приложении? – WRK