`
CaiDeHen
  • 浏览: 96896 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

#73 Complex Forms Part 1

阅读更多
Complex forms often lead to complex controllers, but that doesn't have to be the case. In this episode see how you can create multiple models through a single form while keeping the controller clean.
# projects_controller.rb
def new
  @project = Project.new
  3.times { @project.tasks.build }
end

def create
  @project = Project.new(params[:project])
  if @project.save
    flash[:notice] = "Successfully created project."
    redirect_to projects_path
  else
    render :action => 'new'
  end
end

# models/project.rb
def task_attributes=(task_attributes)
  task_attributes.each do |attributes|
    tasks.build(attributes)
  end
end

<!-- new.rhtml -->
<% form_for :project, :url => projects_path do |f| %>
  <p>
    Name: <%= f.text_field :name %>
  </p>
  <% for task in @project.tasks %>
    <% fields_for "project[task_attributes][]", task do |task_form| %>
      <p>
        Task: <%= task_form.text_field :name %>
      </p>
    <% end %>
  <% end %>
  <p><%= submit_tag "Create Project" %></p>
<% end %>
分享到:
评论

相关推荐

    PROFESSIONAL F# 2.0

    The basics section forms the foundation of the F# language, introducing essential concepts and constructs. - **Lexical Structure (Chapter 2):** This chapter covers the lexical structure of F#, ...

    sympy-docs-pdf-1.0.pdf

    #### 1. Installation SymPy is a Python library for symbolic mathematics. The installation process is straightforward and can be done through several methods. ##### 1.1 Anaconda One popular way to ...

    Cours-VB-NET-2010-Developpez-com

    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 ...

    modeling algorithm

    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 ...

    GWT in Action

    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...

    软件开发技术基础:第一章 C#.NET程序设计基础3.ppt

    在UI设计中,我们可以创建一个简单的Windows Forms应用程序,包含两个输入框(tb1re, tb1im, tb2re, tb2im)让用户输入复数,一个按钮(btnadd)触发复数相加操作,以及两个文本框(tbrre, tbrim)显示结果。...

    Pattern Recogintion and Machine Learning

    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 ...

    Advanced Engineering Mathematics

    1.2 Complex Numbers 10 1.3 The Complex Plane 15 1.4 Modulus and Argument Representation of Complex Numbers 18 1.5 Roots of Complex Numbers 22 1.6 Partial Fractions 27 1.7 Fundamentals of Determinants ...

    Machine Learning for Audio, Image and Video Analysis 2nd

    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 ...

    Beginning PHP 5.3

    - **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 ...

    Quick Time File Format

    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 ...

    Pro AngularJS

    Part 1 – Getting Started 1. Getting Ready 2. Your First AngularJS App 3. Putting AngularJS in Context 4. HTML and CSS Primer 5. Javascript Primer 6. SportsStore: A Real Application 7. SportsStore: ...

    Advanced Linear Algebra

    Part 1: Preliminaries, 1 Part 2: Algebraic Structures, 17 Part I---Basic Linear Algebra, 33 1 Vector Spaces, 35 Vector Spaces, 35 Subspaces, 37 Direct Sums, 40 Spanning Sets and Linear ...

    Practical Machine Learning with Python(pdf书+sourcecode)

    Part 1 focuses on understanding machine learning concepts and tools. This includes machine learning basics with a broad overview of algorithms, techniques, concepts and applications, followed by a ...

Global site tag (gtag.js) - Google Analytics