# Generating Dynamic text box

**URL:** <https://www.ruby-forum.com/t/generating-dynamic-text-box/189457>\
**Category:** Rails\
**Created:** [June 1, 2010, 11:50am UTC](https://www.ruby-forum.com/t/generating-dynamic-text-box/189457 "2010-06-01T11:50:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![venu1234](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/41988e/32.png) [@venu1234](https://www.ruby-forum.com/u/venu1234)\
**Post date:** [June 1, 2010, 11:50am UTC](https://www.ruby-forum.com/t/generating-dynamic-text-box/189457/1 "2010-06-01T11:50:49Z")

</div>

#Project controller

class ProjectsController \< ApplicationController

def index  
@projects = Project.find(:all)  
end

def new  
@project = Project.new  
@project.tasks.build  
end

def create  
@project = Project.new(params[:project])  
if @project.save  
flash[:notice] = “Successfully created project and tasks.”  
redirect\_to projects\_path  
else  
render :action =\> ‘new’  
end  
end

def edit  
@project = Project.find(params[:id])  
end

def update  
params[:project][:existing\_task\_attributes] ||= {}

@project = Project.find(params[:id])  
if @project.update\_attributes(params[:project])  
flash[:notice] = “Successfully updated project and tasks.”  
redirect\_to project\_path(@project)  
else  
render :action =\> ‘edit’  
end  
end

end

#Project.rb

class Project \< ActiveRecord::Base

has\_many :tasks, :dependent =\> :destroy

validates\_presence\_of :name  
validates\_associated :tasks

after\_update :save\_tasks

def new\_task\_attributes=(task\_attributes)  
task\_attributes.each do |attributes|  
tasks.build(attributes)  
end  
end

def existing\_task\_attributes=(task\_attributes)  
tasks.reject(&:new\_record?).each do |task|  
attributes = task\_attributes[task.id.to\_s]  
if attributes  
task.attributes = attributes  
else  
tasks.delete(task)  
end  
end  
end

def save\_tasks  
tasks.each do |task|  
task.save(false)  
end  
end

end

#Project/\_task.html.erb

\<% @task = task %\>  
\<%= error\_messages\_for :task %\>

 \<% fields\_for\_task(task) do |task\_form| %\> 

Task: \<%= task\_form.text\_field :name %\> \<%= link\_to\_function "remove", "$(this).up('.task').remove()" %\>

 \<% end %\>

#Project/edit.html.erb

\<% form\_for :project, :url =\> project\_path(@project), :html =\> { :method  
=\> ‘put’ } do |f| %\>  
\<%= render :partial =\> ‘fields’, :locals =\> { :f =\> f } %\>

\<%= submit\_tag "Update Project" %\>

\<% end %\>

#Project/\_fields.html.erb

\<%= error\_messages\_for :project %\>

Name: \<%= f.text\_field :name %\>

 \<%= render :partial =\> 'task', :collection =\> @project.tasks %\>

\<%= link\_to\_function('Add') do |page| page.visual\_effect :highlight, 'tasks' end%\>

#Project/show.html.erb

\<%= link\_to ‘Edit’, edit\_project\_path(@project) %\> |  
\<%= link\_to ‘Back’, projects\_path %\>

#projects/new.html.erb

\<% form\_for :project, :url =\> projects\_path do |f| %\>  
\<%= render :partial =\> ‘fields’, :locals =\> { :f =\> f } %\>

\<%= submit\_tag "Create Project" %\>

\<% end %\>

#project\_helper

module ProjectsHelper

def fields\_for\_task(task, &block)  
prefix = task.new\_record? ? ‘new’ : ‘existing’  
fields\_for(“project[#{prefix}\_task\_attributes][]”, task, &block)  
end

def add\_task\_link(name)  
link\_to\_function name do |page|  
page.insert\_html :bottom, :tasks, :partial =\> ‘task’, :object =\>  
Task.new  
end  
end

end

#Task.rb

class Task \< ActiveRecord::Base

belongs\_to :project  
validates\_presence\_of :name

end

#

---

<div class="post-metadata">

**Author:** ![venu1234](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/41988e/32.png) [@venu1234](https://www.ruby-forum.com/u/venu1234)\
**Post date:** [June 1, 2010, 4:18pm UTC](https://www.ruby-forum.com/t/generating-dynamic-text-box/189457/2 "2010-06-01T16:18:31Z")

</div>

What’s your question? Can you express it simply without listing your  
entire codebase?
