如何在一个Ruby on Rails表单中编辑多个记录?

时间:2021-12-25 07:21:15

I'm making a simple to-do list application to teach myself Ruby on Rails, however I've run into a problem. I have a simple form that lists to-do items with a check box to the left of them, and an "Update" button at the bottom, as so:

我正在制作一个简单的待办事项列表应用程序来自学Ruby on Rails,但是我遇到了一个问题。我有一个简单的表单,列出待办事项,左侧有一个复选框,底部有一个“更新”按钮,如下所示:

[ ] Do the dishes
[ ] Take out the garbage
[ ] Take over the world
( Update )

[]做菜[]取出垃圾[]接管世界(更新)

Each to-do item is a separate record in the database with a "completed" boolean field. I want the form to submit a list of checked-off item ids to an action in which I can set each item's "completed" field to true, which will hide them from the view.

每个待办事项都是数据库中的单独记录,带有“已完成”的布尔字段。我希望表单将一个已检查项目ID的列表提交给一个动作,在该动作中我可以将每个项目的“已完成”字段设置为true,这将使它们从视图中隐藏。

I know how to make a form that references multiple models, but not one that references multiple records of the same model. Any tips?

我知道如何制作一个引用多个模型的表单,但不是一个引用同一模型的多个记录的表单。有小费吗?

Thanks!

4 个解决方案

#1


26  

Railscasts is your friend!

Railscasts是你的朋友!

http://railscasts.com/episodes/52-update-through-checkboxes

It's really simple:

这很简单:

# routes.rb
map.resources :tasks, :collection => { :complete => :put }

# tasks_controller.rb
def complete
  Task.update_all(["completed_at=?", Time.now], :id => params[:task_ids])
end

# views\tasks\complete.html.erb
<% form_tag complete_tasks_path, :method => :put do %>
  <ul>
  <% for task in @incomplete_tasks %>
    <li>
      <%= check_box_tag "task_ids[]", task.id %>
      <%= task.name %>
    </li>
  <% end %>
  </ul>
  <%= submit_tag "Mark as Complete" %>
<% end %>

#2


2  

Wow. That is some timing. The item right before this question in my RSS reader is the latest railscast from Ryan Bates and it appears to cover exactly what you are asking.

哇。那是一些时机。我的RSS阅读器中的这个问题之前的项目是来自Ryan Bates的最新轨道广播,它似乎涵盖了您的要求。

#3


1  

Why not use a remote function to update the database when an item is checked, and get rid of the update button altogether?

为什么不在选中项目时使用远程函数更新数据库,并完全删除更新按钮?

#4


0  

Since you can include the ID for each record when you create your form you will want the controller to iterate through items in you post then find and update each of those records accordingly.

由于您可以在创建表单时包含每条记录的ID,因此您希望控制器迭代您发布的项目,然后相应地查找和更新每条记录。

#1


26  

Railscasts is your friend!

Railscasts是你的朋友!

http://railscasts.com/episodes/52-update-through-checkboxes

It's really simple:

这很简单:

# routes.rb
map.resources :tasks, :collection => { :complete => :put }

# tasks_controller.rb
def complete
  Task.update_all(["completed_at=?", Time.now], :id => params[:task_ids])
end

# views\tasks\complete.html.erb
<% form_tag complete_tasks_path, :method => :put do %>
  <ul>
  <% for task in @incomplete_tasks %>
    <li>
      <%= check_box_tag "task_ids[]", task.id %>
      <%= task.name %>
    </li>
  <% end %>
  </ul>
  <%= submit_tag "Mark as Complete" %>
<% end %>

#2


2  

Wow. That is some timing. The item right before this question in my RSS reader is the latest railscast from Ryan Bates and it appears to cover exactly what you are asking.

哇。那是一些时机。我的RSS阅读器中的这个问题之前的项目是来自Ryan Bates的最新轨道广播,它似乎涵盖了您的要求。

#3


1  

Why not use a remote function to update the database when an item is checked, and get rid of the update button altogether?

为什么不在选中项目时使用远程函数更新数据库,并完全删除更新按钮?

#4


0  

Since you can include the ID for each record when you create your form you will want the controller to iterate through items in you post then find and update each of those records accordingly.

由于您可以在创建表单时包含每条记录的ID,因此您希望控制器迭代您发布的项目,然后相应地查找和更新每条记录。