Vell
Vell

Reputation: 357

activeadmin rails 4 understanding how to create custom forms

I am new to activeadmin / formtastic and I have having a bit of trouble understanding how things work. I read through the documentation on how to create a form using formtastic but I seem to be still running into issues and I am sure its me not understanding how things work.

I am creating a discussions application very similar to a blog application and the end result is that I would like to create an interface for the administrators to add comments to discussions without having to go into the users interface.

My starting point is the discussions view in the admin section presented by activeadmin. I am attempting to work on the add comment form. According to the instructions, I should be able to add a form using

form partial: 'new_admin_comment_form', locals {discussion_comment: DiscussionComment.new}

which then I should create this partial in app/views/admin/discussions folder. I have done that and have entered some arbitrary text to make sure the partial renders and it does. But once I start adding code I am not able to get the form to display.

The current code I am working with is:

<%= semantic_form_for [:admin, discussion_comment] do |f| %>
    <%= f.inputs, :body %>
    <%= f.actions %>
<% end %>

So a few questions I have that I wasn't able to find in the documentation:

  1. Where do I create instance variables to be used in my form? I have been setting these in the activeadmin files and that is bothering me.
  2. How do I pass params around? I assumed I could do this as normal yet when I try to view them using <%= debug params.inspect %>, it is empty even when I should have at least the id that was in the parent form. Even when using locals: {id: params[:id]}, id is empty in the partial.
  3. What are the best ways to debug why my form is not appearing? Am I able to use regular ERB if worse comes to worse?

Upvotes: 1

Views: 715

Answers (1)

Mark Fraser
Mark Fraser

Reputation: 3198

You can do this without a custom form. If you stick to the active admin DSL you can use its has_many method. Example here:

http://www.activeadmin.info/docs/5-forms.html

Your Discussion model should look like this

class Discussion < ActiveRecord::Base

    has_many :discussion_comments
    accepts_nested_attributes_for :discussion_comments, allow_destroy: true
end

Upvotes: -1

Related Questions