新手问题 使用 collection_select 多选框报错!急啊!

topswim · 2014年07月25日 · 最后由 topswim 回复于 2014年07月26日 · 3414 次阅读

以下是我的代码: view:


<%= f.collection_select :user_ids,User.all,:id,:cname,{},{multiple: true} %>

model1:

class IncidentRecord < ActiveRecord::Base
    has_many :incident_record_userships
    has_many :users, through: :incident_record_userships
end

model2

class IncidentRecordUsership < ActiveRecord::Base
    belongs_to :incident_record
    belongs_to :user
end

model3

class User < ActiveRecord::Base
    before_save { self.email = email.downcase }
    before_create :create_remember_token
    validates :name, presence: true, length: { maximum: 50 }
  validates :cname, presence: true, length: { maximum: 50 }
  validates :tel,presence: true, length: { maximum: 11 }
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_senstive: false}

    has_secure_password
    validates:password,length: { minimum: 6}
  has_many :incident_record_userships
  has_many :incident_records, through: :incident_record_userships

  def User.new_remember_token
    SecureRandom.urlsafe_base64
  end

  def User.encrypt(token)
    Digest::SHA1.hexdigest(token.to_s)
  end

  private

    def create_remember_token
      self.remember_token = User.encrypt(User.new_remember_token)
    # Create the token.
    end
end

controller

class IncidentRecordsController < ApplicationController
  before_action :set_incident_record, only: [:show, :edit, :update, :destroy,:fix_edit]

  # GET /incident_records
  # GET /incident_records.json
  def index
    @incident_records = IncidentRecord.all
  end

  # GET /incident_records/1
  # GET /incident_records/1.json
  def show
  end

  # GET /incident_records/new
  def new
    @incident_record = IncidentRecord.new
  end

  # GET /incident_records/1/edit
  def edit
  end

  def fix_edit
  end
  # POST /incident_records
  # POST /incident_records.json
  def create
    @incident_record = IncidentRecord.new(incident_record_params_new)

    respond_to do |format|
      if @incident_record.save
        format.js { redirect_to @incident_record, notice: 'Incident record was successfully created.' }
      else
        format.js { render action: 'new' }
      end
    end
  end

  # PATCH/PUT /incident_records/1
  # PATCH/PUT /incident_records/1.json
  def update
    respond_to do |format|
      if @incident_record.update(incident_record_params_new)
        format.html { redirect_to @incident_record, notice: 'Incident record was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @incident_record.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /incident_records/1
  # DELETE /incident_records/1.json
  def destroy
    @incident_record.destroy
    @incident_records = IncidentRecord.all
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_incident_record
      @incident_record = IncidentRecord.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def incident_record_params_new
      puts  params[:incident_record][:user_ids]
      params.require(:incident_record).permit(:sms_title,:no, :host_id, :sys_id, :incident_type, :effect_degree, :priority, :incident_time, :record_user, :come_from, :customer_type, :customer, :title, :key_word, :description, :status, :belong_team, :belong_user,{:user_ids =>[]},:time_out)
    end

    def incident_record_params_fix
      params.require(:incident_record).permit(:incident_wiki_id, :modify, :solution, :fixed_time, :close_code)
    end
end

表单提交提示:

  • Users is invalid
  • Users is invalid
  • Users is invalid

我这个是参考 rails casts 258-token-fields-revised 写的。请大家指教啊!

用 options_from_collection_for_select 试试

參考這篇看看 http://stackoverflow.com/questions/8239961/rails-has-many-through-and-collection-select-with-multiple

另外看到有個地方好像有點問題 create action 裡的 format.js 是不是應該用 format.html?

问题出在这里


has_secure_password
validates:password,length: { minimum: 6}

和 password 有关,具体什么原因不知道啊。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号