0

我遇到了一系列困扰我的问题!一种 Rails 新手,所以我无法弄清楚这一点。

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

我的 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 method“a”出现错误合并:String pointing to line<%= f.check_box :answers_expected, "a" %> in _form.html.erb, but it works when I changef.check_box tof.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 的理解。

谢谢。

编辑:

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

4

1 回答 1

0

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

<%= f.check_box :answers, {}, "a" %>

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

于 2017-01-06T18:26:19.883 回答