选择带有导轨4 mongoid的嵌入式型号表格

时间:2015-10-06 19:57:15

标签: ruby-on-rails ruby mongoid simple-form

我有一个嵌入式模型,嵌入式模型中的Document只能由管理员创建,但用户可以在表单中选择他们想要的值

class User 
  include Mongoid::Document

  embeds_one :state_model, class_name: "state_model", cascade_callbacks: true

  accepts_nested_attributes_for :state_model

  attr_accessor :current_password, :job_title_str, :state_model_id

  convert_id_to_object :state_model_id, :state_model
end
class UsersController << ApplicationController

  def update_params
    params.require(:user).permit(:state_model_id)
  end

end
module ConvertIdToObject
  extend ActiveSupport::Concern

  module ClassMethods
    def convert_id_to_object(id, object)

      id, object = id, object

      if StateModel.by_id(id).present?

          object = StateModel.by_id(id)

        else

      end
    end
  end

end
class StateModel
  include Mongoid::Document


  field :name, type: String, localize: true
  field :value, type: String

  scope :by_value, -> (value){ where({:value => value}) unless value == ''}
  scope :by_id, -> (id){ where({:id => id}) unless id == ''}


  embedded_in :user

  validates :name, presence: true, uniqueness: true
  validates :value, presence: true, uniqueness: true

end
= simple_form_for user  do |f|
  = f.input :state_model_id , collection: state_collection
  = f.button :submit
Post:
{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"3/pmSUHrKRx56ycbraKYL+mEuAiS9QlwWt/bTglgsio=",
 "user"=>{"state_model_id"=>"5612cb074d6f722582050000"},
 "commit"=>"Update User",
 "id"=>"bowtch"}

结束我的usercontroller引发了一个Notimplemented错误就好像不接受state_model_id作为参数......我被困在这里......

0 个答案:

没有答案