【问题标题】:Rails 6 change params to hash of hashesRails 6 将参数更改为哈希值
【发布时间】:2021-07-08 12:12:52
【问题描述】:

我有一个应用程序,用户必须在其中填写调查问卷。我需要将用户的答案存储在 TestResult 模型中,该模型只有一个字段 answers:string

通过当前的实现,我从表单中获取参数:

params => {
  {
    "question_#{id}": "some answer 1",
    "question_#{id}": "some answer 12345",
  }
}

我想改成下面的结构:

# expected hash params
params => {
  {
    question: 'first question',
    answer: 'some answer 1'
  },
  {
    question: 'second question',
    answer: 'some answer 123431'
  }
}

我应该改变什么(可能在视图中)来获得这个哈希?

new.html.erb

<%= simple_form_for :test_results, url: test_results_path  do |f| %>
  <% @randomize_questions.map do |q| %>
    <%= q[:question] %>
    <%= f.input "question_#{q[:id]}", collection: q[:answers], as: :radio_buttons %>
  <% end %>
  <%= f.button :submit %>
<% end %>

控制器:

class TestResultsController < ApplicationController
  before_action :fetch_random_questions, only: [:new, :create]

  def new
    @test_result = TestResult.new
  end

  def create
    @test_result = TestResult.new(
      answer: test_result_params,
    )

    @test_result.save
    redirect_to dummy_path
    end
  end

  private

  def test_result_params
    params.require(:test_results).permit!
  end

  def fetch_random_questions
    TestQuestion.where(published: true).order('RANDOM()')

    @randomize_questions = test_questions.map do |obj|
      {
        id: obj.id,
        question: obj.question,
        answers: [obj.correct_answer, obj.other_answer1, obj.other_answer2, obj.other_answer3],
      }
    end
  end
end

测试结果模型

类TestResult

【问题讨论】:

  • TestResult 看起来如何?它是 question_id 和 answer_id 的组合还是文本?
  • @Joel_Blum 问题已更新,它只是一个表字段 answer 我想将所有答案存储为哈希

标签: ruby-on-rails


【解决方案1】:

参数从输入名称中获取他的结构。

因此您可以为问题添加一个隐藏字段,然后为您的两个字段指定一个名称。

<%= simple_form_for :test_results, url: test_results_path  do |f| %>
  <% @randomize_questions.map do |q| %>
    <%= q[:question] %>
    <%= f.input "question_#{q[:id]}", as: :hidden, input_html: { name: "test_results[#{q[:id]}][question]", value: q[:question] }  %>
    <%= f.input "question_#{q[:id]}", collection: q[:answers], as: :radio_buttons, input_html: { name: "test_results[#{q[:id]}][answer]" } %>
  <% end %>
  <%= f.button :submit %>
<% end %>

参数应该如下所示:

params => {
  test_result: {
    1 => {
      question: "...",
      answer: "..."
    },
    2 => {
      question: "...",
      answer: "..."
    }
  }
}

未测试。您能告诉我这是否适合您吗?

【讨论】:

  • 使用你的代码我得到一个错误:ActionController::ParameterMissing in TestResultsController#create param is missing or the value is empty: test_results
  • 应该是输入名称。我错过了's'。 => name: 'test_results[#{q[:id]}][question]name: 'test_results[#{q[:id]}][answer]'
  • 嗯...这不是我所期望的,似乎每个新的 test_result 都是哈希内的 ActionController::Parameters 对象。看看:{"1"=&gt;#&lt;ActionController::Parameters {"answer"=&gt;"Est voluptate quo.", "question"=&gt;"question of category1"} permitted: true&gt;, "2"=&gt;#&lt;ActionController::Parameters {"answer"=&gt;"", "question"=&gt;"question of category2"} permitted: true&gt;, "8"=&gt;#&lt;ActionController::Parameters {"answer"=&gt;"", "question"=&gt;"question of category3"} permitted: true&gt;, (...)
  • @mr_muscle ActionController::Parameters 实例实现了to_hash 方法,因此您应该能够将它们传递到期望散列的位置,并且它们将自动转换为散列。如果您将它传递给您自己的方法之一,请确保调用to_hash,如果您期望哈希并且提供的参数不是哈希。或者,您可以使用 .transform_values(&amp;:to_h) 转换它们
  • 好的,可以了!但我仍然不知道问题 id 来自哪里。我的意思是当我有 params => test_result: { 1 => { question: "...", answer: "..." }, 23 => { ... }, }` 整数(1 和23)都对应q[:id]但是怎么来的呢?
猜你喜欢
  • 2017-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-22
  • 2016-05-27
  • 1970-01-01
  • 2021-04-21
  • 1970-01-01
相关资源
最近更新 更多