我有两个版本的同一索引页,显示产品列表。常规视图-显示列表管理视图-显示具有编辑、删除等选项的列表。
index操作只是进行数据库查询并返回该实例变量。当前,我有相同的索引操作来呈现索引页面。我希望将索引操作表单产品重用到管理控制器中。
def index
@products = Product.all
end
路线:
/product/index => product#index
/admin/product/index => product#index
我尝试了给定的prepend_view_path技术[在这里是如何拦截rails的模板呈现,但在这两种情况下,最终总是呈现admin/product/index.html.erb文件。
我希望Product#index controller#action呈现:
/index.html.erb用于/积/索引
和
/admin/product/index.html.erb用于/admin/product/index
有人可以帮助我如何以一种优雅的方式完成这一工作,而不仅仅是为管理控制器类和产品控制器类编写相同的操作。
PS:我刚进入红宝石和红宝石铁轨的一个星期。任何帮助都是非常感谢的。
发布于 2014-11-03 07:43:26
在您的控制器中,只需呈现您想为用户服务的视图,我的意思是:
ProductsController
def index
# ...
render 'index'
end
Admin::ProductsController
def index
# ...
render 'products/index'
end
发布于 2014-11-03 07:38:55
路线上有个错误。
/积/索引=> product#index
/admin/product/index => product#index
它们都指向与产品相同的contoller名称。
我建议这些路线
resources :admin do
resources :product do
end
end
resources :product do
end
通过这样做,可以确保您有两个产品控制器。一个带有Admin::ProductController第二个ProductController
admin_product_path用于管理视图,product_path用于普通视图
发布于 2020-08-30 15:43:36
在使用Rails 6应用程序时,我遇到了这个挑战。
我有两种类型的用户:Customers和Admins。我使用设计 gem进行身份验证。我希望产品视图的Customer与Admins视图不同。
我已经在控制器中有了一个目录--为Admins设计配置。
,这是我怎么做的,
首先,使用命名空间为Admins products
视图定义一个新的products
路由admins
。
namespace :admins do
resources :products do
end
end
注意事项:这将影响管理员的路径/URL。比方说,products_path
不会是admins_products_path
。
然后,在控制器中将products_controller.rb
添加到app/controllers/admins
目录中:
class Admins::ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
# GET /products
# GET /products.json
def index
@products = Product.all
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
@product = Product.new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
@product = Product.new(product_params)
respond_to do |format|
if @product.save
format.html { redirect_to admins_product_path(@product), notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: admins_product_path(@product) }
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to admins_product_path(@product), notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: admins_product_path(@product) }
else
format.html { render :edit }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to admins_products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Only allow a list of trusted parameters through.
def product_params
params.require(:product).permit(:name, :sku, :short_description, :full_description)
end
end
注意事项:注意使用Admins模块的ProductsController名称空间以及create
、update
和destroy
操作中的修改路径
最后,在视图中,在app/views/admins/products
目录中添加与Admins产品关联的视图。
注意事项:您可能必须修改视图中的路径,以便与管理员产品的路由相对应。比如说,admins products的视图将是admins_product_path(product)
,而不是product
或product_path(product)
。
有一种使用单元格 gem的更干净的方法。这消除了重复代码的需要,而且当您需要为最多3个或更多角色定义视图时,它也会派上用场。您可以在这里阅读更多关于如何使用它的内容:Rails中的面向对象视图。
就这样。
我希望这对有帮助
https://stackoverflow.com/questions/26709285
复制相似问题