首页 > 解决方案 > 嵌套路由中的新建/创建操作不起作用

问题描述

更新问题

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

查看/问题/new.html.erb

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

有用。然而,这不是传递用户的最佳方式。但我不知道该怎么做。

结束更新

原始问题

我正在建立一个用户可以创建项目的网站。每个项目包含几篇论文。每篇论文都可以通过回答几个问题来进行审查。

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

我被困在通过 simple_form_for ( view/questions/new.html.erb )new/create提交论文答案的行动中。基本上,一个 simple_form 来回答模型的问题。两者都成功通过,但不会发生新的评论创建。我不确定我做错了什么。Questionpaper.idproject.id

查看/问题/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

路由.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 在终端上路由输出

铁路路线

标签: ruby-on-rails

解决方案


推荐阅读