我在ruby上链接用户时遇到错误

时间:2013-07-24 23:05:40

标签: ruby-on-rails jquery-tokeninput

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

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

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

prohibited this aula from being saved:

似乎Aula只能保存,如果我把我登录的学生。

任何sugestions?

这是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

Aulas模特

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

Aula _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

0 个答案:

没有答案