I have 3 scaffolds that resources are nested as following: mangas > chapters > scans
. I'm trying to create a website that allows visitors to read mangas chapters, but I'm getting some difficulties to set up the scan_controller's setter.
I have some constraints to follow: the manga scaffold uses FriendlyId, and both chapters and scans are shown to the URL via their chapter/scan number (an integer)
So, for the manga controller, i did the following:
private
def set_manga
@manga = Manga.friendly.find(params[:id])
end
I simply followed the manuel, no problem whatsoever.
For the chapter_controller, I did this
private
def set_chapter
@manga = Manga.find_by(slug: params[:manga_id])
@chapter = @manga.chapters.find_by(chapter_number: params[:id])
end
This allows me to get all chapters that are linked to the manga I want, and only them. Plus, I get to pass the chapter_number as an id into the link.
And, lastly, I tried this for the scan_controller:
private
def set_scan
@manga = Manga.find_by(slug: params[:manga_id])
@chapter = @manga.chapters.find_by(chapter_number: params[:id])
@scan = @manga.chapters.scans.find(params[:id])
end
However, at this point, I can't get a satisfying result. With this configuration, I get undefined method 'pejis' for #<Chapter::ActiveRecord_Associations_CollectionProxy:0x0000000713d1a0>
. I tried to also set as follow:
private
def set_scan
@chapter = Chapter.find_by(chapter_number: params[:id])
@scan = @chapter.scans.find_by(scan_number: params[:id])
end
But i get undefined method 'pejis' for nil:NilClass
, which is weird because it doesn't return any error with predefined ids in the rails console.
As anyone an idea ?
Thank you in advance
So, after re-reading again and again my code, I simply needed to do the following:
So I had to remove @manga since it's not concerned anymore, then to set the scan's chapter_id as being the chapter's number.