私は頭を悩ませている一連の問題を抱えています!Railsの初心者なので、これを理解するのに苦労しています。
「Answers_expected」と呼ばれる「質問」モデル用のフィールドがあります。通常のテキスト入力ではなく、「1 つ」または「複数」の 2 つのオプションが必要です。
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 method
"a":String 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, value: "a" %>
に変更しましたが、ショー ページで期待される回答がまだゼロとして表示されていますか?