I want to create a web page where i can see my item with list of questions.
"items/1/questions"
in routes.rb i have
resources :items do
resources :questions
end
questions/index.html.erb
<div class="panel panel-default">
<div class="panel-heading">
<td><%= @item.title %></td>
</div>
<div class="panel-body">
<p><%= @item.description %></p>
<p><%= @item.price %></p>
</div>
</div>
<div class="panel-body"><%= render 'questions/form' %></div>
<div class="panel-body">
<% if @item.questions.any? %>
<%= render partial: 'questions/question', collection: @item.questions %> </div>
<% end %>
</div>
question/_question.html.erb
<p><%= question.body %></p>
Now i want to create an answer-option for admin, looks like the answer field in every question_partial.
So i create a model Answer, belongs_to :question
(in Question model has_one :answer
), question_id:integer, body:string
.
In answers_controller.rb ...
def new
@answer = Answer.new(answer_params)
end
def create
@question = Question.find(params[:question_id])
@answer = @question.build_answer(answer_params)
if @current_user == @admin
@answer.save
flash[:success] = "Your answer has been sent!"
redirect_to item_questions_path(@item)
else
flash[:errors] = "Your answer hasn't been sent!"
redirect_to item_question_path(@item)
end
end
def show
@answer = Answer.find(params[:id])
end
and trying to create a form_for @question.answer
question/_question.html.erb
<p><%= question.body %></p>
<% if @current_user = @admin %>
<%= form_for Answer.new do |f| %>
<p>
<%= f.text_field :body" %>
</p>
<p>
<%= f.submit "answer"%>
</p>
<% end %>
<% end %>
and get the error "undefined method `answers_path'"
I want to build item_page with questions-list where every question_partial should have: answer or answer_form (all on one page, 'items/:id/questions')
i think i have a problem in router (maybe in answers_controller too), but have no idea how could it organize.
did you have an idea?) ty