简体   繁体   中英

Rails Active Admin - Edit/create association on show page?

I have a show page for an author that lists the authors books. I'd like to be able to add a book name in line to quickly add a new book association. I could do it pretty easily with rails MVC on the front end, but I am having a hard time undertsanding the activeadmin DSL to do things that are not quite out of the box.

Here's the current table of data on the show page, straight-forward.

    show :title => :name do
        panel "Books" do
            table_for(author.books) do 
                column("Book") { |book| link_to book.name.titleize, admin_book_path(book) }
                column("Release") { |book| book.release_date.to_formatted_s(:long_ordinal)}
                column("Sales") { |book| book.orders.count }
            end
        end
    end

What I would like to see is at the bottom of the list a blank textfield that I can input a new book title and click ADD to add it to the list. But I only see info in the docs on how to edit the form itself, which gets shown only on the edit page.

Overall I love how quickly activeadmin sets up, but it feels like I'm missing some basic concepts of how it functions, and the docs aren't helping me grasp it...

You could for example render a from as a partial in the Show page and add any needed controller actions to add books and redirect back to Show page.

    row("Add book") do |item|
      div do
        render "form_to_add_books" 
      end

Just use associations in your models and use it like this

form do |f|
  f.inputs do
    f.input :book_name
      f.inputs do
        f.has_many :titles do |p|
           p.input :title
         end  
        end
       end

f.has_many will automatically generates the button.

And don't forget to use

accepts_nested_attributes_for :titles

in your books model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM