嵌套路线中的新建/创建动作不起作用

时间:2019-02-03 19:00:59

标签: ruby-on-rails

更新问题

我想我知道问题出在哪里:我没有让用户提出问题。但是我不确定该怎么做。 这是肯定的问题,因为如果我写

view / questions / new.html.erb

 <%= f.association :user, label: "Which user is creating it?",
:as => :hidden, :input_html => { :value => current_user.id }  %>

有效。但是,这不是通过用户的最佳方法。但是我不知道该怎么做。

END UPDATE

原始问题

我正在建立一个用户可以在其中创建项目的网站。每个项目包含几篇论文。可以通过回答几个问题来复习每篇论文。

我有四个模型(用户,项目,论文,问题)。

我被困在new/create操作中,无法通过simple_form_for( view / questions / new.html.erb )将答案提交给论文。基本上,一个simple_form回答模型Question的问题。 paper.idproject.id均已成功通过,但新创建的审阅未发生。我不确定自己在做什么错。

view / questions / new.html.erb

  <div class="container">
  <h4>You are reviewing the questions for paper paper:</h4>

    <div class="row ">
      <div class="col-sm-6 col-sm-offset-3">

        <%= simple_form_for [@project, @paper, @question] do |f| %>
        <%= f.input :question_1, :collection =>["N/A", "No - 0", "Partially - 0.5", "Yes - 1"], label: "Question 1" %>
        <%= f.input :question_2, :collection =>["N/A", "No - 0", "Partially - 0.5", "Yes - 1"], label: "Question 2" %>
        <%= f.input :question_3, :collection =>["N/A", "No - 0", "Partially - 0.5", "Yes - 1"], label: "Question 3" %>

        <div class="form-actions">
          <%= f.button :submit, "Send your review" %>
        </div>
      </div>
      <% end %>
    </div>
  </div>

questions_controllers.rb

  def new
    @project = Project.find(params[:project_id])
    @paper = Paper.find(params[:paper_id])
    @question = Question.new
  end

  def create
    @question = Question.new(question_params)
    @question.paper = Paper.find(params[:paper_id])
    @question.project = Project.find(params[:project_id])
    if @question.save
      redirect_to projects_path
    else
      render :new
    end

user.rb-模型

class User < ApplicationRecord

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  has_many :projects
  has_many :questions
end

project.rb-模型

  belongs_to :user
  has_many :papers, dependent: :destroy
  has_many :questions

paper.rb-模型

class Paper < ApplicationRecord
  belongs_to :project
  has_many :questions
  has_one_attached :paper_pdf
end

question.rb-模型

class Question < ApplicationRecord
  belongs_to :user
  belongs_to :paper
  belongs_to :project
end

route.rb

Rails.application.routes.draw do
  resources :projects do
    resources :papers do
      resources :questions
    end
  end
  devise_for :users
  root to: 'pages#home'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

跟踪在终端上输出的路线

Rails routes

0 个答案:

没有答案
相关问题