I have a modal form rendered via javascript. The model is called a book .
# controllers/books_controller.rb def new @book = Book.new end def create @book = Book.find(params[:id]) @book.save end
Instead of new and html editing, I use coffeescript:
# views/new.js.coffee CustomModal.open "<%= j render('books/modal_form', book: @book) %>"
-
# views/create.js.coffee <% if @book.valid? %> CustomModal.hide() # Other callback scripts for showing alert, etc <% else %> # Script for showing errors in the modal <% end %>
And the link to start the modal:
= link_to "Create Book", new_book_path, remote: true
Now the problem is that this link was only used on the book list page. Thus, the js callback, when the book was created, triggered a warning and updated the list with the changes.
Now I have to add this button to another page where there is no list, so I need another callback (no matter what callbacks really are).
So, I had to add something like to create.js.coffee:
# views/create.js.coffee <% if @book.valid? %> CustomModal.hide() # if the list exists # show alert # update lists # else # do different things # end <% else %> # Script for showing errors in the modal <% end %>
It seems to be dirty, but it is not so terrible. The problem is that I now have more than three conditional expressions, because the "Create Book" button is used several times along the web application.
So, any ideas on a design pattern for this?
ruby-on-rails coffeescript turbolinks
Ariel scherman
source share