Визуализация макетов от другой модели
В настоящее время я использую Spree в нашем продукте
Я создаю собственный контроллер, то есть "custom_models_names" в нашем проекте
и мое требование состоит в том, чтобы сделать ту же страницу, что и Products/index.html.erb
в custom_models_names/index.html.erb
поэтому для реализации этого подхода мне нужно скопировать действие "index and collection" из контроллера Products в нашем контроллере, чтобы мой контроллер выглядел так, и я скопировал все представление products/index.html.erb в нашем представлении
чем выглядит мой контроллер и вид
Контроллер / Шпрее / администратор / custom_models_names
class Spree::Admin::CustomModelNamesController < Spree::Admin::BaseController
def index
session[:return_to] = request.url
respond_with(@collection)
end
def collection
return @collection if @collection.present?
params[:q] ||= {}
params[:q][:deleted_at_null] ||= '1'
params[:q][:not_discontinued] ||= '1'
params[:q][:s] ||= 'name asc'
@collection = super
# Don't delete params[:q][:deleted_at_null] here because it is used in view to check the
# checkbox for 'q[deleted_at_null]'. This also messed with pagination when deleted_at_null is checked.
if params[:q][:deleted_at_null] == '0'
@collection = @collection.with_deleted
end
# @search needs to be defined as this is passed to search_form_for
# Temporarily remove params[:q][:deleted_at_null] from params[:q] to ransack products.
# This is to include all products and not just deleted products.
@search = @collection.ransack(params[:q].reject { |k, _v| k.to_s == 'deleted_at_null' })
@collection = @search.result.
distinct_by_product_ids(params[:q][:s]).
includes(product_includes).
page(params[:page]).
per(params[:per_page] || Spree::Config[:admin_products_per_page])
if try_spree_current_user.try(:has_spree_role?, 'admin')
else
@collection = @collection.where(created_by: spree_current_user.id)
end
@collection
end
end
и для просмотраview /spree/admin/custom_models_names/index.html.erb
<% content_for :page_title do %>
<%= plural_resource_name(Spree::Product) %>
<% end %>
<% content_for :page_actions do %>
<% end if can?(:create, Spree::Product) %>
<% content_for :table_filter do %>
<div data-hook="admin_products_sidebar">
<%= search_form_for [:admin, @search] do |f| %>
<%- locals = {f: f} %>
<div data-hook="admin_products_index_search" class="row">
<div class="col-md-6">
<div class="form-group">
<%= f.label :name_cont, Spree.t(:name) %>
<%= f.text_field :name_cont, size: 15, class: "form-control js-quick-search-target" %>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<%= f.label :variants_including_master_sku_cont, Spree.t(:sku) %>
<%= f.text_field :variants_including_master_sku_cont, size: 15, class: "form-control" %>
</div>
</div>
<% if current_spree_user.respond_to?(:has_spree_role?) && current_spree_user.has_spree_role?(:admin) %>
<div class="col-md-6">
<div class="form-group">
<%= f.label :vendor_name_cont, Spree.t(:vendor_name) %>
<%= f.text_field :vendor_name_cont, class: "form-control" %>
</div>
</div>
<% end %>
<div class="col-md-12">
<div class="field checkbox">
<label>
<%= f.check_box :deleted_at_null, { checked: params[:q][:deleted_at_null] == '0' }, '0', '1' %>
<%= Spree.t(:show_deleted) %>
</label>
</div>
<div class="field checkbox">
<label>
<%= f.check_box :not_discontinued, { checked: params[:q][:not_discontinued] == '0' }, '0', '1' %>
<%= Spree.t(:show_discontinued ) %>
</label>
</div>
</div>
</div>
<div data-hook="admin_products_index_search_buttons" class="form-actions">
<%= button Spree.t(:search), 'search' %>
</div>
<% end %>
</div>
<% end %>
<%= render partial: 'spree/admin/shared/index_table_options', locals: { collection: @collection } %>
<% if @collection.any? %>
<table class="table" id="listing_products">
<thead>
<tr data-hook="admin_products_index_headers">
<th><%= Spree.t(:sku) %></th>
<th><%= Spree.t(:status) %></th>
<th colspan="2"><%= sort_link @search,:name, Spree.t(:name), { default_order: "desc" }, { title: 'admin_products_listing_name_title' } %></th>
<th class="text-center">
<%= sort_link @search, :master_default_price_amount, Spree.t(:master_price), {}, { title: 'admin_products_listing_price_title' } %>
</th>
<% if current_spree_user.respond_to?(:has_spree_role?) && current_spree_user.has_spree_role?(:admin) %>
<th><%= Spree.t(:vendor_name) %>
<% end %>
<th data-hook="admin_products_index_header_actions" class="actions"></th>
</tr>
</thead>
<tbody>
<% @collection.each do |product| %>
<tr <%== "style='color: red;'" if product.deleted? %> id="<%= spree_dom_id product %>" data-hook="admin_products_index_rows" class="<%= cycle('odd', 'even') %>">
<td class="sku"><%= product.sku rescue '' %></td>
<td class="status"><%= available_status(product) %> </td>
<td class="image"><%= mini_image product %></td>
<td><%= link_to product.try(:name), edit_admin_product_path(product) %></td>
<td class="text-center"><%= product.display_price.to_html rescue '' %></td>
<% if current_spree_user.respond_to?(:has_spree_role?) && current_spree_user.has_spree_role?(:admin) %>
<td><%= product.vendor.try(:name) %>
<% end %>
<td class="actions actions-3 text-right" data-hook="admin_products_index_row_actions">
<%= link_to_edit product, no_text: true, class: 'edit' if can?(:edit, product) && !product.deleted? %>
<%= link_to_clone product, no_text: true, class: 'clone' if can?(:clone, product) %>
<%= link_to_delete product, no_text: true if can?(:delete, product) && !product.deleted? %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<div class="alert alert-info no-objects-found">
<%= Spree.t(:no_resource_found, resource: plural_resource_name(Spree::Product)) %>,
<%= link_to Spree.t(:add_one), new_object_url %>!
</div>
<% end %>
<%= render partial: 'spree/admin/shared/index_table_options', locals: { collection: @collection } %>
После этого, когда я нажал localhost:3000/admin /cutom_model_names в браузере, я получил эту ошибку, введите описание изображения здесь
я получаю некоторые ответы из сети, но не могу преодолеть эту ошибку
Если у вас есть идея, мы можем сделать это другим способом, я жду вашего ответа.