Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use rails_admin forms in custom views?

I am making my own custom view that I need to make the process of creating associated models less painful for my users. I want to display all of the models associated pieces in-line, with controls to edit them. This is quite easy to roll my own for the basic fields, but I'd rather use a form_filtering_select partial for the inline model's associations, but I can't find any documentation to do this.

like image 479
Daniel Avatar asked Sep 03 '12 22:09

Daniel


1 Answers

You can use Nested Form

Consider a User class which returns an array of Project instances from the projects reader method and responds to the projects_attributes= writer method:

class User
  def projects
    [@project1, @project2]
  end

  def projects_attributes=(attributes)
    # Process the attributes hash
  end
end

Note that the projects_attributes= writer method is in fact required for fields_for to correctly identify :projects as a collection, and the correct indices to be set in the form markup.

When projects is already an association on User you can use accepts_nested_attributes_for to define the writer method for you:

class User < ActiveRecord::Base
  has_many :projects
  accepts_nested_attributes_for :projects
end

This model can now be used with a nested fields_for. The block given to the nested fields_for call will be repeated for each instance in the collection:

<%= nested_form_for @user do |user_form| %>
  ...
  <%= user_form.fields_for :projects do |project_fields| %>
    <% if project_fields.object.active? %>
      Name: <%= project_fields.text_field :name %>
    <% end %>
  <% end %>
  ...
<% end %>

Here goes the Reference for details.

like image 73
Awais Avatar answered Sep 18 '22 16:09

Awais