繁体   English   中英

私有方法“ main_image”是否需要nil:NilClass? 怎么修

[英]private method `main_image' called for nil:NilClass? how to fix

portfolios_controller.rb

class PortfoliosController < ApplicationController
  def index
    @portfolio_items = Portfolio.all 
  end

  def new
    @portfolio_item =Portfolio.new
  end

  def create
    @portfolio_item =Portfolio.new(params.require(:portfolio).permit(:title, :subtitle, :body))

    respond_to do |format|
      if @portfolio_item.save
        format.html { redirect_to portfolios_path, notice: 'Your Portfolio Item is now live.' }
        format.json { render :show, status: :created, location: @blog }
      else
        format.html { render :new }
        format.json { render json: @blog.errors, status: :unprocessable_entity }
      end
    end
  end

  def edit
    @portfolio_item = Portfolio.find(params[:id])
  end

  def update
    @portfolio_item = Portfolio.find(params[:id])
    respond_to do |format|
      if @portfolio_item.update(params.require(:portfolio).permit(:title, :subtitle, :body))
        format.html { redirect_to portfolios_path, notice: 'The record successfully updated.' }
      else
        format.html { render :edit }
      end
    end
  end 
end

def show
   @portfolio_item = Portfolio.find(params[:id])
end

show.html.erb

<%= image_tag @portfolio_item.main_image %>

<h1><%= @portfolio_item.title %></h1>

<em><%= @portfolio_item.subtitle %></em>

<p><%= @portfolio_item.body %></p>

添加show动作

def show
  @portfolio_item = Portfolio.find(params[:id])
end

在你的portfolios_controller

私有方法“ main_image”是否需要nil:NilClass?

您在show方法之前结束了 PortfoliosController ,因此它失去了作用域。 将其放在 PortfoliosController

def update
  @portfolio_item = Portfolio.find(params[:id])
  respond_to do |format|
    if @portfolio_item.update(params.require(:portfolio).permit(:title, :subtitle, :body))
      format.html { redirect_to portfolios_path, notice: 'The record successfully updated.' }
    else
      format.html { render :edit }
    end
  end
end 

def show
   @portfolio_item = Portfolio.find(params[:id])
end
end #class end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM