我希望生成一个链接,该链接的前缀附加到指定的路由本身.像这样显示路径"/ old/recipes":
recipes_path(:prefix => "old/") # the correct way should show "/old/recipes"
Run Code Online (Sandbox Code Playgroud)
我不想触摸routes.rb文件,但修改带有前缀的命名路由.这是可能的,你会如何正确地做到这一点?
编辑:我正在使用Rails 3.添加可选前缀的原因是我也想使用普通的recipes_path.所以我想同时使用"/ recipes"和"/ old/recipes".
当我的矩形的笔划宽度很大时,我的SVG的笔划会以某种方式切断.我有以下代码:
<svg width="500px" height="300px">
<rect width="352" height="128" stroke="#333" stroke-width="16" fill-opacity="0"/>
</svg>
Run Code Online (Sandbox Code Playgroud)
这是jsfiddle:https://jsfiddle.net/7ej6fzbg/3/
从jsfiddle可以看出,矩形的顶部和左侧的笔划宽度略有缩小.如何解决这个问题,使整个矩形的笔划宽度为16像素?
我相信我可以改变矩形的x和y位置,但我相信如果我想稍后更改笔划宽度,那将不会非常强大.有任何想法吗?
我正在寻找创建一个选择菜单,根据所选选项显示和隐藏某些div; 这样的事情:
<select name="choose_colors" id="color_selector">
<option value="select_color">Select color</option>
<option value="red">Choose Red</option>
<option value="blue">Choose Blue</option>
<option value="green">Choose Green</option>
</select>
<div id="red_options" class="color_options">
...
</div>
<div id="blue_options" class="color_options">
...
</div>
<div id="green_options" class="color_options">
...
</div>
Run Code Online (Sandbox Code Playgroud)
因此,如果使用选择"选择红色",则会显示#red_options的div,并且#blue_options和#green_options将隐藏.
如果用户返回默认选项"选择颜色",则隐藏#red_options /#blue_options/#green div.
我怎么在Jquery那样做?我以为会是这样的:
<script>
$(".color_options").hide();
$('select[name="choose_colors"]').change(function () {
if ("Select color") {
$("#red_options").hide();
$("#blue_options").hide();
$("#green_options").hide();
}
if ("Red") {
$("#red_options").show();
}
if ("Blue") {
$("#blue_options").show();
}
if ("Green") {
$("#green_options").show();
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
当然这不能正常工作.有任何想法吗?
我正在尝试使用模型中的定义来总计所有"数量"列,如下所示:
def self.total
self.all.collect(&:amount).sum
end
Run Code Online (Sandbox Code Playgroud)
有了它,"Recipe.total"按预期工作.但是,我正在使用一个传递"Recipe.find(:all)"的插件,我似乎无法将其传递给方法来查找总数.那是:
Recipe.find(:all).total # doesn't work
Run Code Online (Sandbox Code Playgroud)
有没有办法在我的模型中定义不同的方法,使Recipe.find(:all).total像Recipe.total一样工作?
例如,而不是在我的终端中键入所有5个命令:
command 1
command 2
command 3
command 4
command 5
Run Code Online (Sandbox Code Playgroud)
我只想输入一个运行上述所有5个命令的命令:
command everything
Run Code Online (Sandbox Code Playgroud)
那可能吗?在Ubuntu中我需要编辑什么才能这样做?
例如,假设我为类Task定义了范围,有没有办法找到该类的所有范围名称?
我正在寻找类似的东西:
Task.scope_names
Run Code Online (Sandbox Code Playgroud)
这将输出如下内容:
["completed", "uncompleted", "pending"]
Run Code Online (Sandbox Code Playgroud)
关于如何做的任何想法?
对于PostgreSQL数据库中的hstore列,我知道我可以在Ruby on Rails中使用类似的"LIKE"查询来搜索包含某个字符串的名称:
Product.where("hstore_data -> 'author' LIKE '%billy%'")
Run Code Online (Sandbox Code Playgroud)
我尝试了jsonb列类型,但得到了这个错误:
ActiveRecord :: StatementInvalid:PG :: UndefinedFunction:ERROR:运算符不存在:jsonb ~~ unknown
HINT:没有运算符匹配给定的名称和参数类型.您可能需要添加显式类型转换.:SELECT"products".*FROM"products"WHERE(jsonb_data - >'author'LIKE'%billy%')
有没有办法正确使用"LIKE"的jsonb列类型?
我想定义一个让我传递选项的方法; 就像是:
@user.tasks(:completed => true)
Run Code Online (Sandbox Code Playgroud)
我认为这样的东西可以在我的用户模型中工作(但事实并非如此):
User.rb模型
def tasks(options)
tasks.find(:all, options)
end
Run Code Online (Sandbox Code Playgroud)
如何正确定义方法让我使用@ user.tasks(:completed => true)?
我正在使用ruby,我正试图找到一种方法来抓取{start_grab_entries}和{end_grab_entries}之间的文本,如下所示:
{start_grab_entries}
i want to grab
the text that
you see here in
the middle
{end_grab_entries}
Run Code Online (Sandbox Code Playgroud)
像这样的东西:
$1 => "i want to grab
the text that
you see here in
the middle"
Run Code Online (Sandbox Code Playgroud)
到目前为止,我尝试了这个作为我的正则表达式:
\{start_grab_entries}(.|\n)*\{end_grab_entries}
Run Code Online (Sandbox Code Playgroud)
但是,使用$ 1,这给了我一个空白.你知道我能做些什么来正确地抓住标签之间的文本块吗?
如果我在Rails中有一个表单,并且我只想向用户公开某些字段,如何阻止用户攻击表单并添加其他字段以插入不需要的数据?
例如,假设我有一个task.rb模型,其中包含以下列:name,description,internal_notes.对于"app/views/tasks/new.html.erb"中的公共表单,我只想暴露公共字段,如下所示:
form
name:
description:
/form
Run Code Online (Sandbox Code Playgroud)
如何防止用户黑客攻击表单并执行此操作:
form
name:
description:
internal_notes: <--- inserted by user (e.g. through Google Chrome Elements)
/form
Run Code Online (Sandbox Code Playgroud)
例如,用户可以进入<input type="text" name="task[internal_notes]" />表单,并提交不需要的数据.
有办法防止这种情况吗?
ruby ×6
javascript ×2
activerecord ×1
hstore ×1
jquery ×1
jsonb ×1
postgresql ×1
regex ×1
shell ×1
svg ×1
ubuntu ×1