1

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

anndrew78
  • 196
  • 1
  • 20

2 Answers2

0

You are getting the error undefined method 'answers_path' because you don't have a route to the answer create action (or any answer actions). Add resources :answers to your route file underneath your other routes.

Ryan K
  • 3,985
  • 4
  • 39
  • 42
0

You need to define the route for the answer, following your approach of nested resources this could look like:

routes.rb

resources :items do 
 resources :questions do
   resource :answer
 end
end

The url will now look like /items/:item_id/questions/:question_id/answer which means there is no answer id in the url since it's a singular resource i.e a question can only have one answer. You also have to adapt your controller to fetch the question first and then access the answer like @question.answer

answers_controller.rb

def new
  # first find the question, then build an answer for it
  @question = Question.find(params[:question_id])
  @answer   = @question.build_answer
end

def show
  @question = Question.find(params[:question_id]) 
  # NOTE: @answer can be nil here if no answer has been created
  @answer   = @question.answer
end

In your view you got another typo, you used the assignment operator = for comparison. I've also changed the form_for helper to build the correct url.

question/_question.html.erb

<p><%= question.body %></p>
<% if @current_user == @admin %>
 <%= form_for [question.item, question, question.answer.present? ? question.answer : question.build_answer] do |f| %>
  <p>
    <%= f.text_field :body" %>
  </p>
  <p>
    <%= f.submit "answer"%>
  </p>
 <% end %>
<% end %>
sled
  • 14,525
  • 3
  • 42
  • 70
  • Thank you for help, especially for answers_controller. I did all your solutions and take a new error. undefined method `item_question_answers_path' – anndrew78 Jan 24 '15 at 22:19
  • make sure that you have the `resource` keyword in singular in your route e.g `resource :answer`, run `rake routes` in your command line to list all available routes and append the relevant routes to your question. – sled Jan 24 '15 at 22:24