我正在尝试使用简单的形式在 Rails 4 中制作一个应用程序。
我有 3 个模型:Project、Project_Question、Project_Answer
这些协会是:
项目:
has_many :project_questions, dependent: :destroy#, through: :projects
accepts_nested_attributes_for :project_questions
项目问题:
belongs_to :project#, counter_cache: true
has_one :project_answer, dependent: :destroy
belongs_to :user
accepts_nested_attributes_for :project_answer
项目答案:
belongs_to :project_question#, counter_cache: true
belongs_to :user
我的路线嵌套如下:
resources :projects do
resources :project_questions do
resources :project_answers
end
end
在我的项目问题部分中,我想要一个链接来回答这个问题。我有一个链接如下:
<%= link_to 'Answer this question', new_project_project_question_project_answer_path(:project_id => @project.id, :project_question_id => singleQuestion.id) %>
在附加问题的帮助下,我得到了链接点的帮助:
http://stackoverflow.com/questions/30978447/rails-link-to-path
此后,我更改了项目答案模型,以便项目问题有一个(而不是有很多项目答案),并将“回答此问题”链接中的路径更新为单数而不是复数。
当我单击“回答此问题”链接时,出现以下错误:
undefined method `project_answers_path'
它指向项目答案表格中的简单表格。该表格具有:
<%= simple_form_for [@project, @project_question, @project_answer] do |f| %>
<%= f.input :project_question_id, as: :hidden, input_html: {value: @project_question.id} %>
<%= f.input :answer, label: 'Answer?', :label_html => {:class => 'question-project'}, placeholder: 'Type your answer here', :input_html => {:style => 'width: 650px', class: 'response-project'} %>
<br><br><br>
<%= f.button :submit, 'Send!', :class => "cpb" %>
<% end %>
当我为项目答案寻找路线时,我得到:
project_project_question_project_answers GET /projects/:project_id/project_questions/:project_question_id/project_answers(.:format) project_answers#index
POST /projects/:project_id/project_questions/:project_question_id/project_answers(.:format) project_answers#create
new_project_project_question_project_answer GET /projects/:project_id/project_questions/:project_question_id/project_answers/new(.:format) project_answers#new
edit_project_project_question_project_answer GET /projects/:project_id/project_questions/:project_question_id/project_answers/:id/edit(.:format) project_answers#edit
project_project_question_project_answer GET /projects/:project_id/project_questions/:project_question_id/project_answers/:id(.:format) project_answers#show
PATCH /projects/:project_id/project_questions/:project_question_id/project_answers/:id(.:format) project_answers#update
PUT /projects/:project_id/project_questions/:project_question_id/project_answers/:id(.:format) project_answers#update
DELETE /projects/:project_id/project_questions/:project_question_id/project_answers/:id(.:format) project_answers#destroy
我不明白为什么 project_project_questions_project_answers 操作的 #new 操作在只有一个答案时是复数,但我认为这与我单击“回答此问题”链接时收到的错误消息有关.
new_project_project_question_project_answer GET /projects/:project_id/project_questions/:project_question_id/project_answers/new(.:format) project_answers#new
e
当我尝试将表单的第一行复数化以获取项目答案时,对于项目问题,如下所示:
<%= simple_form_for [@project, @project_questions, @project_answer] do |f| %>
我收到此错误:
undefined method `project_answers_path' for #<#<Class:0x0000010a193f10>:0x0000010a3abb40>
当我尝试将项目答案、项目问题和项目答案的表单的第一行复数时,如下所示:
<%= simple_form_for [@project, @project_questions, @project_answers] do |f| %>
我收到此错误:
undefined method `model_name' for NilClass:Class
谁能看到我做错了什么?我读了一篇文章,建议我在 routes.rb 中使用 project_answer(单数)。我试过这个,但我得到一个错误,说没有路线匹配复数。
项目答案控制器:
class ProjectAnswersController < ApplicationController
before_action :set_project_answer, only: [:show, :edit, :update, :destroy]
# GET /project_answers
# GET /project_answers.json
def index
@project_answers = ProjectAnswer.all
end
# GET /project_answers/1
# GET /project_answers/1.json
def show
end
# GET /project_answers/new
def new
@project_answer = ProjectAnswer.new
end
# GET /project_answers/1/edit
def edit
end
# POST /project_answers
# POST /project_answers.json
def create
@project_answer = ProjectAnswer.new(project_answer_params)
respond_to do |format|
if @project_answer.save
format.html { redirect_to @project_answer, notice: 'Project answer was successfully created.' }
format.json { render action: 'show', status: :created, location: @project_answer }
else
format.html { render action: 'new' }
format.json { render json: @project_answer.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /project_answers/1
# PATCH/PUT /project_answers/1.json
def update
respond_to do |format|
if @project_answer.update(project_answer_params)
format.html { redirect_to @project_answer, notice: 'Project answer was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @project_answer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /project_answers/1
# DELETE /project_answers/1.json
def destroy
@project_answer.destroy
respond_to do |format|
format.html { redirect_to project_answers_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project_answer
@project_answer = ProjectAnswer.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_answer_params
params[:project_answer].permit(:answer, :project_question_id)
end
end