我在名为FormsController的控制器中定义了以下强大的参数和新方法。用例是用户可以创建一种包含多个字段的表单。
class FormsController < ApplicationController
def index
@forms = Form.all
end
def new
@form = Form.new(form_params)
end
...
private
def form_params
params.require(:form).permit(:user, :name, :tag, :link, :repo, :hpx_id, :tmp_id)
end
end
并且当前使用以下新视图从用户那里获得一些输入
<div class="form">
<%= simple_form_for @param do |f| %>
<div class="form-inputs">
<%= render "hpx_select", name: "[form]hpx_id", selected: (params[:hpx_id]) %>
</div>
<div class="form-inputs";>
<%= f.input :user %>
<%= f.input :name %>
<%= f.input :tag %>
<%= f.input :link %>
<%= f.input :repo %>
</div>
<div class="form-inputs">
<%= f.select(:tmp_id, options_for_select(@forms.map{ |t| [t.name, t.id] })) %>
</div>
<div class="form-actions">
<%= f.button :submit, "Create", class: "btn-primary" %>
</div>
<% end %>
</div>
当我提交并创建新表单时,它会插入为:
SQL (6.4ms) INSERT INTO "forms" ("user", "name", "tag", "link", "repo", "hpx_id", "tmp_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["user", "value1"], ["name", "value2"], ["tag", "value3"], ["link", "value4"], ["repo", "value5"], ["hpx_id", "id1"], ["tmp_id", "id2"], ["created_at", "2020-06-13 20:54:41.366047"], ["updated_at", "2020-06-13 20:54:41.366047"]]
提交时的参数哈希看起来像:
{“user”=>”value1”, “name”=>”value2”, “tag”=>”value3”, “link”=>”value4”, “repo”=>”value5”, “hpx_id”=>”id1”, "tmp_id"=>”id2”}
从现在开始,是否可以将任何键或特定的哈希数组归因于表单参数以获取输出,如:
form_params => { 1 => {user: "user1" }, 2 => {user: "user2" }, 3 => {user: "user3" }.. }
form_params => { 1 => {name: "name1" }, 2 => {name: "name2" }, 3 => {name: "name3" }.. }
...
我想知道这样做的诀窍是否可以轻松地在下一步中复制表格。
此行是问题,it should look like this
@form = Form.new(form_params)