2015-06-22 67 views
0

我试图在Rails的应用程序4.的Rails的link_to路径

我有3种型号:项目,Project_Question,Project_Answer

的关联是:

项目:

has_many :project_questions 

并接受项目问题的嵌套属性。

项目问题:项目答案

belongs_to :project 
has_one :project_answer 

,并接受嵌套属性。

我的路线是嵌套如下:

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_question_id => project_question.id) %> 

我有我的路径文件名的路线,但我收到此错误信息:

undefined local variable or method `project_question' for #<#<Class:0x0000010742b9d8>:0x0000010f810b68> 

我应该走在括号?

查看:

<div class="containerfluid"> 
    <div class="row"> 
    <div class="col-md-10 col-md-offset-1"> 
     <% @project.project_questions.each do |singleQuestion| %> 

      <div class="categorytitle"> 
      <%= singleQuestion.title %> 

      </div> 
      <div class="generaltext"> 
      <%= singleQuestion.try(:content) %> 
      </div> 
      <span class="editproject"> 
      <% if current_user.id == @project.creator_id %> 
       <%= link_to 'Answer this question', new_project_project_questions_project_answer_path(:project_question_id => project_question.id) %> 

      <% end %> 
      </span> 
     <% end %> 

    </div> 
    </div> 
</div> 

项目问题控制器:

class ProjectQuestionsController < ApplicationController 
     before_action :set_project_question, only: [:show, :edit, :update, :destroy] 

     # GET /project_questions 
     # GET /project_questions.json 
     def index 
     @project_questions = ProjectQuestion.all 
     end 

     # GET /project_questions/1 
     # GET /project_questions/1.json 
     def show 


     end 

     # GET /project_questions/new 
     def new 
     @project_question = ProjectQuestion.new 
     @project = Project.find(params[:project_id]) 
     # @project_id = params[:project_id] 
     @project_question.project_answers[0] = ProjectAnswer.new 

     end 

     # GET /project_questions/1/edit 
     def edit 
     end 

     # POST /project_questions 
     # POST /project_questions.json 
     def create 
     @project_question = ProjectQuestion.new(project_question_params) 
     @project_question.project_id = project_question_params[:project_id] 


     respond_to do |format| 
      if @project_question.save 
      format.html { redirect_to project_url(Project.find(project_question_params[:project_id])), notice: 'Project question was successfully created.' } 
      format.json { render action: 'show', status: :created, location: @project_question } 
      else 
      format.html { render action: 'new' } 
      format.json { render json: @project_question.errors, status: :unprocessable_entity } 
      end 
     end 
     end 

     # PATCH/PUT /project_questions/1 
     # PATCH/PUT /project_questions/1.json 
     def update 
     respond_to do |format| 
      if @project_question.update(project_question_params) 
      format.html { redirect_to @project_question, notice: 'Project question was successfully updated.' } 
      format.json { head :no_content } 
      else 
      format.html { render action: 'edit' } 
      format.json { render json: @project_question.errors, status: :unprocessable_entity } 
      end 
     end 
     end 

     # DELETE /project_questions/1 
     # DELETE /project_questions/1.json 
     def destroy 
     @project_question.destroy 
     respond_to do |format| 
      format.html { redirect_to project_questions_url } 
      format.json { head :no_content } 
     end 
     end 

     private 
     # Use callbacks to share common setup or constraints between actions. 
     def set_project_question 
      @project_question = ProjectQuestion.find(params[:id]) 
     end 

     # Never trust parameters from the scary internet, only allow the white list through. 
     def project_question_params 
      params[:project_question].permit(:id, :title, :content, :project_id, :user_id, 
      project_answer_atttibutes: [:id, :answer, :project_question_id, :user_id] 
     ) 
     end 
    end 
+0

你定义的东西。您的视图中是否有任何具有“ProjectQuestion”实例的变量? –

+3

'new_project_project_question_project_answer_path' yuck ... – max

+0

我不知道这是什么意思?我在每个模型中都有一个属性,名为:id和每个belongs_to模型中具有parent_id的属性。我认为这就是为什么我应该使用项目问题ID。 – Mel

回答

1

当您运行rake routes,你会发现这一个

new_project_project_question_project_answer GET /projects/:project_id/project_questions/:project_question_id/project_answers/new(.:format)  project_answers#new 

这意味着它需要:project_id:project_question_id作为键。

这应该工作

<%= link_to 'Answer this question', new_project_project_question_project_answer_path(:project_id => @project.id, :project_question_id => singleQuestion.id) %> 

通知new_project_project_question_project_answer_pathnew_project_project_questions_project_answer_path

+0

喜帕,我想这个建议,但我得到这个错误:未定义的方法'new_project_project_questions_project_answer_path”为#<#<类别:0x0000010742b9d8>:0x000001074400e0> – Mel

+0

@ user2860931'new_project_project_question_project_answer_path'不'new_project_project_questions_project_answer_path' – Pavan

+0

非常感谢帕 – Mel

0

你的link_to应该是低于

<%= link_to 'Answer this question', new_project_project_questions_project_answer_path(:project_id => @project.id, :project_question_id => singleQuestion.id) %> 

查看看上去象这样

<div class="containerfluid"> 
    <div class="row"> 
    <div class="col-md-10 col-md-offset-1"> 
     <% @project.project_questions.each do |singleQuestion| %> 

      <div class="categorytitle"> 
      <%= singleQuestion.title %> 

      </div> 
      <div class="generaltext"> 
      <%= singleQuestion.try(:content) %> 
      </div> 
      <span class="editproject"> 
      <% if current_user.id == @project.creator_id %> 
       <%= link_to 'Answer this question', new_project_project_questions_project_answer_path(:project_question_id => singleQuestion.id) %> 

      <% end %> 
      </span> 
     <% end %> 

    </div> 
    </div> 
</div> 

检查PARAMS

def project_question_params 
    params[:project_question].permit(:id, :title, :content, :project_id, :user_id, 
      project_answer_atttibutes: [:id, :answer, :project_question_id, :user_id] 
     ) 
    end 

project_id

,你没有通过它link_to 所以thwos新的错误missing required keys: [:project_id]

+0

当我尝试:<%= link_to'回答这个问题',new_project_project_question_project_answer_path(:project_question_id => singleQuestion.id)%> 我得到这个错误:没有路线匹配{:action =>“new”,:controller =>“project_answers”,:id =>“70”,:project_question_id => 27}缺少必需的键:[:project_id] – Mel

+0

当我尝试在路径中使用多个'问题'时,出现此错误:undefined method'new_project_project_questions_project_answer_path 'for#<#:0x0000010f09b978> – Mel

+0

请发布控制器代码以及 –