undefined method 'user' for nil:NilClass
Pokaż cały kontroler i cały widok w którym masz ten problem.
Controller:
class PinsController < ApplicationController
before_action :find_pin, only: [:show, :edit, :update, :destroy, :upvote]
before_action :authenticate_user!, except: [:index, :show]
before_action :pin_owner, only: [:edit, :update, :destroy]
def index
@pins = Pin.all.order("created_at DESC")
@users = User.all
end
def show
end
def new
#@pin = Pin.new
@pin = current_user.pins.build
end
def create
#@pin = Pin.new(pin_params)
@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
Aby zablokować edycję i usuwanie pinów innym użytkownikom niż autor
def pin_owner
unless @pin.user_id == current_user.id
flash[:notice] = 'Access denied as you are not owner of this Pin'
redirect_to pins_path
end
end
def userpin
@pins = current_user.pins
@users = User.all
@user = User.find(params[:user])
@pin = @user.pins
end
def mypins
@pins = current_user.pins
@user = current_user
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
def upvote
@pin.upvote_by current_user
redirect_to :back
end
private
def pin_params
params.require(:pin).permit(:title, :description, :image)
end
def find_pin
@pin = Pin.find(params[:id])
end
end
index.html.haml
#pins.transitions-enabled - @pins.each do |pin| .box.panel.panel-default = link_to (image_tag pin.image.url), pin .panel-body %h2= link_to pin.title, pin %p.user Author: - @users.each do |user| = link_to pin.user.email, user_pins_path(user: user)
Na moje oko to nadal problem ze wcięciami.
Ps.:
Poprawnie sformatowany
text na forum
można wkleić wpisując
``` <- (to są odwrócone pojedyńcze cudzysłowy, masz je na klawisusz z tyldą, tym pod ESC,
linijkę przed kodem z wcięciami
i linijkę zaraz po kodzie z wcięciami.
;)
A tego z wcięciami to nie wiedziałem - dzięki za zwrócenie uwagi
Controller:
class PinsController < ApplicationController
before_action :find_pin, only: [:show, :edit, :update, :destroy, :upvote]
before_action :authenticate_user!, except: [:index, :show]
before_action :pin_owner, only: [:edit, :update, :destroy]
def index
@pins = Pin.all.order("created_at DESC")
@users = User.all
end
def show
end
def new
#@pin = Pin.new
@pin = current_user.pins.build
end
def create
#@pin = Pin.new(pin_params)
@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
# Aby zablokować edycję i usuwanie pinów innym użytkownikom niż autor
def pin_owner
unless @pin.user_id == current_user.id
flash[:notice] = 'Access denied as you are not owner of this Pin'
redirect_to pins_path
end
end
def userpin
@pins = current_user.pins
@users = User.all
@user = User.find(params[:user])
@pin = @user.pins
end
def mypins
@pins = current_user.pins
@user = current_user
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
def upvote
@pin.upvote_by current_user
redirect_to :back
end
private
def pin_params
params.require(:pin).permit(:title, :description, :image)
end
def find_pin
@pin = Pin.find(params[:id])
end
end
index.html.haml
#pins.transitions-enabled
- @pins.each do |pin|
.box.panel.panel-default
= link_to (image_tag pin.image.url), pin
.panel-body
%h2= link_to pin.title, pin
%p.user
Author:
- @users.each do |user|
= link_to pin.user.email, user_pins_path(user: user)
Nie wiem dlaczego tam nie może znaleźć tego usera z pętli. Nie mniej jednak wydaje mi się że wiem co chcesz osiągnąć i tutaj to User.all a potem iterowanie po tym nie ma w tym wypadku najmniejszego sensu.
Dla każdego pin’a chcesz wyświetlić link do wszystkich pinów autora tego konkretnego pinu. Więc, na moje oko, to powinno tak wyglądać.
#pins.transitions-enabled
- @pins.each do |pin|
.box.panel.panel-default
= link_to (image_tag pin.image.url), pin
.panel-body
%h2= link_to pin.title, pin
%p.user
Author:
= link_to pin.user.email, user_pins_path(user: pin.user)
Wydaje mi się, że jest dosyć jasne co i dlaczego zmieniłem, ale w razie czego pytaj
Ps. Nie chce się tutaj narzucać, ale ogólne przyjęło się robić wcięcia w rubym nie tabulatorami, ale dwoma spacjami, w HAML’u tak samo. Dobrze od początku używać tych spacji, bo potem może być problem się przerzucić i dostosować do teamu z którym pracujesz.
Świetnie! Teraz działa tak jak powinno
Tutaj właściwy kod z wcięciami:
#pins.transitions-enabled
- @pins.each do |pin|
.box.panel.panel-default
= link_to (image_tag pin.image.url), pin
.panel-body
%h2= link_to pin.title, pin
%p.user
Author:
= link_to pin.user.email, user_pins_path(user: pin.user)
Odnośnie rady z tabulatorami - każda wskazówka jest bardzo cenna, dzięki!.
W RoR doświadczenie zbieram póki co w domowym zaciszu, dlatego od kolejnego projektu będę stosował dwie spacje jako wcięcie w kodzie.
Dzięki serdeczne za cierpliwość i pomoc przy rozwiązaniu problemu