我正在创建一个简单的商店,我有一个单一的产品表。我希望任何访问url“website/products”的商店的人只能“显示”产品。但在url'website/admins/products'上,我想创建、编辑、销毁。
管理控制器的CRUD已满,而另一个控制器只有show。
我不确定问题是在我的路由文件中还是在我的表单中(或者其他什么东西?)
这是我的表格,两者都一样
<%= form_with( model: product, local: true) do |form| %>
<% if product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% product.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description %>
</div>
<div class="field">
<%= form.label :price %>
<%= form.text_field :price %>
</div>
<div class="field">
<%= form.label :inventory_count %>
<%= form.number_field :inventory_count %>
</div>
<div class="admin/actions">
<%= form.submit %>
</div>
这是我的路由文件
Rails.application.routes.draw do
namespace :admin do
resources :products
end
resources :products
end
控制器/产品控制器
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
def index
@products = Product.all
end
def show
end
def update
respond_to do |format|
if @product.ipdate(product_params)
format.html {redirect_to @product, notice: 'product was successfully updated.' }
else
format.json {render :edit }
format.json {render json: @product.errors, status: :unprocessable_entity }
end
end
end
private
def set_product
@product = Product.find(params[:id])
end
def product_params
params.require(:product).permit(:title, :description, :price, :inventory_count)
end
end
class Admin::ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
def index
@products = Product.all
end
def show
end
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @product.ipdate(product_params)
format.html {redirect_to @product, notice: 'product was successfully updated.' }
else
format.json {render :edit }
format.json {render json: @product.errors, status: :unprocessable_entity }
end
end
end
def destroy
@product.destroy
respond_to do |format|
format.html {redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json {head :no_content }
end
end
private
def set_product
@product = Product.find(params[:id])
end
def product_params
params.require(:product).permit(:title, :description, :price, :inventory_count)
end
end
更新:View index.html.erb
24 <p id="notice"><%= notice %></p> 23 22 <h1>Products</h1> 21 20 <table> 19 <thead> 18 <tr> 17 <th>Title</th> 16 <th>Description</th> 15 <th>Price</th> 14 <th>Inventory count</th> 13 <th colspan="3"></th> 12 </tr> 11 </thead> 10 9 <tbody> 8 <% @products.each do |product| %> 7 <tr> 6 <td><%= product.title %></td> 5 <td><%= product.description %></td> 4 <td><%= product.price %></td> 3 <td><%= product.inventory_count %></td> 2 <td><%= link_to 'Show', product %></td> 1 <td><%= link_to 'Edit', edit_admin_product_path(product) %></td> 25 <td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td> 1 </tr> 2 <% end %> 3 </tbody> 4 </table> 5 6 <br> 7 8 <%= link_to 'New Product', new_admin_product_path %>
如果您有任何想法或建议,我将非常感谢,谢谢!