Guest User

Untitled

a guest
May 20th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. def create
  2.  
  3. params[:book][:authors].map!{|id| Author.find(id)} if params[:book][:authors]
  4.  
  5. if params[:author_text] and params[:author_text].strip != ''
  6. authors = params[:author_text].strip.split(/[\r\n]+/)
  7. params[:book][:authors] = Array.new if !params[:book][:authors]
  8. authors.each do |string|
  9. _, part1, part2 = string.match(/(.*)\s([^\s]+)$/).to_a
  10. params[:book][:authors] << Author.find_or_create_by_first_name_and_last_name(part1, part2)
  11. end
  12. end
  13.  
  14.  
  15. @book = Book.new(params[:book])
  16.  
  17. respond_to do |format|
  18. if @book.save
  19. flash[:notice] = 'Book was successfully created.'
  20. format.html { redirect_to(@book) }
  21. format.xml { render :xml => @book, :status => :created, :location => @book }
  22. else
  23. @books = Book.all
  24. format.html { render :action => "index" }
  25. format.xml { render :xml => @book.errors, :status => :unprocessable_entity }
  26. end
  27. end
  28. end
Add Comment
Please, Sign In to add comment