ruby - Rails Beginner NoMethodError in PinsController#new -
having issues keep getting error undefined method `pins' nil:nilclass, can explain how fix error. been following tutorial , 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 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 , :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 devise_for :users resources :pins root "pins#index" end
if using gem devise
helper method current_user
returns current logged in user in session.
pleases make sure user logged in/
you can make sure user logged in adding before_action :authenitcate_user!
in application controller(for actions in application).
Comments
Post a Comment