You need to set the value of instance variable @link
as
@link = Link.find(params[:id])
in show action because in show
View of link
, you would be displaying details of a particular Link object(@link) like
<%= @link.title%>
In order to do this the @link variable should be set as a Link
object else you get an error.
As you already have a set_link
method defined in the controller, you can reuse it across various actions using before_action
.
class LinksController < ApplicationController
before_action :set_link, only: [:show, :edit, :update, :destroy]
....
....
end
and remove the calls to @link = Link.find(params[:id])
from show, edit, update and destroy
actions( if any).
Setting up before_action
with the only
option would result in set_link
method being called before the listed action is executed.
For eg: If you visit show url for a link then, firstly set_link would be called and after that show action(as show action is in the :only list)
Notice that the same thing (before_action :set_high_score
) is setup for HighScoresController
which is why redundant calls to @highscore = HighScore.find(params[:id]
are avoided.