레일에 멍청한 놈. 학생들이 수업에 참여할 수있는 링크를 제공하려고합니다 (class_session_id
및 student_id
을 저장할 Attendance
모델을 새로 만듭니다). 그러나ActiveRecord의 post 메서드 사용
, 제가 시도한 것은 나에게 내 class_sessions_controller이 코드에서 제공되는 플래시 오류, 제공 : 나는 새로운 전화 했어 방법 그러나
before_action :authenticate_user!, only: :update
before_action :only => [:new, :create, :edit, :destroy] do
if !current_student || !current_student.admin?
flash[:alert] = "You must be logged in as an administrator to control class sessions"
redirect_to :class_sessions
end
end
before_action :set_class_session, only: [:show, :edit, :update, :destroy]
를, 내가 볼 수없는, 생성, 편집 , 또는 클래스 세션에서 파괴하십시오. 그건 그렇고, 내가 비 관리자로 로그인했기 때문에 current_student
은 0이 아니며 current_student.admin?
은 false입니다. 여기
test "students can make attendances for themselves" do
assert_difference('Attendance.count', 1) do
sign_in students(:not_an_admin)
post :create, attendance: { class_session_id: class_sessions(:one).id, student_id: students(:not_an_admin).id }
end
assert_redirected_to attendance_path(assigns(:attendance))
end
참조
내 모델입니다 :
<%= link_to "Join", {class_session_id: class_session.id, student_id: current_student.id}, method: :post, data: { confirm: "Are you sure you want to join this class?" } %>
나는 또한 내가 지금 수행하기 위해 노력하고있어이 같은 작업을 위해 전달 된 검사를
class Attendance < ActiveRecord::Base
belongs_to :student
belongs_to :class_session
end
class ClassSession < ActiveRecord::Base
has_many :attendances
has_many :students, :through => :attendances
end
class Student < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :attendances
has_many :class_sessions, :through => :attendances
end
콘솔이 당시 말한 내용은 다음과 같습니다.
Started POST "/class_sessions?class_session_id=5&student_id=9" for ::1 at 2015-01-12 11:09:57 -0800
Started POST "/class_sessions?class_session_id=5&student_id=9" for ::1 at 2015-01-12 11:09:57 -0800
Processing by ClassSessionsController#create as HTML
Processing by ClassSessionsController#create as HTML
Parameters: {"authenticity_token"=>"3VN01Qj1HV0iqoOdV5c/SwW/6ONhDdcIA59xA38/JR8K3xVfRF8/GwnQ80jwLnfXKu6xlRA91qhPK9ECJsxLTQ==", "class_session_id"=>"5", "student_id"=>"9"}
Parameters: {"authenticity_token"=>"3VN01Qj1HV0iqoOdV5c/SwW/6ONhDdcIA59xA38/JR8K3xVfRF8/GwnQ80jwLnfXKu6xlRA91qhPK9ECJsxLTQ==", "class_session_id"=>"5", "student_id"=>"9"}
Student Load (0.5ms) SELECT "students".* FROM "students" WHERE "students"."id" = $1 ORDER BY "students"."id" ASC LIMIT 1 [["id", 9]]
Student Load (0.5ms) SELECT "students".* FROM "students" WHERE "students"."id" = $1 ORDER BY "students"."id" ASC LIMIT 1 [["id", 9]]
Redirected to http://localhost:3000/class_sessions
Redirected to http://localhost:3000/class_sessions
Filter chain halted as #<Proc:[email protected]/Users/michaelsnowden/mvgate/app/controllers/class_sessions_controller.rb:3> rendered or redirected
Filter chain halted as #<Proc:[email protected]/Users/michaelsnowden/mvgate/app/controllers/class_sessions_controller.rb:3> rendered or redirected
Completed 302 Found in 4ms (ActiveRecord: 0.5ms)
Completed 302 Found in 4ms (ActiveRecord: 0.5ms)
나는 쉽게 출석/_form.html.erb 사용하는 학생 및 클래스 세션 관리자로 새로운 출석 만들 수 있습니다
<%= simple_form_for(@attendance) do |f| %>
<% if @attendance.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@attendance.errors.count, "error") %> prohibited this attendance from being saved:</h2>
<ul>
<% @attendance.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :student_id, collection: Student.all, label_method: :email %>
<%= f.input :class_session_id, collection: ClassSession.all, label_method: :short_description %>
<% end %>
을 그리고 난이 콘솔에서 작동하는지 테스트했습니다
s = Student.first # Me, the admin
s.attendances.first # #<Attendance id: 3, class_session_id: 5, student_id: 8, ... >
s.class_sessions.first # <ClassSession id: 5, ...>
통해이 잘못되는 LINK_TO의 게시물에 대한 내 구문만큼이나 간단하다, 또는 내 class_sessions_controller을 포함하는 더 복잡한 뭔가?
무엇이 오류입니까? –
link_to 도우미에서 route_path를 전달하지 않았으므로 이것이 문제가 될 수 있습니다.오류 로그를 게시 할 수 있으면 문제를 쉽게 디버그 할 수 있습니다. –
@MarekLipka 오류 로그를 추가했습니다. – michaelsnowden