标签: format

将Rails渲染格式限制为html的方法

我有一个只有html模板的Rails 2.1.2站点,例如jobs.html.erb,所以当我请求一个restful资源时:

www.mysite.com/jobs/1

然而,如果我要求,它会以html呈现我的工作:

www.mysite.com/jobs/1.xml

我收到错误:

模板丢失在视图路径c:/ workspace/mysite/app/views中缺少模板jobs/show.xml.erb

更糟糕的是,我也可以请求类似的东西

www.mysite.com/jobs/1.xyz

事实上,我看到了错误:

模板丢失在视图路径c:/ workspace/mysite/app/views中缺少模板作业/ show.xyz.erb

要严格地呈现html内容,告诉Rails我最不清楚也最简单的方法是除了.html.erb文件之外我不想呈现任何内容.

重要的是要注意:

  • 我的一些控制器动作包含对render()方法的条件调用,而其他人使用默认的Rails行为,即如果你不调用render(),那么将呈现名为youraction.html.erb的模板.
  • 我的代码不使用responds_to()方法

如果解决方案不在render/responds_to级别会很好,因为我必须修改大量的操作.也许有一种方法可以配置Rails,以便只呈现html模板?

format rest mime ruby-on-rails render

12
推荐指数
4
解决办法
1万
查看次数

如何在python中显示没有秒的区域设置敏感时间格式

我可以使用输出区域敏感时间格式strftime('%X'),但这总是包括秒.如何在没有秒的情况下显示此时间格式?

>>> import locale
>>> import datetime
>>> locale.setlocale(locale.LC_ALL, 'en_IE.utf-8')
'en_IE.utf-8'
>>> print datetime.datetime.now().strftime('%X')
12:22:43
>>> locale.setlocale(locale.LC_ALL, 'zh_TW.utf-8')
'zh_TW.utf-8'
>>> print datetime.datetime.now().strftime('%X')
12?22?58?
Run Code Online (Sandbox Code Playgroud)

我能想到这样做的唯一方法是尝试解析输出locale.nl_langinfo(locale.T_FMT)并删除秒位,但这带来了它自己的诡计.

>>> print locale.nl_langinfo(locale.T_FMT)
%H?%M?%S?
>>> locale.setlocale(locale.LC_ALL, 'en_IE.utf-8')
'en_IE.utf-8'
>>> print locale.nl_langinfo(locale.T_FMT)
%T
Run Code Online (Sandbox Code Playgroud)

解:

(根据pixelbeat的回答.)

# -*- coding: utf-8 -*-
import locale
def locale_time(t, show_seconds=False):
    if show_seconds:
        return t.strftime('%X')
    replacement_fmts = [
        (u'.%S', u''),
        (u':%S', u''),
        (u',%S', u''),
        (u':%OS', ''),
        (u'?????%S', u''),
        (u' %S?', u''),
        (u'%S?', u''),
        (u'%r', '%I:%M %p'),
        (u'%t', '%H:%M'),
        (u'%T', …
Run Code Online (Sandbox Code Playgroud)

python format time locale

12
推荐指数
1
解决办法
2052
查看次数

正则表达式:如何找到模式的最大整数值?

想象一下,我有以下字符串:

"I'll have some %1%, some %42% and maybe some %5% as well."
Run Code Online (Sandbox Code Playgroud)

基本上,我有兴趣知道模式%(整数)%之后的最大整数值.

我甚至不确定是否可以使用正则表达式.我可以使用什么正则表达式,以便在上面的例子中答案是42?

PS一个简单的解决方案显然是简单地查找任何%(整数)%模式,并使用脚本(c ++代码)迭代所有匹配并找到最高值.我的问题是:是否可以在正则表达式中立即执行此操作?

背景:理解下面的内容可能没有必要回答这个问题,但我想你们中的一些人可能想知道.

基本上我使用的是C++和boost :: format.格式用这样的占位符构图:%1%,%2%等.如果提供的变量数与格式本身的最大整数值不对应,Boost :: format会抛出异常.我将使用的格式由(受信任的)用户(网站管理员)提供.不过,为了正确地做事,我需要验证模式.因此,我需要在模式中找到最大整数,以确保在运行时不会抛出异常.

如果您使用带有用户提供格式的boost :: format,您是如何处理此问题的?

顺便说一句,没有boost-format标签!(虽然还有其他的boost-foo标签).

Billy ONeal提供了正确的答案,Beh Tou Cheh(在所选答案的评论中)非常友好地粘贴了实际的代码:

#include <iostream>
#include <string>
#include <deque>
#include "strtk.hpp"

int main() 
{
   std::string s = "I'll have some %1%, some %42% and maybe some %5% as well.";
   std::deque<int> int_list;
   strtk::split_regex("%([\\d]+)%",
                       s,
                       strtk::range_to_type_back_inserter(int_list),
                       strtk::match_mode::match_1);

   if (!int_list.empty())
   {
        std::cout << "max: " << strtk::max_of_cont(int_list) << std::endl;
   } …
Run Code Online (Sandbox Code Playgroud)

c++ regex format boost

12
推荐指数
1
解决办法
1581
查看次数

格式字符串漏洞 - printf

为什么打印内存地址的值为0x08480110?我不确定为什么有5%的08x参数 - 这会把你带到哪里?

address = 0x08480110
address (encoded as 32 bit le string): "\x10\x01\x48\x08"
printf ("\x10\x01\x48\x08_%08x.%08x.%08x.%08x.%08x|%s|");
Run Code Online (Sandbox Code Playgroud)

这个例子来自本文的第11页http://crypto.stanford.edu/cs155/papers/formatstring-1.2.pdf

c security string format

12
推荐指数
2
解决办法
1万
查看次数

我们可以使用String.format()来填充/前缀一个具有所需长度的字符吗?

java.lang.String.format(String str, String str1)用于添加特定字符的前缀.

我可以为以下数字执行此操作:

int sendID = 202022;
String usiSuffix = String.format("%032d", sendID);
Run Code Online (Sandbox Code Playgroud)

String的长度为32,左侧为0:00000000000000000000000000202022

如何在sendID之String类的时候实现同样的目标:

String sendID = "AABB";
Run Code Online (Sandbox Code Playgroud)

我想要一个输出,如: 0000000000000000000000000000AABB

java string format

12
推荐指数
3
解决办法
4万
查看次数

防止<code>标记中的自动换行符

我有一个html code标签,包裹在一个pre固定宽度的标签中,我得到了丑陋的自动换行符:

丑陋的换行!

我想要实现的是,文本不会在空格上自动分解,但是当我white-space: nowrapcode元素添加一个元素时,整个东西会折叠成一行,所以所有\n和\ r \n字符也会被忽略:

单线

有没有人知道如何防止自动换行,但保持预期的换行符?

html css format whitespace twitter-bootstrap

12
推荐指数
1
解决办法
9039
查看次数

如何在Java中将字节数组转换为十六进制格式

我知道您可以使用printf并使用StringBuilder.append(String.format("%x", byte))将值转换为HEX值并在控制台上显示它们.但我希望能够实际格式化字节数组,以便每个字节显示为HEX而不是十进制.

这是我的代码的一部分,我已经说过我说的前两种方式:

if(bytes > 0)
    {
        byteArray = new byte[bytes]; // Set up array to receive these values.

        for(int i=0; i<bytes; i++)
        {
            byteString = hexSubString(hexString, offSet, CHARSPERBYTE, false); // Isolate digits for a single byte.
            Log.d("HEXSTRING", byteString);

            if(byteString.length() > 0)
            {
                byteArray[i] = (byte)Integer.parseInt(byteString, 16); // Parse value into binary data array.
            }
            else
            {
                System.out.println("String is empty!");
            }

            offSet += CHARSPERBYTE; // Set up for next word hex.    
        }

        StringBuilder sb = new …
Run Code Online (Sandbox Code Playgroud)

java format stringbuilder hex bytearray

12
推荐指数
3
解决办法
4万
查看次数

git pre-commit钩子格式代码 - Intellij/Android Studio

本要点介绍了如何在预提交时使用Eclipse格式化程序自动格式化Java代码.

资料来源:https://gist.github.com/ktoso/708972

码:

#!/bin/sh
#
# This hook will run the eclipse code formatter before any commit
# to make the source look as it's supposed to look like in the repo.

ECLIPSE_HOME=$HOME/eclipse
STYLE_FILE=$HOME/org.eclipse.jdt.core.prefs
echo "Running pre-commit hook: run-eclipse-formatter---------------------"
echo "Will run eclipse formatter, using: $STYLE_FILE"
echo "Listing folders to run formatter on… "
code_dirs=`find . -maxdepth 3 | grep 'src/'`
for dir in $code_dirs; do
   echo $dir;
done;

echo "Launching eclipse code formatter…    "
exec $ECLIPSE_HOME/eclipse …
Run Code Online (Sandbox Code Playgroud)

git format hook intellij-idea android-studio

12
推荐指数
2
解决办法
4724
查看次数

Angular中的Money格式指令

我需要一个过滤货币字段的指令,因此用户只需要输入并隐含小数.

需求:

  1. 格式十进制字段作为用户类型 -

用户输入时从百分位开始.所以他们输入"4"并看"0.04",输入"42"并看"0.42",输入298023并看"2980.23"

  1. 字段必须是数字
  2. 必须允许负面 -
  3. 允许0.00作为数字输入
  4. 理想情况下,使用type ="number",但"type = text"是可以的
  5. 您应该能够清除该字段为空.

ng-currency过滤器不能满足这些要求.请看看掠夺者的行为,看看我的意思.

我的First Plunker有`input = text'并允许负数.一个问题是你不能输入负数作为第一个数字.清除该字段后,它将返回"0.00",但应该完全清除.

   app.directive('format', ['$filter', function ($filter) {
 return {
            require: 'ngModel', //there must be ng-model in the html
            link: function (scope, elem, attr, ctrl) {
                if (!ctrl) return;

                ctrl.$parsers.unshift(function (viewValue, modelValue) {
                    var plainNumber = viewValue.replace(/[^-+0-9]/g,'');
                    var newVal = plainNumber.charAt(plainNumber.length-1);
                    var positive = plainNumber.charAt(0) != '-';
                    if(isNaN(plainNumber.charAt(plainNumber.length-1))){
                      plainNumber = plainNumber.substr(0,plainNumber.length-1)
                    }
                    //use angular internal 'number' filter
                    plainNumber = $filter('number')(plainNumber / 100, …
Run Code Online (Sandbox Code Playgroud)

javascript format currency angularjs angularjs-directive

12
推荐指数
1
解决办法
6466
查看次数

如何规避GCC中的格式截断警告?

我收到以下gcc格式 - 截断警告:

test.c:8:33: warning: ‘/input’ directive output may be truncated writing 6 bytes into a region of size between 1 and 20 [-Wformat-truncation=]
snprintf(dst, sizeof(dst), "%s-more", src);
                             ^~~~~~
test.c:8:3: note: ‘snprintf’ output between 7 and 26 bytes into a destination of size 20
snprintf(dst, sizeof(dst), "%s-more", src);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

像这样的代码:

char dst[20];
char src[20];
scanf("%s", src);
snprintf(dst, sizeof(dst), "%s-more", src);
printf("%s\n", dst);
Run Code Online (Sandbox Code Playgroud)

我知道它可能会被截断 - 但这正是我首先使用snprintf的原因.有没有办法让编译器明白这是预期的(不使用编译指示或-Wno格式截断)?

c format gcc gcc-warning

12
推荐指数
3
解决办法
8776
查看次数