Screencast: Formulare mit verschachtelten Modellen Teil 2

In dieser Woche geht es weiter mit dem zweiten Teil von Ryans Screencast zu dem Thema „Formulare mit verschachtelten Modellen“. Er wie weitere Felder dynamisch mit JavaScript, über Prototype oder Query, hinzugefügt werden können.

Download:

Download (15.9 MB, 12:40)
alternative download for iPod & Apple TV (20.2 MB, 12:40)

 

Resourcen:

Quellcode:

[html]
<!– _form.html.erb –>
<p><%= link_to_add_fields "Add Question", f, :questions %></p>

<!– _question_fields.html.erb –>
<%= link_to_remove_fields "remove", f %>

<p><%= link_to_add_fields "Add Answer", f, :answers %></p>

<!– _answer_fields.html.erb –>
<%= link_to_remove_fields "remove", f %>
[/html]

[javascript]
// application.js
function remove_fields(link) {
$(link).previous("input[type=hidden]").value = "1";
$(link).up(".fields").hide();
}

function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).up().insert({
before: content.replace(regexp, new_id)
});
}

// application_jquery.js
function remove_fields(link) {
$(link).prev("input[type=hidden]").val("1");
$(link).closest(".fields").hide();
}

function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).parent().before(content.replace(regexp, new_id));
}
[/javascript]

Screencast: Formulare mit verschachtelten Modellen

Fomulare mit verschachtelten Modellen lassen sich viel einfacher mit der Methode accepts_nested_attributes erstellen. Wie diese Methode benutzt werden kann, zeigt Ryan in seinem dieswöchigen Screencast.

Download:

Download (22.4 MB, 11:09)
Alternativer Download für iPod & Apple TV (15.2 MB, 11:09)

 

Resourcen:

Quellcode:

[bash]
rails surveysays
script/generate nifty_layout
script/generate nifty_scaffold survey name:string
script/generate model question survey_id:integer content:text
script/generate model answer question_id:integer content:string
rake db:migrate
[/bash]

[ruby]
# models/survey.rb
class Survey < ActiveRecord::Base
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end

# models/question.rb
class Question < ActiveRecord::Base
belongs_to :survey
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end

# models/answer.rb
class Answer < ActiveRecord::Base
belongs_to :question
end

# surveys_controller.rb
def new
@survey = Survey.new
3.times do
question = @survey.questions.build
4.times { question.answers.build }
end
end
[/ruby]

[html]
<!– views/surveys/_form.html.erb –>
<% form_for @survey do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<% f.fields_for :questions do |builder| %>
<%= render "question_fields", :f => builder %>
<% end %>
<p><%= f.submit "Submit" %></p>
<% end %>

<!– views/surveys/_question_fields.html.erb –>
<p>
<%= f.label :content, "Question" %><br />
<%= f.text_area :content, :rows => 3 %><br />
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove Question" %>
</p>
<% f.fields_for :answers do |builder| %>
<%= render ‚answer_fields‘, :f => builder %>
<% end %>

<!– views/surveys/_answer_fields.html.erb –>
<p>
<%= f.label :content, "Answer" %>
<%= f.text_field :content %>
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove" %>
</p>
[/html]