Using forms

 

Load DRG Form in Rails view file.

 

<ul>
  <li class="dc-link dc-animate">
    <%= dc_link_to( 'Notes', 'table',
                    { controller: :cmsedit, table: 'note', formname: 'note' },
                    target: 'iframe_edit', title: 'Create new note') %>
  </li>
</ul>

 

This will in conjunction with proper CSS create an action link on Rails view. Classes dc-link and dc-animate will make link look like a button and provide animation effect when clicked on link.

 

dc_link_to helper method is almost identical to Rails link_to, with added option of icon, which may be fontastic icon name or image filename defined in assets. Edit controller is "cmsedit", edit table is "note", formname is "note" and since is same as table name, it could be left out in this example. Additional parameters can include:

action: (index), edit, new

method: (get), put, post

id: ID of document to edit

ids: when document is embedded a list of all parent document id-s separated by semicolon.

 

Target is important. It defines an iframe where the form will be opened. Title will be drawn on mouse over. 

 

Insert document "edit" link in renderer.

 

def link_4edit
  return '' if @opts[:edit_mode] < 2

  @opts[:editparams].merge!( { table: 'dc_piece',
                               controller: 'cmsedit',
                               action: 'edit',
                               id: @piece.id,
                               title: "#{t('drgcms.edit')}: #{@piece.name}" } )
  dc_link_for_edit( @opts[:editparams] )
end

 

dc_link_for_edit accepts options as hash. Optional parameters have same names as on example above as dc_link_for_edit is internally calling dc_link_to method. @opts[:editparams] var is present by default and can be used for grouping parameters.

 

insert document "create" link in renderer.

 

if @opts[:edit_mode] > 1
  opts = { controller: 'cmsedit', formname: 'dc_page',
           table: 'dc_page;dc_part', ids: @page._id,
           p_div_id: 'picture', title: 'New picture' }
  html << dc_link_for_create(opts)
end

 

This example shows the usage of dc_link_for_create helper. It will create a link for creating a new dc_part document which is embedded in the dc_page document. 

 

It also shows how to provide initialization value to fields on a form. Fields can be initialized by passing a parameter with name preceding by p_. In the example will be field name div_id initialized with a value of 'picture' (p_ is for params).

 

You will learn in the next chapter how to add forms loading to CMS menu.


Last update: 26.01.2022