简体   繁体   English

用于更新Rails 4中记录的新表格

[英]A new form to update a record in Rails 4

I have a Rails 4 app. 我有一个Rails 4应用程序。 In this app, I have multiple HABTM relationships between various models. 在这个应用程序中,我在各种模型之间具有多个HABTM关系。 For example, a task can have many learning objectives as well as many missions associated with it. 例如,一项任务可以具有许多学习目标以及与之相关的许多任务。 Right now, I have the edit form in modal dialog that edits the task record and is invoked via the edit_task_path(@task) that updates the record. 现在,我在模式对话框中有一个编辑表单,用于编辑任务记录,并通过更新记录的edit_task_path(@task)进行调用。 I would like to create a new form that also does that update action; 我想创建一个新表单,该表单也执行该更新操作; however, I only want this form to be for updating the associations for the tasks. 但是,我只希望此表单用于更新任务的关联。 My main question is: how do I create and call a second form to route to the update action for the same record? 我的主要问题是:如何创建和调用第二个表单以路由到同一记录的更新操作? I would basically like a update_task_path(@task) that handles the update controller action as well. 我基本上想要一个update_task_path(@task)来处理更新控制器的动作。 I've looked at various stack overflows and none specifically address a new form for the update controller action. 我研究了各种堆栈溢出,但没有一个专门针对更新控制器操作的新表单。

As you're routing to the same place you don't really need to do anything other than have a seperate form partial. 当您要路由到同一个地方时,除了将表单的一部分单独放置之外,您实际上不需要执行任何其他操作。 I assume the task would have to be valid at the point you're changing associations, and the associations would be valid (or you don't have anything to validate associated objects) otherwise on failure it would render the main form if that is what you've put in the controller. 我假设任务在您更改关联时必须是有效的,并且关联将是有效的(或者您没有任何东西可以验证关联的对象),否则在失败时它将呈现主要形式您已放入控制器。

# main_form
<%= form_for @task do |f| %>
  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>
<% end %>

# mission_and_learning_objective_form
<%= form_for @task do |f| %>
  <div class="field">
    <%= f.collection_check_boxes :mission_ids, Mission.all, :id, :name %>
  </div>

  <div class="field">
    <%= f.collection_check_boxes :learning_objective_ids, LearningObjective.all, :id, :name %>
  </div>
<% end %>

You might consider having a seperate controller just for that though 您可能会为此而考虑使用单独的控制器

Routes 路线

resources :task_settings, only: [:edit, :update]

Controller 控制者

class TaskSettingsController < ApplicationController
  def edit
    @task = Task.find(params[:id])
  end

  def update
    @task = Task.find(params[:id])
    if @task.update(task_setting_params)
      redirect_to @task, notice: "Settings Updated"
    else
      render :edit
    end
  end

  private

  def task_setting_params
    params.require(:task).permit(mission_ids: [], learning_objective_ids: [])
  end
end

View 视图

# task_settings/edit
<%= form_for @task, url: task_setting_path(@task) do |f| %>
  <div class="field">
    <%= f.collection_check_boxes :mission_ids, Mission.all, :id, :name %>
  </div>

  <div class="field">
    <%= f.collection_check_boxes :learning_objective_ids, LearningObjective.all, :id, :name %>
  </div>
<% end %>

To me having a separate controller is a bit cleaner, especially if you want different behaviour if you're only updating the associations. 对我来说,拥有一个单独的控制器会更干净一些,尤其是如果您仅在更新关联时想要不同的行为时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM