Rails 4 - 路径

时间:2015-06-19 12:13:41

标签: ruby-on-rails

我想弄清楚轨道4.

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

项目问题属于项目。

我想要一个在提交问题后重定向到项目页面的表单。我不断被引导回到项目列表,而不是询问问题的具体项目。我正在努力弄清楚如何学习铁轨。

在我的project_question_form中,我有:

<div class="containerfluid">
  <div class="row">
    <div class="col-md-10 col-md-offset-1">

      <div class="header-project">What would you like to know?</div>

      <%= render 'form' %>
      <div class="formminor">
      <%= link_to 'Back', projects_path(@project) %>
      </div>
    </div>
  </div>
</div>

我希望“后退”链接中的括号位回到用户点击“提出问题”时所在的项目页面。相反,用户将被带回列出所有项目索引的页面。

在我问一个问题表格时,我有:

<div class="containerfluid">
  <div class="row">
    <div class="col-md-10 col-md-offset-1">
      <%= 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 %>
    </div>
  </div>
</div>

在我的routes.rb中,我有:

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

谁能看到我做错了什么?我尝试在第一种形式中使用(@project_id)和(@ project.id)代替(@project)。这些替代方案都不起作用。

我的projects_controller有:

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 }, flash[:notice] = "Project saved"
        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

当我捆绑exec rake路线时(如下所示),我为所有模型获得了很多路线。包含project_question的是:

project_answers#destroy
                    project_project_questions GET      /projects/:project_id/project_questions(.:format)                                               project_questions#index
                                              POST     /projects/:project_id/project_questions(.:format)                                               project_questions#create
                 new_project_project_question GET      /projects/:project_id/project_questions/new(.:format)                                           project_questions#new
                edit_project_project_question GET      /projects/:project_id/project_questions/:id/edit(.:format)                                      project_questions#edit
                     project_project_question GET      /projects/:project_id/project_questions/:id(.:format)                                           project_questions#show
                                              PATCH    /projects/:project_id/project_questions/:id(.:format)                                           project_questions#update
                                              PUT      /projects/:project_id/project_questions/:id(.:format)                                           project_questions#update
                                              DELETE   /projects/:project_id/project_questions/:id(.:format)                                           project_questions#destroy

乍得的路线建议:

projects GET      /projects(.:format)                                                                             projects#index
                                              POST     /projects(.:format)                                                                             projects#create
                                  new_project GET      /projects/new(.:format)                                                                         projects#new
                                 edit_project GET      /projects/:id/edit(.:format)                                                                    projects#edit
                                      project GET      /projects/:id(.:format)                                                                         projects#show
                                              PATCH    /projects/:id(.:format)                                                                         projects#update
                                              PUT      /projects/:id(.:format)                                                                         projects#update
                                              DELETE   /projects/:id(.:format)                                                                         projects#destroy

我的project_question控制器:

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

此链接位于我的项目展示页面中:

  <%= link_to 'Ask a question', new_project_project_question_path(:project_id => @project.id) %> <% end %>

5 个答案:

答案 0 :(得分:0)

您应该使用project_path(@project)代替projects_path(@project)

答案 1 :(得分:0)

我建议运行的最好的事情 var comp = jasmineReact.render(<Comp />); expect(comp.state.something).toBe('else');

然后确定您的路径名称和所需参数。根据您的问题,我们无法直接回答正确的路径。所以最佳做法是使用rake路线。

答案 2 :(得分:0)

你能试试吗

project_path(@project_question_id)

答案 3 :(得分:0)

首先,试试:

project_path(@project_id)

然后,如果这不起作用,那么您需要确保当您从项目页面转到询问问题页面时,您将@project.id作为名为{{1}的参数传递}。

答案 4 :(得分:0)

您必须在@project的{​​{1}}操作中定义new,如下所示

ProjectQuestionsController

并将您的def new @project_question = ProjectQuestion.new @project = Project.find(params[:project_id]) #here @project_question.project_answers[0] = ProjectAnswer.new end 更改为

link_to