从Rails帮助程序中查找当前呈现MIME类型

时间:2017-02-14 16:06:45

标签: ruby-on-rails ruby actionview

我正在寻找一种方法来查找我当前从帮助程序执行的渲染类型。主要做这样的事情:

 $("input").on("blur keyup", function(){
        row.children("td").each(function(){ 
           $(this).children('input').each(function () {
             if($(this).attr("name") === 'BUYDATE') && $(this).valid()){
                 //validate tds
             }
           });
        });
    });

form.validate({
        focusInvalid: false,  
        onkeyup: function(element) {  rule!!
            var element_id = $(element).attr('name');
            if (this.settings.rules[element_id]) {
                if (this.settings.rules[element_id].onkeyup !== false) {
                    $.validator.defaults.onkeyup.apply(this, arguments);
                }
            }
        },  
        rules: {
            "BUYDATE": { 
                required: { depends:function(){
                            //iterate through rows here?
                            //this only validate onn submit
                            //I guess maybe on could trigger submit onkeyup
                            //or blur?
                         }
                }
            },
            "DLOCN": { 
                required:{
                 depends: function(){
                            //iterate through rows here?
                            //this only validate onn submit
                            //I guess maybe on could trigger submit onkeyup
                            //or blur?
                }
               }
            }
        },
        messages: { // custom messages 
            "EVENT": {
                required: "Select a Program.",
                HTH_SelectValue: "Select a Program."            
            },
            "LOCN": {
                HTH_SingleLOCN: "A single location must be selected when using this option to load items."
            },
            "DLOCN": {
                required: "A customer location must be supplied when using this option to load items."
            }
        },          
        showErrors: function(errorMap, errorList) {
            FormError.hide();
            // Clean up any tooltips for valid elements
            $.each(this.validElements(), function (index, element) {
                element = $(element);
                NoError_ToolTip(element);
            });
            // Create new tooltips for invalid elements
            $.each(errorList, function (index, error) {
                element = $(error.element);
                message = error.message;
                Error_ToolTip(element,message);
                FormError.show();
            });
        },                  
        invalidHandler: function (event, validator) { //display error alert on form submit     
            success.hide();
            FormError.show();
            $(document).scrollTop( $(".form-body:first-of-type").offset().top ); 
        },
         submitHandler: function (form) {
            success.show();
            FormError.hide();
           // Submit1(form,FormError,success);
        }

    });
}

我们在我们正在使用的某个宝石中使用# some_helper.rb def url_to_faq if plain_text_render ... else # HTML end end 的变通方法覆盖,但它很严重。是否有一些官方的方法来获取渲染器元数据,无论是在Rails 4还是Rails 5?

1 个答案:

答案 0 :(得分:0)

我认为你可以在这里使用演示者。

在控制器中:

var request = require('request');

module.exports = {

  getCompletedOrders: function(date, callback) {

    request('https://some.api.getorders.json?' + '&start=' + date, function(error, response, body) {

      //Check for error
      if (error) {
        callback(error);
      }
      //Check for success status code
      if (response.statusCode !== 200) {
        callback(new Error('Invalid Status Code Returned:' + response.statusCode));
      }
      var allCompletedOrders = JSON.parse(body);
      callback(null, allCompletedOrders);
    });
  }

}

链接演示者类:

var myapi = require('./myapi');

// all of my express stuff goes here and eventually i setup my route below

app.get('/orders/complete/:date', function(req, res, next) {
  myapi.getCompletedOrders(req.params.date, function(error, orders) {
    if ( error ) {
      next(error);
    }
    res.send(orders);
  });
});

然后在视图中:

('0' + hours[hour]).slice(-2)

通过将view_context传递给演示者,您可以访问视图助手。如果不需要,请将其丢弃。

关于演示者的好文章:Presenters in Rails by Nithin Bekal