Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Form pre-populate with values for "name" field

forms pre-popluate dynamic forms

  • Please log in to reply
1 reply to this topic

#1 klyrish

klyrish

    Signalman

  • Members
  • 10 posts
  • LocationDenver, CO

Posted 20 November 2013 - 06:54 PM

So I watched the Railscast on dynamic forms (http://railscasts.co...3-dynamic-forms) and, while cool and possibly something to use later on, I don't think that's exactly what I need for this form.

 

I'm building a hall of records for one of my fantasy football leagues. It's a dynasty league which entails a LOT more than a standard re-draft league. Long-term contracts for players, salary caps, etc. It's crazy, awesome and fun.

 

So one of the things in this hall of records is the yearly payouts. The way it's currently setup, I have to make a new payout by entering the year and then using form_for, create the associated awards one-by-one. What I want to have is it automatically create all 22 of the yearly payout awards immediately upon creating a new payout with no user_id associated with them until they've been won. So right now, I've got it so that it builds 22 awards on the payout#new view, but I still have to enter the award name and amount for each one of them.

 

Is there a way that those could be handled via an array or some other collection so that the 22 awards are not only automatically present on the payouts#new view, but with the award name and amount already inserted in there?

 

Code in case it's necessary:

 

award.rb

class Award < ActiveRecord::Base
  attr_accessible :amount, :name, :user_id, :notes, :awards_attributes, :users_attributes
  
  belongs_to :user
  belongs_to :payout
  
end

payout.rb

class Payout < ActiveRecord::Base
  attr_accessible :year, :awards_attributes, :users_attributes
  
  has_many :awards
  
  accepts_nested_attributes_for :awards,
                                :reject_if => :all_blank,
                                :allow_destroy => true
                                
  default_scope order('year DESC')
end
payouts_controller.rb
def new
    @payout = Payout.new
    22.times {@awards = @payout.awards.build}

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @payout }
    end
  end

And the awards are things like

First place | 600

Second place | 300

Third place | 100

 

etc including division winners (2 of these), most points during the season, weekly high scores (13 of these), highest weekly score, worst record, and a few others.

 

The dynamic form Railscast seems like it could do the trick but would be overkill and a lot of extra work for this since it's not going to be applying to different types of payouts. Each payout is going to have the same awards every year. However, if that truly is the best way to do it, I'm happy to go and refactor my code. I just wanted some input from fresh eyes/minds.

 

EDIT: I should've double-checked the name of this post before submitting as it's not really the best/most-descriptive it could be. Sorry. :(



#2 klyrish

klyrish

    Signalman

  • Members
  • 10 posts
  • LocationDenver, CO

Posted 21 November 2013 - 03:12 PM   Best Answer

Aaaaaaand I figured it out on my own again. I love this forum. I ask questions and just doing that gets me thinking and, so far, I've been able to solve my issues myself. :)

 

For anyone who cares, here's the solution I came up with (there are actually 24 awards now after some changes):

 

payouts_controller.rb

 def new
      @payout = Payout.new
      @payout.initialize_form(24)
     
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @payout }
    end
  end

payout.rb

def initialize_form(count)
    yearly_award_names = [["Champion", 600], 
                      ["Runner up", 300], 
                      ["Third place", 100], 
                      ["Philip Division Champ", 40], 
                      ["Russell Division Champ", 40], 
                      ["Most points during the season", 25],
                      ["High score - Week 1", 5],
                      ["High score - Week 2", 5],
                      ["High score - Week 3", 5],
                      ["High score - Week 4", 5],
                      ["High score - Week 5", 5],
                      ["High score - Week 6", 5],
                      ["High score - Week 7", 5],
                      ["High score - Week 8", 5],
                      ["High score - Week 9", 5],
                      ["High score - Week 10", 5],
                      ["High score - Week 11", 5],
                      ["High score - Week 12", 5],
                      ["High score - Week 13", 5],
                      ["Highest weekly total/season", 10],
                      ["Smallest margin of defeat", 5],
                      ["Worst record", 10],
                      ["Losing to lowest winning score", 5],
                      ["NFL Super Bowl prediction", 5]]
    
    
    count.times{ |i|
      self.awards.build
      awards[i].name =  yearly_award_names[i][0]
      awards[i].amount =  yearly_award_names[i][1]
      
    }
  end

And it works perfectly! 

 

https://www.dropbox....form_fields.png







Also tagged with one or more of these keywords: forms, pre-popluate, dynamic forms

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users