无法设置运营商

时间:2010-12-09 06:46:45

标签: ruby-on-rails activerecord authlogic

我正在尝试为我的新用户设置运营商,但是当我提交时,我会收到此错误

  

承运人(#37295244)预计,得到了   串(#15496392)

<div class= "field">
      <%= f.label :carrier %><br>
      <%= f.collection_select(:carrier, Carrier.find(:all) ,:name, :name) %>
    </div>

的usermodel

class User < ActiveRecord::Base

  acts_as_authentic
  after_create :set_sub
  after_create :set_universal
  after_create :set_carrier

  def set_sub
    #self.roles << "subscriber"
    self.roles_mask = 4
  end

  def set_universal
      self.channels << Channel.find(1)
  end

  def set_carrier
    self.carriers<< self.carrier_name
  end

  ROLES = %w[admin  moderator subscriber]

  #Each user can subscribe to many channels
  has_and_belongs_to_many :channels

  #Each user who is a moderator can moderate many channels
  #has_many :channel_mods
  has_and_belongs_to_many :modifies , :class_name => "Channel"

  #Each user can receive many messages
  has_and_belongs_to_many :messages

  #Each user belongs to a carrier
  belongs_to :carrier

  #Filter users by role(s)
  named_scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0 "} }

  def roles  
    ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }  
  end

  def roles=(roles)  
    self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum  
  end

  def role_symbols
  roles.map do |role|
    role.underscore.to_sym  # NOT role.name.underscore.to_sym (role is a string)
    end
  end


end

运营商模式

class Carrier < ActiveRecord::Base
  has_many :users
end

用户控制器

class UsersController < ApplicationController

   filter_resource_access

  # GET /users
  # GET /users.xml
  def index
    @users = User.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @users }
    end
  end

  # GET /users/1
  # GET /users/1.xml
  def show
    #@user = User.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @user }
    end
  end

  # GET /users/new
  # GET /users/new.xml
  def new
    #@user = User.new
    @carriers = Carrier.find(:all)

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @user }
    end
  end

  # GET /users/1/edit
  def edit
    #@user = User.find(params[:id])
  end

  def create
    #@user = User.new(params[:user])
    #@user.channels << Channel.find(1)

    respond_to do |format|
      if @user.save
        format.html { redirect_to(:channels, :notice => 'Registration successfully.') }
        format.xml { render :xml => @user, :status => :created, :location => @user }
      else
        format.html { render :action => "new" }
        format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end

  def profile
    @user = User.find(params[:id])
  end



  # PUT /users/1
  # PUT /users/1.xml
  def update
  #@user = current_user

    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.xml
  def destroy
    @user = User.find(params[:id])
    @user.destroy
    respond_to do |format|
      format.html { redirect_to(users_url) }
      format.xml  { head :ok }
    end
  end

  def delete
    @user = User.find(params[:user_id])
    @user.destroy
    redirect_to :users
  end

end

1 个答案:

答案 0 :(得分:3)

我没有测试过您的代码,但乍一看我可以看到可能是错误的来源:

  def set_carrier
    self.carriers<< self.carrier_name
  end

你将carrier_name,可能是一个字符串分配给self.carriers,它应该包含一个Carrier对象数组。