2011-03-27 36 views
3

/my_lib.rb未定義的方法`output_buffer =」爲content_tag與LIB塊

class MyLib 
    include ActionView::Helpers::TagHelper 

    def foo 
     content_tag :div do 
     "hello" 
     end 
    end 
end 

給出

require 'lib/my_lib' ;MyLib.new.foo 
NoMethodError: undefined method `output_buffer=' for #<MyLib:0x7f3209aaa450> 
    from /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/capture_helper.rb:175:in `with_output_buffer' 
    from /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/capture_helper.rb:40:in `capture' 
    from /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/tag_helper.rb:77:in `content_tag' 
    from ./lib/my_lib.rb:6:in `foo' 
    from (irb):1 

回答

8

有你不能把這個邏輯的圖內的特定原因?

content_tag是爲View環境做出,並沒有打算從控制器或其他庫中調用。您將需要設置你的MyLib中的類作爲視圖。

雖然我建議您重新思考,並重新嗅到您的代碼和設計,這些代碼和設計已將您帶到了需要此目的的位置,但您可以通過從ActionView :: Base進行子類化來完成您想要的操作。

class MyLib < ActionView::Base 
    include ActionView::Helpers::TagHelper 

    def foo 
    content_tag :div do 
     "hello" 
    end 
    end 
end 
+0

感謝您發佈此 – 2014-02-25 09:09:09

-6
//my controller... 

function edit($id) { 

    // $this->form_validation->set_rules('phone', 'Phone', 'callback_Check_Phone'); 
    $this->form_validation->set_rules('email', 'Email', 'callback_Check_email'); 
    $this->form_validation->set_rules('uname', 'Uname', 'callback_Check_User'); 

    if ($this->form_validation->run() == TRUE) { 

     $hospitalid = $this->input->post("hospitalid"); 
     $hospitalname = $this->input->post("hospitalname"); 
     $doctorname = $this->input->post('docname'); 
     $babyname = $this->input->post('babyname'); 
     $sex = $this->input->post("sex"); 
     $weight = $this->input->post('weight'); 
     $parentname = $this->input->post('parentname'); 
     $address = $this->input->post("address"); 
     $email = $this->input->post('email'); 
     $phone = $this->input->post('phone'); 
     $username = $this->input->post("uname"); 
     $dob = $this->input->post('dob'); 
     $today_date = date("d-m-Y"); 

     $dateTimestamp1 = strtotime($today_date); 
     $dateTimestamp2 = strtotime($dob); 
     $this->session->set_userdata('pid', $id); 
     $id = $this->session->userdata('pid'); 
     $pass = $this->input->post('password'); 

     if ($dateTimestamp1 < $dateTimestamp2) { 

      $sess_id = $this->session->userdata('id'); 
      $this->session->set_flashdata('msg', 'Invalid Date of Birth'); 

      $result['query'] = $this->panel_model->hospitaldetails($sess_id); 
      $result['query1'] = $this->register_model->edit_patient($id); 
      $this->load->view('innerheader', $result); 
      $this->load->view('edit_patient', $result); 
     } else { 

      $sess_id = $this->session->userdata('id'); 


      $querys = $this->db->query("SELECT days FROM vaccine_details WHERE hospitalid='$sess_id'"); 
      if ($querys->result()) { 
       if (strcmp($pass , $phone)== 0) { 
        $items = array(); 
        $j = 0; 
        foreach ($querys->result() as $row1) { 
         $items[$j++] = $row1->days; 
        } 
        $Date = $dob; 
        $stamp = strtotime($Date); 
        $d = array(); 
        $i = 0; 
        foreach ($items as $day) { 
         $newstamp = $stamp + ($day * 24 * 60 * 60); 
         $d[$i++] = date("m/d/Y", $newstamp); 
         echo "<br>"; 
        } 
        $day = implode(',', $d); 
        $this->register_model->edit_patientdetails($id, $hospitalid, $hospitalname, $doctorname, $babyname, $sex, $weight, $parentname, $dob, $address, $email, $phone, $username, $day, $pass); 
        $this->session->set_flashdata('msg', 'succesfully updated'); 

        redirect('patient_register/patient_detail'); 
       } else { 


        $items = array(); 
        $j = 0; 
        foreach ($querys->result() as $row1) { 
         $items[$j++] = $row1->days; 
        } 
        $Date = $dob; 
        $stamp = strtotime($Date); 
        $d = array(); 
        $i = 0; 
        foreach ($items as $day) { 
         $newstamp = $stamp + ($day * 24 * 60 * 60); 
         $d[$i++] = date("m/d/Y", $newstamp); 
         echo "<br>"; 
        } 
        $day = implode(',', $d); 

        $this->register_model->edit_patientdetail($id, $hospitalid, $hospitalname, $doctorname, $babyname, $sex, $weight, $parentname, $dob, $address, $email, $phone, $username, $day); 
        echo $this->sendregistermsg($phone, $username); 
        echo $this->mail($email, $phone, $username); 

        $this->session->set_flashdata('msg', 'succesfully updated'); 

        redirect('patient_register/patient_detail'); 
       } 
      } else { 

       $querys = $this->db->query("SELECT days FROM vaccine_details WHERE hospitalid='admin'"); 
       if (strcmp($pass , $phone)== 0) { 
        $items = array(); 
        $j = 0; 
        foreach ($querys->result() as $row1) { 
         $items[$j++] = $row1->days; 
        } 
        $Date = $dob; 
        $stamp = strtotime($Date); 
        $d = array(); 
        $i = 0; 
        foreach ($items as $day) { 
         $newstamp = $stamp + ($day * 24 * 60 * 60); 
         $d[$i++] = date("m/d/Y", $newstamp); 
         echo "<br>"; 
        } 
        $day = implode(',', $d); 
        $this->register_model->edit_patientdetails($id, $hospitalid, $hospitalname, $doctorname, $babyname, $sex, $weight, $parentname, $dob, $address, $email, $phone, $username, $day, $pass); 
        echo $this->sendregistermsg($phone, $username); 
        echo $this->mail($email, $phone, $username); 

        $this->session->set_flashdata('msg', 'succesfully updated'); 

        redirect('patient_register/patient_detail'); 
       } else { 
        $items = array(); 
        $j = 0; 
        foreach ($querys->result() as $row1) { 
         $items[$j++] = $row1->days; 
        } 
        $Date = $dob; 
        $stamp = strtotime($Date); 
        $d = array(); 
        $i = 0; 
        foreach ($items as $day) { 
         $newstamp = $stamp + ($day * 24 * 60 * 60); 
         $d[$i++] = date("m/d/Y", $newstamp); 
         echo "<br>"; 
        } 
        $day = implode(',', $d); 
        $this->register_model->edit_patientdetail($id, $hospitalid, $hospitalname, $doctorname, $babyname, $sex, $weight, $parentname, $dob, $address, $email, $phone, $username, $day); 
        $this->session->set_flashdata('msg', 'succesfully updated'); 

        redirect('patient_register/patient_detail'); 
       } 
      } 
     } 
    } else { 
     $this->session->set_userdata('pid', $id); 
     $id = $this->session->userdata('pid'); 
     $this->session->set_flashdata('msg', 'Invalid Entry'); 
     $sess_id = $this->session->userdata('id'); 
     $result['query'] = $this->panel_model->hospitaldetails($sess_id); 
     $result['query1'] = $this->register_model->edit_patient($id); 
     $this->load->view('innerheader', $result); 
     $this->load->view('edit_patient', $result); 
     $this->load->view('footer', $result); 
    } 
} 
+1

這並不甚至看起來像Ruby更不用說軌。 – TJChambers 2016-06-30 14:27:22