【问题标题】:Same name controllers, one for admins, one for users. the admin keeps routing to the user. How to fix?同名控制器,一个用于管理员,一个用于用户。管理员不断路由到用户。怎么修?
【发布时间】:2019-01-12 04:58:06
【问题描述】:

我正在创建一个简单的商店,我有一个产品表。我希望通过 url 'website/products' 访问商店的任何人都只能“显示”产品。但在 url 'website/admins/products' 上,我想创建、编辑、销毁。

我有两个同名“product_controller”的控制器。一个在“控制器/管理员”中,另一个只是在“控制器”中。 管理控制器有完整的 CRUD,而另一个控制器只有显示。

我的问题是我总是被路由到同一个控制器(“网站/管理员/产品”将我发送到“控制器/产品控制器”)

我不确定问题出在我的路由文件还是我的表单(或其他什么?)

这是我的表格,两者都是一样的

    <%= 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  

添加:

控制器/products_controller

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

controllers/admin/products_controller

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

更新:查看 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 %>

如果您有任何想法或建议,我将不胜感激,谢谢!

【问题讨论】:

  • 你能分享控制器的代码吗?

标签: ruby-on-rails model-view-controller controller admin store


【解决方案1】:

路线不是问题,如果您使用该代码运行 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

【讨论】:

  • 我认为这绝对是朝着正确方向迈出的一步,但是当我尝试添加新产品时会弹出此错误 No route matches {:action=&gt;"show", :controller=&gt;"admin/products", :id=&gt;nil}, missing required keys: [:id] 新产品还不需要 ID,因为它还没有没有创建?
猜你喜欢
  • 2017-06-28
  • 2010-11-22
  • 1970-01-01
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 2013-12-10
  • 1970-01-01
  • 2016-07-23
相关资源
最近更新 更多