ruby-on-rails - 嵌套路由中的新建/创建操作不起作用
问题描述
更新问题
我想我知道问题出在哪里:我没有让用户提问。但是我不知道该怎么做。这肯定是问题,因为如果我写
查看/问题/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 来回答模型的问题。两者都成功通过,但不会发生新的评论创建。我不确定我做错了什么。Question
paper.id
project.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 在终端上路由输出
解决方案
推荐阅读
- python - 登录时无法摆脱网站抛出的某个障碍
- windows - 错误:代码="VMExtensionProvisioningError" JsonADDomainExtension
- c# - 如何识别 RDP/MSTSC 登录失败的时间?
- r - 如何在 R 中在 2 个分类变量之间执行 t 检验(是/否)
- c# - 如何使用应用控制器关闭表单?
- c# - 使用多对多到 LINQ 的 SQL 表达式
- angular - 在角度条件下比较环境中的值将始终返回 true
- python - 大熊猫中比较绘制不同二元分类器的 ROC 曲线的最简单方法是什么?
- python - 在 python 中以 numpy.eig 和 scipy.eig 排序的特征值
- python - 关于python模块并从中调用函数的问题