PinsController中的NoMethodError #index,#class的未定义方法`to_a':0x0000010150ec98> </class:0x0000010150ec98>

时间:2014-01-17 21:11:07

标签: ruby-on-rails ruby

我在一个月的课程中在我的pins_controller中收到此错误,并且不知道如何解决它。这是我的控制器。


class PinsController < ApplicationController
  before_action :set_pin, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index]

def index
  @pins = Pin.to_a
end

def show
end

def new
 @pin = current_user.pins.build
end

def edit
end

def create
  @pin = current_user.pins.build(pin_params)
if @pin.save
  redirect_to @pin, notice: 'Pin was successfully created.'
else
  render action: 'new'
end

def更新     if @ pin.update(pin_params)       redirect_to @pin,注意:'Pin已成功更新。'     其他       渲染动作:'编辑'     结束   端

def def     @ pin.destroy     redirect_to pins_url   端

私人     #使用回调来共享操作之间的常见设置或约束。     def set_pin       @pin = Pin.find(params [:id])     端

def correct_user
  @pin = current_user.pins.find_by(id: params[:id])
  redirect_to pins_path, notice: "Not authorized to edit this pin" if @pin.nil?
end

# Never trust parameters from the scary internet, only allow the white list through.
def pin_params
  params.require(:pin).permit(:description, :image)
end

1 个答案:

答案 0 :(得分:2)

你应该

Pin.all

而不是

Pin.to_a

在您的控制器中。