How can I pass data from one scaffold to another?

183 views Asked by At

I'm currently creating a website where one can read several mangas. The structure is as follow (and as asked by the people I'm working for): First of all, you have all the manga titles (let's say, Naruto, One Piece, One Punch, etc). Then, when you click on one of the mangas, you get to the chapters linked to the manga. Then you finally access to the manga's scans.

What bothers me is that I used two different scaffolds (one for the mangas and an other for the chapters), but I don't know how to tell Rails, when a staff member wants to add a chapter to a manga, which manga the chapter is linked to.

I decided to create a link to the chapters' creation (chapters#new) from a manga's title (mangas#show), and to pass the manga's id to the chapter's creation url as following:

routes.rb

get '/chapters/new/:manga_id' => 'chapters#new', as: :new_chapter

manga#show

<%= link_to 'Add a new chapter', new_chapter_path(@manga.attributes['id']) %>

(Since I use FriendlyId, this was the only method i could find to get the real id instead of the slug)

So far, when I get to the chapters#new action, my url looks like the following: http://localhost:3000/chapters/new/1, 1 being the manga id that transmitted with the link.

Then, in the controller, I isolate the id as follows:

def create
    @chapter = Chapter.new(chapter_params)

    if @chapter.save
        @chapter.manga = request.original_url.split('/').last.to_i
      end
      redirect_to @chapter, notice: 'Chapter was successfully created.'
    else
      render :new
    end
  end

However, when submitting the form, I get the following error:

Manga(#106972960) expected, got Fixnum(#10269260)

I believe this is because I gave the controller a number while he was waiting for a whole object, but I thought this was sufficient, and now I run out of idea.

Anyone has an idea?

1

There are 1 answers

2
Alexander Luna On

I suggest laying your site out nested, that way you have CRUD operations for all the important things. For example you would have a Manga Model, Chapter Model and Scan Model.

The Manga Model stores individual mangas and lets you create, update, delete, etc each Manga, The Chapter Model stores the individual chapters of your Manga Model. The Scan Model stores the individual Scans of your chapter.

You don't have to break your head building links. Rails does everything for you. You just have to specify the resources (Controllers) as nested like this in your routes.rb

  resources :Manga do
    resources :Chapter do
      resources Scans
    end
  end

This defines all the routes you need to have CRUD operations on all your resources and the links are organized automatically like this:

yourdomain.com/manga/manga_number/chapter/chapter_number/scans/scan_number

When you create a new Manga:

yourdomain.com/manga/new

When you create a new chapter in your manga:

yourdomain.com/manga/manga_number/chapter/new

When you create a new scan in your chapter:

yourdomain.com/manga/manga_number/chapter/chapter_number/scans/new

All of this thanks to nesting your resources in your router.