2

I was hoping to solve the problem in an ActiveAdmin like solution.

What I want to do is have the user select something from a drop-down (like a book) and based on that selection I want to autofill info about the selection in text fields below (like author and description). I then want them to be able to edit these descriptions before saving or clicking anything else.

Right now I have code that fills the author and description after it gets saved, but I want it to happen right after a book is selected.

ActiveAdmin.register Summary do
  ...
  form do |f|
    f.inputs do
      f.input :book         # creates a dropdown of books
      f.input :author       # a text box I want to default to the selection's book.author
      f.input :description  # a text box I want to default to the selection's book.description
      ...
    end
  end
  ...


  controller do
    before_filter :add_default, :only => [:create]

    private
    def add_default
      params[:summary]
      if params[:summary][:description] == ""
        params[:summary][:description] = Book.find(params[:book][book_id]).description
      end
      if params[:summary][:author] == ""
        params[:summary][:author] = Book.find(params[:book][book_id]).author
      end
    end
  end
  ...
end
4

0 回答 0