0

I have two problems.

I have a scenario where I am rendering multiple forms on a single page, and I need to be able to assign unique id's to them that will stay assigned even when validation errors occur (I am trying to avoid having the new instances of a form render a different id each time)

So firstly I have these 3 models

class Skill < ActiveRecord::Base
  has_many :documents
end

class User < ActiveRecord::Base
  has_many :documents
end

class Document < ActiveRecord::Base
  mount_uploader :media, MediaUploader
  belongs_to :user
  belongs_to :skill

 validates_presence_of :media, message: 'At least 1 File is required'
end

So on my page I render multiple skills, and each skill can have a document (up to three in fact). So each skill gets its own form

class PublicController < ApplicationController

  def index
    @skills = Skill.all
  end
end

public/index View

<% @skills.group_by(&:year_group_name).each do |key, value| %>
  <%= key %>
    <% value.group_by(&:element_name).each do |key_one, value_one| %>
      <%= key_one %>
        <% value_one.each do |s| %> 
             <% document = current_user.documents.where(skill_id: s.id ) %>
             <%= s.skill_description %>
               <% if document %>
                 <% document.each do |doc| %>
                   <%= doc %>
                 <% end %>                     
                  <%= render template: '/documents/new' %>
              <% else %>
                  <%= render template: '/documents/new' %>
              <% end %> 
        <% end %>
     <% end %> 

documents/new

<% object = @document || Document.new %>
  <%if object.errors.any? %>
    <h2><%= pluralize(object.errors.count, "error") %> prohibited this record from being saved</h2>
      <ul class="error_list">
        <% object.errors.full_messages.each do |msg| %>
          <li><%= error_edit(msg) %></li>
          <% end %>
      </ul>
  <% end %>

<%= form_for object, :html => { multipart: true, id: object.object_id.to_s, class: 'upload_document' } do |f| %>

  <%= f.hidden_field :skill_id, class: 'skill_id' %>

  <%= f.label :media %>

  <%= f.file_field :media %>

  <%= f.submit 'Upload' %>
<% end %>

So at the moment i am using the object_id to return the integer identifier of the object. Whilst this does give me a unique id for each form, they do change when validation fails as a new object is rendered

I have thought of two possible ways of covering this,

  1. if an object exists (so instance of @document) then use its id
  2. if it's a new instance, assign the Skill id that the form is within.

This way the id would stay consistent even when validation fails..

I am having some trouble implementing this though, I cant seem to access the Skill.id within my form_for and as for @document assigning it like this throws unable to find document with id =

class PublicController < ApplicationController

  def index
    @skills = Skill.all
    @document = Document.find(params[:id])
  end

There has been one scenario where I have been able to assign the @document.id to a form that has a record, but i have had to hardcode it, which is obviously no good

Scenario 1

class PublicController < ApplicationController

  def index
    @skills = Skill.all
    @document = Document.find(1)
  end

<%= form_for object, :html => { multipart: true, id: object.id, class: 'upload_document' } do |f| %>

So this assigns the id of 1 to all my forms. What approach can I take here?

halfer
  • 19,824
  • 17
  • 99
  • 186
Richlewis
  • 15,070
  • 37
  • 122
  • 283
  • A bit hard to follow. The `html: { id: object.id}` is nothing more than a css selector for your front end. The form's id has no effect on your backend - rails controller never sees it. – Magnuss Jan 29 '15 at 22:30
  • possible duplicate of [Capturing errors for individual forms](http://stackoverflow.com/questions/28154176/capturing-errors-for-individual-forms) – Yan Foto Jan 30 '15 at 00:02

0 Answers0