【问题标题】:NoMethodError in Categories#show类别中的 NoMethodError#show
【发布时间】:2016-08-14 06:04:02
【问题描述】:

你好,我是 ruby​​ 新手,不知道为什么 nil 被传递给我的@category

这里是控制器

class CategoriesController < ApplicationController

    def show
        @cateogry = Category.find(params[:id])
    end

    def new
    end

    def create
      @category = Category.new(category_params)

      @category.save
      redirect_to @category
    end

    private
      def category_params
        params.require(:category).permit(:name_en, :name_ar, :thumb_url, :pano_url, :isactive)
      end
end

还有show.html.erb

<p>
  <strong>Name: (english)</strong>
  <%= @category.name_en %>
</p>

我不断收到一个

类别中的 NoMethodError#show undefined method `name_en' for nil:NilClass

我已经坚持了一天半了,找不到解决方案,任何帮助将不胜感激

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    您在此处将类别拼写为实例变量

    def show
        **@cateogry** = Category.find(params[:id])
    end
    

    【讨论】:

    • 有时盯着代码看很长时间对你有影响.. 抱歉发错字了
    【解决方案2】:

    更新代码,没有错,只是打字错误

    def show
      @category = Category.find(params[:id])
    end
    
    def new
    end
    
    def create
      @category = Category.new(category_params)
    
      @category.save
      redirect_to @category
    end
    
    private
      def category_params
        params.require(:category).permit(:name_en, :name_ar, :thumb_url, :pano_url, :isactive)
      end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多