How can access session variable from controller to model in Ruby on Rails -
i used session variable session[:parent] on controller , want these variable pass model. how pass params controller model?
here controller.rb file
def new @child = child.new @child.pickup_people.build @classes = [['infant', 0], ['toddlers', 1], ['early learners', 2], ['pre school', 3], ['pre kindergarten', 4]] @parent_types = [['father', 'father'], ['mother', 'mother'], ['other', 'other']] @martial_status = [['married', 'married'], ['single', 'single'], ['divorced', 'divorced'], ['separated', 'separated'], ['widowed', 'widowed'], ['other', 'other']] @parent = parent.new #parents mapped child, here mentioned show in form page after creation if session[:parent_id] != nil @parent_sessions = parent.where(:id => session[:parent_id]) end end def create logger.debug "\033[31mi here\033[31m" @child = child.new(child_params) if current_user.has_role? :day_care @child.day_care_id = current_user.day_care.id elsif current_user.has_role? :director @child.day_care_id = current_user.director.day_care.id elsif current_user.has_role? :assistant_director @child.day_care_id = current_user.assistant_director.day_care.id elsif current_user.has_role? :teacher @child.day_care_id = current_user.teacher.day_care.id end respond_to |format| if @child.save # logger.debug "\033[31m#{session[:parent_id]}\033[31m" if session[:parent] != nil session[:parent].each |p| relative.create(:child_id => @child.id, :parent_id => p["id"].to_i, :parent_type => p["type"]) end end #### gflash :success => "child succesfully created." format.html { redirect_to @child } format.json { render :show, status: :created, location: @child } session[:parent] = nil session[:parent_id] = nil logger.debug "\033[31mdestroyed#{session[:parent_id]}\033[31m" else gflash :error => "unable create, please try again." format.html { render :new } format.json { render json: @child.errors, status: :unprocessable_entity } end end end
thanks help!
models interface database. can used without session (e.g. irb). violation of mvc allow models talk session. session object not visible in models. either pass parameter method in model or define method in model returns want , store in session (from controller).
for e.g.
class user < activerecord::base def item_status return :no_such_item end end
in controller
session[:item_status] = current_user.item_status
i hope makes clear...
Comments
Post a Comment