代码之家  ›  专栏  ›  技术社区  ›  Daniel Visca

同名控制器,一个用于管理员,一个用于用户。管理员会一直路由到用户。如何修复?

  •  1
  • Daniel Visca  · 技术社区  · 6 年前

    我正在创建一个简单的商店,我有一个单一的产品表。我希望任何访问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 %>

    如果您有任何想法或建议,我将非常感谢,谢谢!

    1 回复  |  直到 6 年前
        1
  •  1
  •   Javier Menéndez Rizo    6 年前

    路由不是问题所在,如果使用该代码运行rails路由,您可以看到如下内容:

        admin_products GET    /admin/products(.:format)          admin/products#index
                       POST   /admin/products(.:format)          admin/products#create
     new_admin_product GET    /admin/products/new(.:format)      admin/products#new
    edit_admin_product GET    /admin/products/:id/edit(.:format) admin/products#edit
         admin_product GET    /admin/products/:id(.:format)      admin/products#show
                       PATCH  /admin/products/:id(.:format)      admin/products#update
                       PUT    /admin/products/:id(.:format)      admin/products#update
                       DELETE /admin/products/:id(.:format)      admin/products#destroy
              products GET    /products(.:format)                products#index
                       POST   /products(.:format)                products#create
           new_product GET    /products/new(.:format)            products#new
          edit_product GET    /products/:id/edit(.:format)       products#edit
               product GET    /products/:id(.:format)            products#show
                       PATCH  /products/:id(.:format)            products#update
                       PUT    /products/:id(.:format)            products#update
                       DELETE /products/:id(.:format)            products#destroy
    

    如果要使用不同的控制器,则需要两个表单,分别生成到这些控制器的路由

    <%= form_with scope: :product, url: product_path(@product), method: :patch do |form| %>
      ...
    <% end %>
    

    对于管理员

    <%= form_with scope: :product, url: admin_product_path(@product), method: :patch do |form| %>
      ...
    <% end %>
    

    更新

    <%= form_with scope: :product, url: products_path do |form| %>
      ...
    <% end %>
    
    <%= form_with scope: :product, url: admin_products_path do |form| %>
      ...
    <% end %>
    

    Api参考 https://api.rubyonrails.org/v5.1/classes/ActionView/Helpers/FormHelper.html#method-i-form_with