【问题标题】:Submit tabular form and pass hash to the controller提交表格形式并将哈希传递给控制器
【发布时间】:2014-10-21 15:10:18
【问题描述】:

我开始开发一个简单的 Redmine 插件,允许用户编辑和保存特定的 Redmine 问题自定义字段。我对 Rails 和编写 Redmine 插件完全陌生,所以如果这是一个非常简单且容易回答的问题,请原谅。

到目前为止,我所拥有的是一个表格,其中显示了给定产品和版本的问题,以及其他自定义字段。请在此处查看图片:

changelog table

我想做的是更改自定义字段“注释说明”(在文本区域内),当我点击“保存更改”时,我想向控制器发送一个包含所有字段的哈希表单(包括问题 ID 和问题说明说明),只要单击给定行的复选框。然后,控制器将接收给定问题编号的新描述,并使用用户输入的新字符串更新问题字段“Note description”。

这里是相关代码sn-ps。

index.html.erb:

<h3>Changelog</h3>
<table>
  <%= form_tag(controller: "polls", action: "save", method: "get",
    project_id: @project, id: @version_selected_combo) do %>
  <tr>
    <th class="checkbox"></th>
    <th class="tracker">Tracker</th>
    <th class="issue">Issue ID</th>
    <th class="status">Status</th>
    <th class="description">Note Description</th>
    <th class="subject">Subject</th>
  </tr>
  <% @improvement_issues.each_with_index do |issue, index| %>
  <tr>
    <td class="checkbox"><%= check_box_tag 'issue_checkbox' %></td>
    <td class="tracker"><font color="LimeGreen">
      <%= issue.to_s.split(' ').at(0) %></font></td>
    <td class="issue">
      <%= label_tag(:issue_id, issue.to_s.split(' ').at(1)[1..-2]) %></td>
    <td class="status"><%= @improvement_issues_descriptions[index]  %></td>
    <td class="description">
      <%= text_area_tag(:transfer_note_description,
                        @transfer_notes_descriptions[index], 
                        :cols => "100", :rows => "1") %></td>
    <td class="subject"><%= sanitize issue.to_s.partition(': ')[2] %></td>
  </tr>
  <% end %>
  <tr>
    <td><%= submit_tag("Save changes") %></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <% end %>
</table>

如您所见,我在表格中使用表单助手,我不知道这是否是正确或更好的方法。

polls_controller.rb:

def save
  p params
end

这只是点击“保存更改”按钮后调用的方法。

如果我选中两个复选框,我得到的日志如下:

[ 2014-10-21 15:55:21.2754 21609/7f0234d4e700 Pool2/Implementation.cpp:1291 ]: [App 21695 stdout] {"utf8"=>"✓", "issue_checkbox"=>"1", "transfer_note_description"=>"description for the note here", "commit"=>"Save changes", "id"=>"trunk", "method"=>"get", "project_id"=>"sandbox", "action"=>"save", "controller"=>"polls"}

我无法向控制器发送注释说明和问题编号。

我没有模型,因为我不需要模型,因为所有数据都已经在 redmine 数据库中。当用户提交表单时,我希望控制器获取带有问题编号和问题说明描述的哈希。

我想尽可能多地使用rails,因此,如果可以避免的话,我不想使用javascript。

欢迎任何帮助。提前感谢您的宝贵时间。

【问题讨论】:

    标签: ruby-on-rails redmine redmine-plugins


    【解决方案1】:

    我犯了两个错误:

    1. 我选择了错误的表单助手;
    2. 我将表单数据分成不同的表格元素(尽管原始代码 sn-p 中未显示)。

    我的解决方案是替换以下表单助手:

    1. 从 checkbox_tag 到 check_box
    2. 从 label_tag 到 text_field_tag
    3. 从 text_area_tag 到 text_area

    进行这些更改后,我可以很容易地在控制器接收的参数上看到用户刚刚在视图上更改的所有表单数据。

    此外,最初的 form_tag 元素现在位于所有表格元素之外(我现在只有 1 个表格),这样我就可以捕获用户更改的所有表单字段,如下所示:

    <%= form_tag(controller: "polls", action: "save", project_id: @project, id: @version_selected_combo) do %>
     <table>
     (...)
     all the form elements ...
     (...)
     </table>
    <% end %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      • 2019-04-10
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多