dimanche 24 avril 2016

Rails Beginner NoMethodError in PinsController#new

having issues keep getting error undefined method `pins' for nil:NilClass, can someone explain how to fix this error. Been following a tutorial and recently got stuck.


pins_controller.rb

class PinsController < ApplicationController
    before_action :find_pin, only: [:show, :edit, :update, :destroy]


    def index
        @pins = Pin.all.order("created_at DESC")
    end

    def new
        @pin = current_user.pins.build
    end


    def create
        @pin = current_user.pins.build(pin_params)

        if @pin.save
            redirect_to @pin, notice: "Successfully created new Pin"
        else
            render 'new'
        end
    end

    def edit

    end

    def update
        if @pin.update(pin_params)
            redirect_to @pin, notice: "Pin was Successfully updated!"
        else
            render 'edit'
        end
    end


    def destroy
        @pin.destroy
        redirect_to root_path
    end


    private

    def pin_params
        params.require(:pin).permit(:title, :description)
    end

    def find_pin
        @pin = Pin.find(params[:id])
    end
end


user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
    has_many :pins     
end


pin.rb

class Pin < ActiveRecord::Base
    belongs_to :user
end


routes.rb

Rails.application.routes.draw do
  devise_for :users
resources :pins

root "pins#index"
end

Aucun commentaire:

Enregistrer un commentaire