在ActiveRecord上使用post方法

时间:2015-01-12 07:08:36

标签: ruby-on-rails

Rob的总菜鸟。尝试为我的学生提供一个加入课程的链接(创建一个新的Attendance模型,该模型将存储class_session_idstudent_id)。

然而,我尝试过的东西给了我一个flash错误,它来自我的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不是零,current_student.admin?是假的。

这是我试过的:

<%= 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?" } %>

我还有一个测试通过了我正在尝试执行的同一项任务:

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

以下是我的参考模型

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:0x007f83e91cd7b0@/Users/michaelsnowden/mvgate/app/controllers/class_sessions_controller.rb:3> rendered or redirected
Filter chain halted as #<Proc:0x007f83e91cd7b0@/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)

我可以使用attendances / _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更复杂?

2 个答案:

答案 0 :(得分:0)

准备你的链接:(我假设你的控制器中有一个加入动作)

<%= link_to "Join", 
   join_class_sessions_path(class_session_id: class_session.id,
   student_id: current_student.id),  method: :post, 
   data: { confirm: "Are you sure you want to join this class?" } %>

如何构建方法:

actionname_controller_path(extra_attribute: value)
join_class_sessions_path(class_session_id: class_session.id)

#ClassSessionsController.rb

class_sessions_controller.rb    
 def join
   raise params.inspect 
   #here by raise you can see all your parameters and 
   #add your logic as per your need.
 end
end

答案 1 :(得分:0)

我有两个问题:

  • 错误日志显示我的路线错误,所以我在参数前添加了attendances_path
  • 我的参数缺少出勤率的关键

这解决了这两个问题,现在一切正常:

<%= link_to "Join", attendances_path({:attendance =>{ class_session_id: class_session.id, student_id: current_student.id}}), method: :post, data: { confirm: "Are you sure you want to join this class?" } %>