0

我从头开始制作了一个身份验证模型。

我想将一个模型(aula)与学生相关联,我正在使用 jquery-tokeninput 这样做。但是,我使用学生作为用户进行身份验证。

问题是由于某种原因,当我登录时,令牌在保存时给我一个错误。

prohibited this aula from being saved:

似乎只有当我把我登录的学生放在上面时,Aula 才能保存。

有什么建议吗?

这是jQuery:

   jQuery ->
 $('#aula_student_tokens').tokenInput('/students.json', {
 theme: 'facebook'
 prePopulate: $('#aula_student_tokens').data('load')
 preventDuplicates: true
 });

创建动作:

def create
  @aula = Aula.new(params[:aula])

  respond_to do |format|
    if @aula.save
      format.html { redirect_to @aula, notice: 'Aula was successfully created.' }
      format.json { render json: @aula, status: :created, location: @aula }
    else
      format.html { render action: "new" }
      format.json { render json: @aula.errors, status: :unprocessable_entity }
    end
  end
end

奥拉斯模型

class Aula < ActiveRecord::Base
  attr_accessible :name, :student_tokens

  has_many :grades
  has_many :students, :through => :grades, dependent: :destroy

  attr_reader :student_tokens

  def student_tokens=(tokens)
    self.student_ids = Student.ids_from_tokens(tokens)
  end

学生模型

class Student < ActiveRecord::Base
  attr_accessible :name, :password, :password_confirmation, :email

  has_many :grades      
  has_many :aulas, :through => :grades

  has_secure_password

  validates_uniqueness_of :email, :name

  def self.tokens(query)
    students = where("name like ?", "%#{query}%")
    if students.empty?
      [{id: "<<<#{query}>>>", name: "New: \"#{query}\""}]
    else
      students
    end
  end

  def self.ids_from_tokens(tokens)
    tokens.gsub!(/<<<(.+?)>>>/) { create!(name: $1).id }
    tokens.split(',')
  end

end

等级模型

class Grade < ActiveRecord::Base
  attr_accessible :grammar, :oral, :participation, :writing
  belongs_to :aula
  belongs_to :student
end

奥拉_form

<%= form_for(@aula) do |f| %>
  <% if @aula.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@aula.errors.count, "error") %> prohibited this aula from being saved:</h2>

      <ul>
      <% @aula.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :student_tokens, "Students" %><br />
    <%= f.text_field :student_tokens, data: {load: @aula.students} %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

会话控制器

class SessionsController < ApplicationController
  def new
  end

  def create
    student = Student.find_by_email(params[:email])
    if student && student.authenticate(params[:password])
      session[:student_id] = student.id
      redirect_to root_url, notice: "Logged in!"
    else
      flash.now.alert = "Emails or password is invalid"
      render "new"
    end
  end

  def destroy
    session[:student_id] = nil
    redirect_to login_path, notice: "Logged out!"
  end
end
4

0 回答 0