Hi, The code below works perfectly but is so repetitive I feel there must be a standard way to refactor it in this situation. I am learning on my own so am wondering how more experienced programmers would do this. Any examples would be appreciated.
def reset_to_previous contents_history = Profile.find(:first, conditions: [ "user_id = ?", current_user.id])
if contents_history[:last_minisection_id].present? session[:minisection_id] = contents_history[:last_minisection_id] session[:subsection_id] = contents_history[:last_subsection_id] session[:section_id] = contents_history[:last_section_id] session[:chapter_id] = contents_history[:last_chapter_id] session[:book_id] = contents_history[:last_book_id] session[:subject_id] = contents_history[:last_subject_id] session[:subject_title] = Subject.find(session[:subject_id]).title session[:book_title] = Book.find(session[:book_id]).title session[:chapter_title] = Chapter.find(session[:chapter_id]).title session[:section_title] = Section.find(session[:section_id]).title session[:subsection_title] = Subsection.find(session[:subsection_id]).title session[:minisection_title] = Minisection.find(session[:minisection_id]).title redirect_to(:controller => "contents", :action => "index_minisections", :subsection_id => session[:subsection_id]) elsif contents_history[:last_subsection_id].present? session[:subsection_id] = contents_history[:last_subsection_id] session[:section_id] = contents_history[:last_section_id] session[:chapter_id] = contents_history[:last_chapter_id] session[:book_id] = contents_history[:last_book_id] session[:subject_id] = contents_history[:last_subject_id] session[:subject_title] = Subject.find(session[:subject_id]).title session[:book_title] = Book.find(session[:book_id]).title session[:chapter_title] = Chapter.find(session[:chapter_id]).title session[:section_title] = Section.find(session[:section_id]).title session[:subsection_title] = Subsection.find(session[:subsection_id]).title redirect_to(:controller => "contents", :action => "index_subsections", :section_id => session[:section_id]) elsif contents_history[:last_section_id].present? session[:section_id] = contents_history[:last_section_id] session[:chapter_id] = contents_history[:last_chapter_id] session[:book_id] = contents_history[:last_book_id] session[:subject_id] = contents_history[:last_subject_id] session[:subject_title] = Subject.find(session[:subject_id]).title session[:book_title] = Book.find(session[:book_id]).title session[:chapter_title] = Chapter.find(session[:chapter_id]).title session[:section_title] = Section.find(session[:section_id]).title redirect_to(:controller => "contents", :action => "index_sections", :chapter_id => session[:chapter_id]) elsif contents_history[:last_chapter_id].present? session[:chapter_id] = contents_history[:last_chapter_id] session[:book_id] = contents_history[:last_book_id] session[:subject_id] = contents_history[:last_subject_id] session[:subject_title] = Subject.find(session[:subject_id]).title session[:book_title] = Book.find(session[:book_id]).title session[:chapter_title] = Chapter.find(session[:chapter_id]).title redirect_to(:controller => "contents", :action => "index_chapters", :book_id => session[:book_id]) elsif contents_history[:last_book_id].present? session[:book_id] = contents_history[:last_book_id] session[:subject_id] = contents_history[:last_subject_id] session[:subject_title] = Subject.find(session[:subject_id]).title session[:book_title] = Book.find(session[:book_id]).title redirect_to(:controller => "contents", :action => "index_books", :subject_id => session[:subject_id]) elsif contents_history[:last_subject_id].present? session[:subject_id] = contents_history[:last_subject_id] session[:subject_title] = Subject.find(session[:subject_id]).title redirect_to(:controller => "subjects", :action => "index") end end
Thanks,
Dave