2015-06-19 54 views
0

我想製作一個rails 4應用程序。Rails路由錯誤 - 新的路由不存在

我有一個項目模型和一個project_questions模型。

項目問題屬於項目。

我已經提出了一個項目問題表單,用戶可以使用它來詢問關於項目的問題。

我的麻煩是,當我按下提交時,出現路徑錯誤,說沒有匹配的路線。我不明白這一點,因爲我通過腳手架來製作模型,而這些腳手架是我自動製作的主要路線。

我的結構是:

Project_question形式(用簡單的形式寶石):

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

目前,我在項目問題中的顯示操作爲空。但是,它屬於我認爲會覆蓋它的項目。是否有一系列步驟可以使嵌套控制器工作?

謝謝

+0

在點擊該鏈接,您得到這個錯誤? – Pavan

+0

使用'<%= simple_form_for @project_question do | f | %>'而不是'<%= simple_form_for:project_questions do | f | %>' – AbM

+0

你確定你沒有改變腳手架上的任何代碼嗎?似乎在<%= simple_form_for:project_questions do | f |中存在一個錯誤%>它應該是<%= simple_form_for @project_question do | f | %>或這個<%= simple_form_for [@project,@project_question] do | f | %> –

回答

0

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

你有嵌套定義航線,所以它應該是這樣的

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

'沒有路由匹配[POST]「/ projects/70/project_questions/new」問題是他的表單期望'POST'是新的,這應該是取而代之的。 –

+0

嗨帕文,我試過這個,謝謝我沒有想到改變路線,但我得到這個錯誤:不能寫未知的屬性'生成器' – Mel

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)

+0

當我嘗試這種方式,我得到一個錯誤,說:不能重定向到零!它指向我在我的控制器的創建操作中的重定向行,其中指出:format.html {redirect_to @project,notice:'項目問題已成功創建。' } – Mel