简体   繁体   中英

Error with path and Controller in Ruby on Rails

In my application,i have 1-n relation between Users and Questions,1-n relation between Questions and Answers,and 1-n relation between Users and Questions.
My routes:

  question_answers GET /questions/:question_id/answers(.:format) answers#index POST /questions/:question_id/answers(.:format) answers#create new_question_answer GET /questions/:question_id/answers/new(.:format) answers#new edit_question_answer GET /questions/:question_id/answers/:id/edit(.:format) answers#edit question_answer GET /questions/:question_id/answers/:id(.:format) answers#show PATCH /questions/:question_id/answers/:id(.:format) answers#update PUT /questions/:question_id/answers/:id(.:format) answers#update DELETE /questions/:question_id/answers/:id(.:format) answers#destroy 

My controller:

 class AnswersController < ApplicationController before_action :authenticate_user!,only: [:edit,:update,:destroy,:new,:create] before_action :set_answer, only: [:show, :edit, :update, :destroy] respond_to :html def index @answers = Answer.paginate(page: params[:page]) #@answers = Answer.all respond_with(@answers) end def show respond_with(@answer) end def new @questions=Question.find params[:question_id] @answer = Answer.new user: current_user respond_with(@answer) end def create @answer = Answer.new(answer_params) @answer.save respond_with(@answer) end end 

And when i run program with this link: http://127.0.0.1:3000/questions/1/answers/new :
I got this error: undefined method `answers_path'. So what is my error??I thinh i need change answers_path to questions_answer path but how to do this?
Edit: My routes:

 Rails.application.routes.draw do resources :questions do resources :answers end end 

And when i run this link: http://127.0.0.1:3000/questions/1/answers
i got this error:'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.

For a start, in the routes file you are stating that your answers resources all live within the scope of a parent question, but you are only modelling that within AnswersController#new – and even then, the @answer object you have created bears no relation to the question object.

At the very least, you should define @answer using the has_many collection's build method, so that it is defined as belonging to the question, eg:

@answer = @question.answers.build(user: current_user)

You should adjust your other controller actions as well, to ensure that the appropriate answer action is scoped at all times to the parent question resource.

You may also need to adjust your respond_with calls, so that Rails' form helpers can correctly identify the controller path to set for the form's action. You need to provide the nested resource's parent as well, so that the responders code knows to created a nested URL:

respond_with(@question, @answer)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM