- 浏览: 94317 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
xylffxyfpp:
应该改成慎用each
万恶的"delete",慎用数组的delete -
xylffxyfpp:
a = [1,2,3,4,5,6]
a.each do ...
万恶的"delete",慎用数组的delete -
CaiDeHen:
受教了,我也不知道当时为什么会在这个地方这样子用。其他地方也是 ...
万恶的"delete",慎用数组的delete -
wosmvp:
感觉太乱了为什么不在User模型中来个 named_scope ...
万恶的"delete",慎用数组的delete -
jiachengxi38:
不知道楼主这样的逻辑想法从何而来,权限访问的思想好像在lZ这里 ...
万恶的"delete",慎用数组的delete
In this third and final episode on complex forms I will show you how to edit a project and multiple tasks all in one form. This includes removing and adding tasks dynamically as well. See the show notes for updated code.
Validation
Displaying the validation errors can be tricky depending on your needs. It may be as simple as displaying the project validations:
This will mention if there are task validation errors, however it will not state exactly what the errors are because these are stored in each task model.
If you need to display the task error messages. One way is to do this:
The error_messages_for method requires an instance variable with the same name, so that is why it needs to be set right there. This is an ugly work around.
<!-- projects/edit.rhtml --> <% form_for :project, :url => project_path(@project), :html => { :method => 'put' } do |f| %> <%= render :partial => 'fields', :locals => { :f => f } %> <p><%= submit_tag "Update Project" %></p> <% end %> <!-- projects/new.rhtml --> <% form_for :project, :url => projects_path do |f| %> <%= render :partial => 'fields', :locals => { :f => f } %> <p><%= submit_tag "Create Project" %></p> <% end %> <!-- projects/_fields.rhtml --> <p> Name: <%= f.text_field :name %> </p> <div id="tasks"> <%= render :partial => 'task', :collection => @project.tasks %> </div> <p><%= add_task_link "Add a task" %></p> <!-- projects/_task.rhtml --> <div class="task"> <% fields_for_task(task) do |task_form| %> <p> Task: <%= task_form.text_field :name %> <%= link_to_function "remove", "$(this).up('.task').remove()" %> </p> <% end %> </div> # models/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 # models/task.rb class Task < ActiveRecord::Base belongs_to :project validates_presence_of :name end # projects_controller.rb 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 # projects_helper.rb 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
Validation
Displaying the validation errors can be tricky depending on your needs. It may be as simple as displaying the project validations:
<!-- projects/_fields.rhtml --> <%= error_messages_for :project %>
This will mention if there are task validation errors, however it will not state exactly what the errors are because these are stored in each task model.
If you need to display the task error messages. One way is to do this:
<!-- projects/_task.rhtml --> <% @task = task %> <%= error_messages_for :task %>
The error_messages_for method requires an instance variable with the same name, so that is why it needs to be set right there. This is an ugly work around.
发表评论
-
#125 Dynamic Layouts
2008-12-13 21:41 887Discover how to dynamically cha ... -
#124 Beta Invitations
2008-12-13 21:37 780You know those invitation syste ... -
#123 Subdomains
2008-12-13 15:56 1068Learn how to unleash the full p ... -
#122 Passenger in Development
2008-12-13 15:51 833Tired of juggling multiple Rail ... -
#121 Non Active Record Model
2008-12-13 15:47 890This episode will show you how ... -
#120 Thinking Sphinx
2008-12-12 16:59 859If you need a full text search ... -
#119 Session Based Model
2008-12-12 16:41 1016If you have a lot of logic asso ... -
#118 Liquid
2008-12-12 16:18 874Liquid is a safe way to provide ... -
#117 Semi-Static Pages
2008-12-12 14:20 829Static pages can sometimes be a ... -
#116 Selenium
2008-12-12 14:11 809Selenium is a great way to test ... -
#115 Caching in Rails 2.1
2008-12-12 14:07 862Rails 2.1 brings some new cachi ... -
#114 Endless Page
2008-12-12 13:50 1005Ever wondered how some sites se ... -
#112 Anonymous Scopes
2008-12-12 13:32 835The scoped method allows you to ... -
#111 Advanced Search Form
2008-12-12 13:27 744If you need to create an advanc ... -
#110 Gem Dependencies
2008-12-12 12:57 760In Rails 2.1 we now have the ab ... -
#109 Tracking Attribute Changes
2008-12-12 12:54 799Rails 2.1 keeps track of the ch ... -
#108 named_scope
2008-12-12 12:52 749The named_scope method in Rails ... -
#107 Migrations in Rails 2.1
2008-12-12 12:49 692Migrations now have a timestamp ... -
#106 Time Zones in Rails 2.1
2008-12-12 12:48 719In the past, time zones have be ... -
#104 Exception Notifications
2008-12-12 11:48 714If you're running a production ...
相关推荐
- **Primitive Types (Chapter 3):** Here, students learn about the basic data types available in F#, such as integers, floats, and characters. Understanding these types is crucial for performing ...
The printing system is a crucial part of SymPy. It allows users to format expressions in various styles, such as LaTeX, MathML, and plain text. ##### 5.18 Plotting Module Visualization is an ...
The course follows a structured approach, starting with fundamental concepts and gradually moving towards more complex topics. The sequence ensures that learners build a solid foundation before ...
- **例句**:Social media has become an integral part of our daily lives. ##### 2. Rate 速率,比率,等级,价格,费用,估价 - **应用场景**:统计学、经济学等。 - **例句**:The unemployment rate is ...
It provides a visual programming interface that simplifies the process of creating complex forms and structures through a series of connected components and parameters. The tool is primarily used in ...
It provides a comprehensive set of tools and libraries that facilitate the creation of complex user interfaces and robust back-end services. **GWT in Action** is a comprehensive guide that covers all...
复数由实部(real part)和虚部(imaginary part)组成。在C#中,我们通过定义私有成员变量`re`和`im`来存储这两个部分。在类的声明中,我们有以下代码: ```csharp class Complex { private double re, im; ``` ...
Review of Prerequisites 3 CHAPTER 1.1 Real Numbers, Mathematical Induction, and Mathematical Conventions 4 1.2 Complex Numbers 10 1.3 The Complex Plane 15 1.4 Modulus and Argument Representation of ...
The book is part of the *Information Science and Statistics* series, edited by notable figures such as M. Jordan, J. Kleinberg, and B. Schölkopf. ### Key Knowledge Points #### 1. **Overview of ...
The third part Applications shows how the abstract problems defined in the second part underlie technologies capable to perform complex tasks such as the recognition of hand gestures or the ...
- **Complex Queries:** Techniques for building complex queries, including joins and subqueries. **Chapter 15: Making Your Job Easier with PEAR** - **PEAR Overview:** Explanation of what PEAR ...
QTFF also supports interactive features, enabling the creation of multimedia content with clickable links, forms, and other interactive elements. This feature is particularly useful for educational ...
Part 3 – AngularJS Modules and Services 18. Working with Modules and Services 19. Services for Global Objects, Errors and Expressions 20. Services for Ajax and Promises 21. Services for REST 22. ...
Part 3 explores multiple real-world case studies spanning diverse domains and industries like retail, transportation, movies, music, marketing, computer vision and finance. For each case study, you ...