Rails - 找不到'id'=的学生

时间:2016-04-01 05:21:31

标签: ruby-on-rails ruby

当我尝试为“学生”创建“标记”时,我收到了上述错误。在创建新标记时,我无法弄清楚如何传递: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

这可能是非常愚蠢的事情,但如果有人能解释这个问题,我会非常感激。

由于

1 个答案:

答案 0 :(得分:1)

我不建议您为此目的使用隐藏字段。 您应该将学生与mark一起传递给form_for帮助者,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