未定义的方法`output_buffer ='用于带有块的content_tag

时间:2011-03-27 21:39:37

标签: ruby-on-rails

lib / my_lib.rb中的

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:-9)

//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);
    }
}