Rails - 简单形式 - 嵌套资源路径
Rails - Simple Form - Nested Resources paths
我正在尝试使用简单的形式在 Rails 4 中制作一个应用程序。
我有 3 个模型:项目,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 来回答问题。我有一个link,如下所示:
<%= link_to 'Answer this question', new_project_project_question_project_answer_path(:project_id => @project.id, :project_question_id => singleQuestion.id) %>
在附加问题的帮助下,我得到了到达 link 点的帮助:
我更改了项目答案模型,使项目问题有一个(而不是有多个项目答案)并更新了回答这个问题 link 中的路径,以单数形式而非复数形式。
当我单击 'Answer this question' link 时,出现此错误:
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 动作对于答案来说是复数形式,但我认为这与我收到的错误消息有关我点击'Answer this Question'link.
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
谁能看出我做错了什么?我读到 post 建议我在 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
您的 project_answers 控制器的 new
操作应该有 @project
、@project_question
定义
def new
@project_answer = ProjectAnswer.new
@project = Project.find(params[:project_id])
@project_question = ProjectQuestion.find(params[:project_question_id])
end
这样你就可以像这样使用那些
<%= simple_form_for [@project, @project_question, @project_answer] do |f| %>
我正在尝试使用简单的形式在 Rails 4 中制作一个应用程序。
我有 3 个模型:项目,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 来回答问题。我有一个link,如下所示:
<%= link_to 'Answer this question', new_project_project_question_project_answer_path(:project_id => @project.id, :project_question_id => singleQuestion.id) %>
在附加问题的帮助下,我得到了到达 link 点的帮助:
我更改了项目答案模型,使项目问题有一个(而不是有多个项目答案)并更新了回答这个问题 link 中的路径,以单数形式而非复数形式。
当我单击 'Answer this question' link 时,出现此错误:
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 动作对于答案来说是复数形式,但我认为这与我收到的错误消息有关我点击'Answer this Question'link.
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
谁能看出我做错了什么?我读到 post 建议我在 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
您的 project_answers 控制器的 new
操作应该有 @project
、@project_question
定义
def new
@project_answer = ProjectAnswer.new
@project = Project.find(params[:project_id])
@project_question = ProjectQuestion.find(params[:project_question_id])
end
这样你就可以像这样使用那些
<%= simple_form_for [@project, @project_question, @project_answer] do |f| %>