For some reason, I keep getting this error.
Couldn’t find Album without an ID
But that doesn’t make sense because in my debug.params it says:
{“utf8″=>”✓”,
“authenticity_token”=>”os9DVVZS6//bs3Ne2Xfrh4VnKXNtDXkZaE4s/3iQagE=”,
“video”=>{“url”=>”www.youtube.com/watch?v=yIRuri1AB0I”,
“album_id”=>”1”},
“commit”=>”Next”}
Here is the controller:
class VideosController < ApplicationController
include AlbumsHelper
before_filter :signed_in_user, only: [:create, :destroy] #add update later
before_filter :correct_user, only: :destroy
def show
@video = Video.find(params[:id])
end
def new
if signed_in?
@album = Album.find(params[:album_id])
@video = @album.build_video
end
end
def create
@album = Album.find(params[:album_id])
@video = @album.build_video(params[:video])
if @video.save
flash[:success] = "Success!"
redirect_to new_small_reward_path(:album_id => @album)
else
render 'new'
end
end
end
I even added a hidden field to the form, which I didn’t think I should have to do, but decided to try:
The URL says /videos/new?album_id=1 before you click submit.
This problem completely goes away if I write the controller with this:
def new
@@album = Album.find(params[:album_id])
end
and then continue to use the class variable throughout the entire thing. But someone told me that using a class variable is discouraged. How do I do this correctly?
1 Answer