Just getting started in rails and trying to refactor down a bit of my view, and I'm a bit stumped as to where to go for the answer since I'm inside an each loop I need access to the |item|.
I'm pulling the writer from a CSV file, using a creator table and a HABTM assocation with books. The reason is sometimes the writer is also the illustrator and also the editor, I didn't want 3 (and more maybe) tables for each specific role of a book author.
Books Model ( with CSV import )
writer = Creator.where(name: row['writer']).first_or_create!(row.to_hash.slice(:writer))
book.creators << writer
book.writer_id = writer.id
Book View
books.each do |book|
link_to book.creators.detect{|w| w.id == book.writer_id}.name, creator_path(id: book.writer_id)
And then I duplicate that for artist, editor, etc...
I first tried a @books in the controller, but I need to know which book from the loop. I'm not sure the next step to learn about so I can remove the ugly. I've seen lots of things I want to go read up on, but not sure which suits this need. Helpers? Named scopes?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…