'학생'에 대한 '마크'를 만들려고 할 때 위의 오류가 발생합니다. 새 마크를 만들 때 : student_id를 전달하는 방법을 알 수 없습니다.
노선
Rails.application.routes.draw do
resources :students do
resources :marks
end
resources :marks
root 'students#index'
마크 컨트롤러
class MarksController < ApplicationController
def create
@student = Student.find(params[:student_id])
@mark = @student.marks.create(params[:input1, :input2, :input3, :weight1, :weight2, :weight3, :mark1, :mark2, :mark3, :final_mark].permit(:input1, :input2, :input3, :weight1, :weight2, :weight3, :mark1, :mark2, :mark3, :final_mark))
@mark.save
if @mark.save
redirect_to student_path(@student)
else
render 'new'
end
end
def new
@mark = Mark.new
end
private
def set_mark
@mark = Mark.find(params[:id])
end
end
학생 쇼보기
<p id="notice"><%= notice %></p>
<p>
<strong>Student Number</strong>
<%= @student.StudentNumber %>
</p>
<p>
<strong>Project Title</strong>
<%= @student.ProjectTitle %>
</p>
<p>
<strong>Project PDF</strong>
<%= @student.ProjectTitle %>
</p>
<p>
<strong>Reader 1</strong>
<%= @student.Reader1 %>
</p>
<p>
<strong>Reader 2</strong>
<%= @student.Reader2 %>
</p>
<h3> <%= link_to 'Add Mark', new_student_mark_path(@student), class:"btn btn-warning"%> </h3>
<p>
<strong>Reader 3</strong>
<%= @student.Reader3 %>
</p>
<%= link_to 'Edit', edit_student_path(@student) %> |
<%= link_to 'Back', students_path %>
마크 양식
<%= form_for @mark, html: {multipart: true} do |f| %>
<% if @mark.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@mark.errors.count, "error") %> prohibited this grading from being saved:</h2>
<ul>
<% @mark.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label 'Introduction' %><br>
<%= f.text_area :input1 %>
<%= f.number_field :weight1 %>
<%= f.number_field :mark1 %>
</div>
<div class="field">
<%= f.label 'Main' %><br>
<%= f.text_area :input2 %>
<%= f.number_field :weight2 %>
<%= f.number_field :mark2 %>
</div>
<div class="field">
<%= f.label 'Conclusion' %><br>
<%= f.text_area :input3 %>
<%= f.number_field :weight3 %>
<%= f.number_field :mark3 %>
</div>
<div class="actions">
<%= f.submit class:"btn-xs btn-success"%>
</div>
<% end %>
마크 모델
class Mark < ActiveRecord::Base
belongs_to :student
end
학생 모델
class Student < ActiveRecord::Base
has_many :marks
has_attached_file :document
validates_attachment :document, :content_type => { :content_type => %w(application/pdf) }
end
아마 정말 어리석은 일이지만 누군가가 문제를 설명 할 수 있다면 정말 감사 할 것입니다.
감사
이 목적으로 숨겨진 필드를 사용하지 않는 것이 좋습니다. mark와 함께 student를 form_for helper에 전달해야하며 rails는 다음과 같은 적절한 URL을 생성합니다. / students / : student_id / marks이 경우 나중에 작업의 params에서 student_id를 추출 할 수 있습니다.
form_for [@student, @mark], html: {multipart: true} do |f|
중첩 된 리소스에 대한 추가 정보 :
http://guides.rubyonrails.org/routing.html#nested-resources
http://www.informit.com/articles/article.aspx?p=1671632&seqNum=7
https://gist.github.com/jhjguxin/3074080
최신 정보:
이 작업을 수행하려면 새 작업에서 학생 인스턴스를 템플릿에 전달해야합니다.
def new
@student = Student.find(params[:student_id])
@mark = @student.marks.build
end
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다