简体   繁体   English

Rails链接路径

[英]Rails link_to path

I am trying to make an app in Rails 4. 我正在尝试在Rails 4中制作一个应用程序。

I have 3 models: Project, Project_Question, Project_Answer 我有3个模型:Project,Project_Question,Project_Answer

The associations are: 关联是:

Project: 项目:

has_many :project_questions 

and accepts nested attributes for project questions. 并接受项目问题的嵌套属性。

Project Question: 项目问题:

belongs_to :project
has_one :project_answer 

and accepts nested attributes for Project Answers. 并接受“项目答案”的嵌套属性。

My routes are nested as follows: 我的路线嵌套如下:

resources :projects do
  resources :project_questions do
    resources :project_answers
  end
end

In my Project Questions partial, I want a link to answer the question. 在我的“项目问题”部分中,我想要一个链接来回答问题。 I've tried: 我试过了:

  <%= link_to 'Answer this question', new_project_project_question_project_answer_path(:project_question_id => project_question.id) %>

I have a route with that name in my routes file, but I'm getting this error message: 我的路线文件中有一个具有该名称的路线,但出现此错误消息:

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

What should go in the brackets? 括号中应该包含什么?

View: 视图:

<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>

Project Question controller: 项目问题控制器:

  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

When you run rake routes , you will find this one 当您运行rake routes ,您会发现这一条

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

That means it requires :project_id and :project_question_id as keys. 这意味着它需要:project_id:project_question_id作为键。

This should work 这应该工作

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

Notice new_project_project_question_project_answer_path not new_project_project_questions_project_answer_path 请注意, new_project_project_question_project_answer_path不是new_project_project_questions_project_answer_path

Your link_to should be something below 您的link_to应该在下面

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

View will look like below 视图将如下所示

 <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>

Check that 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

There is project_id project_id

and you did not pass it in link_to so it thwos new error missing required keys: [:project_id] 并且您没有在link_to传递它,因此它出现了新错误, missing required keys: [:project_id]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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