Ruby on rails Rails-来自同一操作(索引)的多个视图(管理视图、常规视图)
我有两个版本的同一个索引页,其中显示了一个产品列表。 常规视图-显示列表 管理视图-显示包含编辑、删除等选项的列表 index操作只是进行数据库查询并返回该实例变量。 目前,我有相同的索引操作来呈现索引页面。我想将索引操作表单产品重用到管理控制器中Ruby on rails Rails-来自同一操作(索引)的多个视图(管理视图、常规视图),ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我有两个版本的同一个索引页,其中显示了一个产品列表。 常规视图-显示列表 管理视图-显示包含编辑、删除等选项的列表 index操作只是进行数据库查询并返回该实例变量。 目前,我有相同的索引操作来呈现索引页面。我想将索引操作表单产品重用到管理控制器中 def index @products = Product.all end 路线: /product/index => product#index /admin/product/index => product#index
def index
@products = Product.all
end
路线:
/product/index => product#index
/admin/product/index => product#index
我尝试了[here |中给出的prepend_view_path技术,但在这两种情况下,最终总是呈现admin/product/index.html.erb文件
我希望产品#索引控制器#操作呈现:
/index.html.erb for/product/index
及
/admin/product/index.html.erb for/admin/product/index
有人可以帮助我如何以优雅的方式完成这项工作,而不仅仅是为admin controller类和product controller类编写相同的操作
附言:我刚接触ruby和RubyonRails一周。非常感谢您的帮助。路由有错误 /产品/指数=>产品#指数 /管理员/产品/索引=>产品#索引 两者都指向与产品相同的控制器名称 我建议这些路线
resources :admin do
resources :product do
end
end
resources :product do
end
通过这样做,您可以确保您有两个产品控制器。
一个是Admin::ProductController
第二产品控制器
管理视图的管理\产品\路径
正常视图的产品路径从控制器中,只需渲染您想要为用户服务的视图,我的意思是:
ProductsController
def index
# ...
render 'index'
end
Admin::ProductsController
def index
# ...
render 'products/index'
end
在使用Rails 6应用程序时,我遇到了这个挑战 我有两种类型的用户:客户和管理员。我使用设计宝石进行身份验证。我希望客户的产品视图与管理员视图不同 我已经在控制器中为管理员设计配置了一个
app/controllers/admins
目录
我是这样做的:
首先,使用Admins
名称空间为Adminsproducts
视图定义一个新的路由
namespace :admins do
resources :products do
end
end
注意:这将影响管理员的路径/URL。例如,产品路径
将不是管理员产品路径
然后,在控制器中将products\u 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
类管理员::ProductsController
注意:注意使用Admins模块的ProductsController名称空间以及创建
、更新
和销毁
操作中的修改路径
最后,在视图中,将与管理员产品相关联的视图添加到app/views/Admins/Products
目录中
注意:您可能必须修改视图中的路径,以与管理员产品的路径相对应。例如,管理员产品的显示
视图将是管理员产品路径(产品)
,而不是产品
或产品路径(产品)
使用gem有一种更干净的方法。这消除了重复代码的需要,并且当您需要为最多3个或更多角色定义视图时,它非常方便。您可以在此处阅读有关如何使用它的更多信息:
就这些
我希望这有帮助是的,但是如果我创建两个单独的控制器操作,那么您添加的#…部分将需要在两个操作中复制。那么请说明控制器层次结构。
ProductsController
是Admin::ProductsController
?ProductsController和AdminController的祖先或后代不同,但我想在AdminControl中使用ProductsController的索引操作