【问题标题】:Checkboxes in Rails - Value chosen not displaying in 'Show', undefined method `merge' for "a":StringRails 中的复选框 - 选择的值未显示在“显示”中,“a”的未定义方法“合并”:字符串
【发布时间】:2017-01-07 02:26:19
【问题描述】:

我遇到了一系列问题!有点像 Rails 新手,所以我很难弄清楚。

我的“问题”模型有一个名为“Answers_expected”的字段,我希望它有两个选项,“一个”或“多个”,而不仅仅是普通的文本输入。

My Questions_controller.rb(相关行):

before_action :set_answers_expected 
  private
# Use callbacks to share common setup or constraints between actions.
def set_question
  @question = Question.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def question_params
  params.require(:question).permit(:title, :brief, :idea_id, :user_id, :answers_expected)
end

def set_answers_expected
  @answers_expected = [
    "One",
    "Multiple"
  ]
end

Questions.rb(相关行):

validates :answers_expected, presence: true

_form.html.erb:

<%= form_for(@question) do |f| %>
  <% if @question.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@question.errors.count, "error") %> prohibited this question from  being saved:</h2>

      <ul>
      <% @question.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field form-group">
    <%= f.label :title, "Question" %><br>
    <%= f.text_area :title, class:"form-control" %>
  </div>
  <div class="field form-group">
    <%= f.label :brief, "Description" %><br>
    <%= f.text_area :brief, class:"form-control" %>
  </div>
  <div class="field form-group">
    <%= f.label :answers_expected %><br>
        <% @answers_expected.each do |a| %>
          <%= f.label :a, class: 'checkbox' do %>
            <%= f.check_box :answers_expected, "a" %>
            <%= a %>
          <% end %>
        <% end %>
  </div>

我的问题 show.html.erb:

div class="question">
  <p>
    <strong>Question:</strong>
    <%= @question.title %>
  </p>
  <p>
    <strong>Brief:</strong>
    <%= @question.brief %>
  </p>
  <p>
    <strong>Answers expected:</strong>
    <%= @question.answers_expected %>
  </p>

由于某种原因,在我的服务器上,我收到一个错误 undefined methodmerge' for "a":Stringpointing to linein _form.html.erb, but it works when I changef.check_box@987654328 @f.radio_button`;为什么?

然而,即使使用 radio_button,服务器上的 Answers Expected 显示仍持续显示为零,尽管我单击了什么,我也不明白。

还写了规范,希望在这里为我指明正确的方向:

question_spec.rb:

scenario 'with valid params' do
  click_link 'Create a question'
  fill_in 'Question', with: 'Valid question'
  fill_in 'Description', with: 'This is valid description for the question.'
  check 'One'
  click_button 'Create Question'
  page.should have_content 'One'
  expect(page).to have_content('Question was successfully created.')
end

我的session_helper.rb 拥有:

def submit_question(title = 'valid title', brief = 'valid brief')
  click_link 'Create a question'
  fill_in 'Question', with: title
  fill_in 'Description', with: brief
  check 'One'
  click_button 'Create Question'
end

还有我失败的规范:

1) Visitor submits a question with valid params
 Failure/Error: page.should have_content 'One'
   expected to find text "One" in "Toggle navigation Labthi.ng Home Explore Recent    Activity Example User Sign out × Question was successfully created. 0 Title: Valid Title Phase: 1 Brief: Valid brief for an idea Image: Active: true Components: App Categories: Other User: Example User Direct Define Reputation Activity Question was successfully created. Question: Valid question Brief: This is valid description for the question. Answers expected: 0 Idea: Valid brief for an idea User: Example User No comments. Add comment No answers yet. Why don't you add one ? Add answer Edit | Back"
 # ./spec/features/question_spec.rb:16:in `block (2 levels) in <top (required)>'

非常困惑!这有点影响我对 Rails 的理解。

谢谢。

编辑:

将语法更改为 &lt;%= f.check_box :answers_expected, value: "a" %&gt; ,但显示页面上预期的答案仍显示为零?

【问题讨论】:

  • 使用这个语法:f.check_box :answers_expected, value: "a"
  • 谢谢。但它仍然显示在显示页面上,答案预期 = 0?

标签: ruby-on-rails checkbox capybara


【解决方案1】:

使用check_box 表单助手设置值的正确方法是:

&lt;%= f.check_box :answers, {}, "a" %&gt;

:answers 是字段名称
{} 是您放置 html 属性(如 class 或 id)的位置
"a" 将是值:)

【讨论】:

    猜你喜欢
    • 2021-05-21
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 2014-08-10
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多