[英]private method `main_image' called for nil:NilClass? how to fix
portfolios_controller.rb 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 show.html.erb
<%= image_tag @portfolio_item.main_image %>
<h1><%= @portfolio_item.title %></h1>
<em><%= @portfolio_item.subtitle %></em>
<p><%= @portfolio_item.body %></p>
Add show
action 添加show
动作
def show
@portfolio_item = Portfolio.find(params[:id])
end
In your portfolios_controller
在你的portfolios_controller
private method `main_image' called for nil:NilClass? 私有方法“ main_image”是否需要nil:NilClass?
You are ending the class PortfoliosController
before show
method, so it lost the scope. 您在show
方法之前结束了 PortfoliosController
类 ,因此它失去了作用域。 Put it inside the 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.