ruby-on-rails 产品编号索引中的NoMethodError

p5fdfcr1  于 2023-03-04  发布在  Ruby
关注(0)|答案(2)|浏览(99)

对于products_path/index中的**〈%= link_to www.example.com,category_path(product. category)%〉这一行,我收到NoMethodError。我不确定是什么问题,因此请求帮助。感谢您的帮助!product.category.name, category_path(product.category) %>which are inproducts_path/index. I'm not sure what's wrong, so asking for help. Thank for any help!
我的代码

    • 产品_控制器. rb**
class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @products = Product.all
  end

  # GET /products/1
  def show
    @comments = @product.comments.order("created_at DESC").paginate(:page => params[:page], :per_page => 5)
  end

  # GET /products/new
  def new
    @product = Product.new
    @categories = Category.all.map{|c| [ c.name, c.id ] }
  end

  # GET /products/1/edit
  def edit
    @categories = Category.all.map{|c| [ c.name, c.id ] }
  end

  # POST /products
  def create
    @product = Product.new(product_params)
    @product.category_id = params[:category_id] 

    respond_to do |format|
      if @product.save
        format.html { redirect_to :back, 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

  # PATCH/PUT /products/1
  def update
    @product.category_id = params[:category_id]
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { render :show, status: :ok, location: @product }
      else
        format.html { render :edit }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  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
    # Use callbacks to share common setup or constraints between actions.
    def set_product
      @product = Product.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def product_params
      params.require(:product).permit(:name, :description, :image_url, :color, :price, :year_of_manufacture)
    end
  end
    • 类别_控制器. rb**
class CategoriesController < ApplicationController
  before_action :set_category, only: [:show, :edit, :update, :destroy]

  # GET /categories
  def index
    @categories = Category.all
  end

  # GET /categories/1
  def show
  end

  # GET /categories/new
  def new
    @category = Category.new
  end

  # GET /categories/1/edit
  def edit
  end

  # POST /categories
  def create
    @category = Category.new(category_params)

    respond_to do |format|
      if @category.save
        format.html { redirect_to @category, notice: 'Category was successfully created.' }
        format.json { render :show, status: :created, location: @category }
      else
        format.html { render :new }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /categories/1
  def update
    respond_to do |format|
      if @category.update(category_params)
        format.html { redirect_to @category, notice: 'Category was successfully updated.' }
        format.json { render :show, status: :ok, location: @category }
      else
        format.html { render :edit }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /categories/1
  def destroy
    @category.destroy
    respond_to do |format|
      format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_category
      @category = Category.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def category_params
      params.require(:category).permit(:name, :desc)
    end
end
    • 铁路路线**
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
    category_products GET    /categories/:category_id/products(.:format)          products#index
                      POST   /categories/:category_id/products(.:format)          products#create
 new_category_product GET    /categories/:category_id/products/new(.:format)      products#new
edit_category_product GET    /categories/:category_id/products/:id/edit(.:format) products#edit
     category_product GET    /categories/:category_id/products/:id(.:format)      products#show
                      PATCH  /categories/:category_id/products/:id(.:format)      products#update
                      PUT    /categories/:category_id/products/:id(.:format)      products#update
                      DELETE /categories/:category_id/products/:id(.:format)      products#destroy
           categories GET    /categories(.:format)                                categories#index
                      POST   /categories(.:format)                                categories#create
         new_category GET    /categories/new(.:format)                            categories#new
        edit_category GET    /categories/:id/edit(.:format)                       categories#edit
             category GET    /categories/:id(.:format)                            categories#show
                      PATCH  /categories/:id(.:format)                            categories#update
                      PUT    /categories/:id(.:format)                            categories#update
                      DELETE /categories/:id(.:format)                            categories#destroy
    • 已解决**

我不知道什么是错的,但删除了我的代码,这是下面重写,现在我没有得到NoMethodError。

  • _产品. html. erb(索引. html. erb)*
<% cache product do %>
<div class="col-lg-3 col-sm-4 col-md-4 col-xs-12 products">

  <div class="product-block">
    <div class="product-img-list">
      <h3 id="product-name"><%= product.name %></h3>
      <%= link_to product_path(product), class: 'product_link' do %>
      <span class="product-img-text"><span><h4><i class="glyphicon glyphicon-usd"></i><%= product.price %></h4></span></span>
      <%= image_tag(product.image_url, class: "img-responsive") %>
      <% end %>
    </div>
    <p id="product-description"><%= truncate product.description, length: 100 %></p>
    <%= link_to Category.find(product.category_id).name, category_path(product.category_id) %>
  </div>

  <% if signed_in? && current_user.admin? %>
    <ul>
      <li><%= link_to 'Show', product %></li>
      <li><%= link_to 'Edit', edit_product_path(product) %></li>
      <li><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></li>
    </ul>
  <% end %>

</div><!-- /col -->
<% end %>
ijnw1ujt

ijnw1ujt1#

如果错误显示
未定义# Product的方法“category”
这是因为您没有正确定义关联。请包括

belongs_to :category

product.rb模型中的行。
如果错误描述显示
未定义nil的方法'name':NilClass
那么您需要检查是否存在categorynilproducts

bmvo0sr5

bmvo0sr52#

我也犯了同样的错误。
我以为这是路由问题,但实际上是因为视图中的代码引用了一个不存在的属性:

<%= @product.description %>

但它应该是<%= @product.long_description %>
因此,只需仔细检查视图中的代码(或者使用binding.pry来调试它-我就是这么做的)

相关问题