我有一組問題正在做我的頭!一種Rails newb,所以我很難解決這個問題。Rails中的複選框 - 值選擇不顯示在'顯示',未定義的方法`合併'爲「a」:字符串
我有一個名爲'Answers_expected'的'問題'模型的字段,我希望'One'或'Multiple'有兩個選項,而不僅僅是普通的文本輸入。
我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>
對於我的服務器上的一些原因,我得到 「A」 的錯誤undefined method
合併」:字符串pointing to line
<%= f.check_box:answers_expected, 「A」 %>in _form.html.erb, but it works when I change
f.check_box to
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的理解有點混亂。
謝謝。
編輯:
改變了語法,但預期顯示頁面上的答案仍然顯示爲零?
使用此語法:'f.check_box:answers_expected,值:「a」' – MrYoshiji
謝謝。但它仍然顯示在顯示頁面上,預期答案= 0? – Annika