我有两种型号:公寓和房间。公寓有很多房间,房间属于公寓。我使用smart_listing gem作为ajax表单。我在edit_apartment_path
中显示我的表= render 'rooms/index' # index is partial
我将它添加到我的apartment_controller
def edit
@rooms = smart_listing_create :rooms,
Room.where(apartment_id: params[:apartment_id]),
partial: "rooms/list"
end
现在我必须为表单设置路径
= simple_form_for object, url: object.new_record? ? apartment_rooms_path : apartment_room_path(id: object),
remote: true, html: {class: "form-horizontal"} do |f|
= f.input :title
= f.button :submit
我可以编辑我创建的房间,但我无法在公寓内创建新的房间。我的错误:
ActionController::UrlGenerationError - No route matches {:action=>"edit", :apartment_id=>nil, :controller=>"rooms", :id=>#<Room id: 83, title: "dawawd">, created_at: "2016-02-11 10:36:30", updated_at: "2016-02-11 10:36:30", apartment_id: 4>} missing required keys: [:apartment_id]:
我的路线
resources :apartments do
resources :rooms
end
可能smart_listing不支持嵌套路由。有人有想法吗? :)
答案 0 :(得分:3)
这里是使用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.erb 强>
<%= 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" %>
答案 1 :(得分:0)
您也应该收到@apartment
表单,在这种情况下,如果您的@room.persisted?
表单请求编辑,否则创建:
= simple_form_for [@apartment, @room], remote: true, html: {class: "form-horizontal"} do |f|
= f.input :title
= f.button :submit