Rails Route Error - 新的路由不存在

时间:2015-06-19 14:31:07

标签: ruby-on-rails routes

我正在尝试制作rails 4应用程序。

我有一个项目模型和一个project_questions模型。

项目问题属于项目。

我已经制作了一个项目问题表单,用户可以用它来询问有关项目的问题。

我的麻烦是,当我按提交时,出现路线错误,表示没有匹配的路线。我不明白,因为我通过脚手架制作了模型,我理解这是自动制作主要路线。

我的结构是:

Project_question表单(使用简单形式gem):

  <%= simple_form_for :project_questions do |f| %>
          <%= f.input :project_id, as: :hidden, input_html: {value: @project_question_id} %>
          <%= f.input :title, label: 'Question:',  :label_html => {:class => 'question-title'}, placeholder: 'Type your question here', :input_html => {:style => 'width: 100%', :rows => 4, class: 'response-project'} %>
          <%= f.input :content, label: 'Is there any context or other information?', :label_html => {:class => 'question-title'}, placeholder: 'Context might help to answer your question', :input_html => {:style => 'width: 100%', :rows => 5, class: 'response-project'} %>

      <br><br><br>
          <%= f.button :submit, 'Send!', :class => "cpb" %>

<% end %>

路线:

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

项目问题的控制器:

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

项目控制器:

class ProjectsController < ApplicationController
  #layout :projects_student_layout

  before_action :authenticate_user!  

  # GET /projects
  # GET /projects.json
  def index
    @projects = current_user.projects


    #can i have more than one index? do i need to change something in the routes? if i want to list the related projects and the expiring projects - how do i do that within one index?

    #is there a way to order these, so that for educators they are in order of course and for students they are in order of next milestone date?
    #@projects.order("created_at DESC")
  end

  # def index2
 #    @projects = Project.find_xxx_xx
 #  end

  def list
    @projects = Project.find(:all)
  end

  def toggle_draft
    @project = Project.find(params[:id])
    @project.draft = true
    @project.save
    redirect_to project_path(@project)
  end

  # GET /projects/1
  # GET /projects/1.json
  def show
    #authorise @project

    @project = Project.find(params[:id])
    @creator = User.find(@project.creator_id)
    @creator_profile = @creator.profile

    #@approver_profile = User.find(@project.educator_id).profile #educators are the only people who approve projects
#    if profile == 'studnet'
    #@approval = @project.approval

 #   @invitations = @project.project_invitations

  end

  # GET /projects/new
  def new
    #authorise @project
    @project = Project.new
    @project.scope = Scope.new
    @project.scope.datum = Datum.new
    @project.scope.material = Material.new
    @project.scope.mentoring = Mentoring.new
    @project.scope.participant = Participant.new
    @project.scope.funding = Funding.new
    @project.scope.ethic = Ethic.new
    @project.scope.group_research = GroupResearch.new
    @project.scope.backgroundip = Backgroundip.new
    @project.scope.result = Result.new
    @project.scope.finalise = Finalise.new

  end

  # GET /projects/1/edit
  def edit
    #authorise @project
    @project =Project.find(params[:id])    
  end

  # POST /projects
  # POST /projects.json
  def create
    #authorise @project
    @project = Project.new(project_params)
    @project.creator_id = current_user.id
    @project.users << current_user
    respond_to do |format|
      if @project.save
        format.html { redirect_to @project }
        format.json { render action: 'show', status: :created, location: @project }
      else
        format.html { render action: 'new' }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /projects/1
  # PATCH/PUT /projects/1.json
  def update
    #authorise @project
    @project = Project.find(params[:id])
    @project.creator_id = current_user.id

    respond_to do |format|
      if @project.update(project_params)
        format.html { redirect_to @project }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /projects/1
  # DELETE /projects/1.json
  def destroy
    #authorise @project

    @project.destroy
    respond_to do |format|
      format.html { redirect_to projects_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.

    def project_params
      params.require(:project).permit(
      :id, :title, :description, :video_proposal, :link_to_video_proposal,
      :expiry_date_for_sponsor_interest,  :motivation, :approach,
      :completion_date, :start_date, :industry_id,  :recurring_project,
      :frequency, :date_for_student_invitation, :date_for_student_interest, :closed, :student_objective, 
      :industry_relevance, :hero_image, :project_id,
      project_question_attributes: [:title, :content, :user_id, :project_id,
      project_answer_attributes: [:answer, :project_question_id]],
      scope_attributes: [:id, :project_id, :data, :material, :mentoring, :participant, :funding, :ethic, :group, :result, :disclosure, :finalise,
                         :if_mentoring, :if_participant, :if_funding, :if_ethic, :if_group_research, :if_backgroundip, :if_datum, :if_material,
                         datum_attributes: [:id, :prim_sec, :qual_quant, :survey, :survey_link, :experiment, :other_type, :other_description,
                                           :confidential, :data_description, :scope_id],
                         material_attributes: [:id, :mattype, :description, :scope_id],
                         mentoring_attributes: [:id, :frequency, :description, :scope_id],
                         funding_attributes: [:id,  :expenses, :honorarium, :financing, :currency, :size, :amount_expenses, :amount_honorarium,
                                 :comment, :amount_principal_financing, :return_on_finance, :period_of_return, :expense_description, :amount_expenses_pennies, :amount_honorarium_pennies, :amount_principal_financing_pennies,
                                               :amount_expenses_currency, :scope_id],
                         participant_attributes: [:id,  :title, :description, :location, :costs, :participation_cost,
                                                   :eligibility, :eligibility_criteria, :currency, :participation_cost_pennies, :participation_cost_currency,
                                                   :location_specific ],
                         group_research_attributes: [:id, :number_of_group_members, :scope_id],
                         ethic_attributes: [:id, :obtained, :date_expected, :ethics_comment, :ethics_policy_link, :scope_id],
                         result_attributes: [:id, :report, :standard_licence, :bespoke_licence, :option, :assignment, :other_outcome,
                                             :consulting, :link_to_bespoke_licence, :description],
                         disclosure_attributes: [:id, :allusers, :publicity, :limitedorganisation, :limitedindustry, :limiteduser, :approveddisclosure],
                         backgroundip_attributes: [:id, :scope_id, :copyright, :design, :patent, :trademark, :geographical_indication,
                                                   :trade_secret, :other, :identifier_copyright, :identifier_design, :identifier_patent,
                                                   :identifier_trademark, :identifier_geographical_indication, :identifier_trade_secret,
                                                   :identifier_other, :description, :registered_owner, :unregistered_interest, :conditions,
                                                   :pbr, :identifier_pbr ],

                         finalise_attributes: [:id, :draft, :reminder, :reminder_date, :finalised_at, :scope_id]
                          ]
      )


    end

错误消息显示:

No route matches [POST] "/projects/70/project_questions/new"

我不知道在哪里制作路线或如何为新路线制作路线。我认为这是生成脚手架时自动化过程的一部分。有人能看出出了什么问题吗?

我发现SO上的另一个用户有嵌套路由问题。他们改变了控制器显示动作如下:

def show
  @user = User.find(params[:user_id])
  @album = @user.albums.find(params[:id])
  @photo = @album.photos.build
end

目前,我在项目问题中的表演行动是空的。但是,它属于我认为会涵盖它的项目。是否有一系列步骤可以使嵌套控制器工作?

谢谢

2 个答案:

答案 0 :(得分:0)

  

没有路线匹配[POST]&#34; / projects / 70 / project_questions / new&#34;

您已定义 嵌套路线 ,因此它应该是这样的

<%= simple_form_for [@project, @project_question] do |f| %>

答案 1 :(得分:0)

问题不在于你的route.rb文件在你的表单中。你没有正确地指出你应该去哪里。

将表单更改为以下内容。

<%= simple_form_for [@project, @project_question] do |f| %>
          <%= f.input :project_id, as: :hidden, input_html: {value: @project_question_id} %>
          <%= f.input :title, label: 'Question:',  :label_html => {:class => 'question-title'}, placeholder: 'Type your question here', :input_html => {:style => 'width: 100%', :rows => 4, class: 'response-project'} %>
          <%= f.input :content, label: 'Is there any context or other information?', :label_html => {:class => 'question-title'}, placeholder: 'Context might help to answer your question', :input_html => {:style => 'width: 100%', :rows => 5, class: 'response-project'} %>

      <br><br><br>
          <%= f.button :submit, 'Send!', :class => "cpb" %>

<% end %>

你应该将一个数组作为第一个参数传递给simple_form_for方法(负责设置正确的表单URL)