Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def create
- params[:book][:authors].map!{|id| Author.find(id)} if params[:book][:authors]
- if params[:author_text] and params[:author_text].strip != ''
- authors = params[:author_text].strip.split(/[\r\n]+/)
- params[:book][:authors] = Array.new if !params[:book][:authors]
- authors.each do |string|
- _, part1, part2 = string.match(/(.*)\s([^\s]+)$/).to_a
- params[:book][:authors] << Author.find_or_create_by_first_name_and_last_name(part1, part2)
- end
- end
- @book = Book.new(params[:book])
- respond_to do |format|
- if @book.save
- flash[:notice] = 'Book was successfully created.'
- format.html { redirect_to(@book) }
- format.xml { render :xml => @book, :status => :created, :location => @book }
- else
- @books = Book.all
- format.html { render :action => "index" }
- format.xml { render :xml => @book.errors, :status => :unprocessable_entity }
- end
- end
- end
Add Comment
Please, Sign In to add comment