我开始研究一些框架.特别是Code Igniter让我很着迷.但通过我发现的文件夹搜索system/helpers包含21个PHP文件.在这些文件中,我们发现了数千个与html,xml,字符串,数组,数字,表单等相关的函数.
这应该很简单,但我真的不明白这些helper文件的含义.我的意思是:什么通常被认为是帮手?他们需要吗?我可以没有风险地删除它们吗?
我定义了一个helper类,如下所示
module SessionsHelper
def current_user
@current_user= User.find_by_fbid(session[:fbid])
end
def sign_in(user)
session[:fbid] = user.fbid
@current_user = user
end
def signed_in?
!current_user.nil?
end
end
Run Code Online (Sandbox Code Playgroud)
我在我的应用程序控制器中包含了Helper类
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
end
Run Code Online (Sandbox Code Playgroud)
登录方法从会话控制器调用
class SessionsController < ApplicationController
def create
user = User.find_or_create_by_fbid(params[:user][:fbid])
user.update_attributes(params[:user])
sign_in(user)
redirect_to user_path(user)
end
end
Run Code Online (Sandbox Code Playgroud)
但是我无法从用户#show view访问'current_user'变量.
<% if signed_in? %>
<p>
<b>Current User:</b>
<%= current_user.name %>
</p>
<% end %>
Run Code Online (Sandbox Code Playgroud)
它说:nil的未定义方法`name':NilClass
任何人都可以建议吗?方法current_user根本没有从index调用.
session ruby-on-rails helper session-cookies ruby-on-rails-3
如何从ember中的另一个助手调用把手块助手.
我有兴趣在绑定帮助器中转换以下内容.
{{#each link in sideMenuLinks}}
<li class="navigation page">
{{#linkTo ROUTE_VARIABLE link.linkToRouteContext}}
{{{link.iconTag}}}<i class="icon-right-open"></i>{{link.linkText}}</a>
{{/linkTo}}
</li>
{{/each}}
Run Code Online (Sandbox Code Playgroud)
请注意,我必须在帮助程序中调用{{#linkTo}}块,并使用属性link.linkToRoute中的值更改ROUTE_VARIABLE.
ColumnSort我的rails项目中有一个模块.
module ColumnSort
extend ActiveSupport::Concern
def sort_column
# do something
end
end
Run Code Online (Sandbox Code Playgroud)
我正在CompaniesController和它一起使用它UsersController.
class CompaniesController < ApplicationController
include ColumnSort
helper_method :sort_column
end
class UsersController < ApplicationController
include ColumnSort
helper_method :sort_column
end
Run Code Online (Sandbox Code Playgroud)
它工作正常.但是我想helper_method :sort_column在模块中写下这一行ColumnSort.我怎么写呢?
<%= f.check_box:TYPE %> AB <br><br>
Run Code Online (Sandbox Code Playgroud)
我在我的代码中使用了上面提到的,当我点击提交时,它需要1if checked和0if not checked存储在db.我怎么能存储string类似AB,如果checked和nil如果not checked,我想该字符串存储db,而不是0 and 1?
我有一个rspec规范:
require "spec_helper"
describe ApplicationHelper do
describe "#link_to_cart" do
it 'should be a link to the cart' do
helper.link_to_cart.should match /.*href="\/cart".*/
end
end
end
Run Code Online (Sandbox Code Playgroud)
和ApplicationHelper:
module ApplicationHelper
def link_to_cart
link_to "Cart", cart_path
end
end
Run Code Online (Sandbox Code Playgroud)
这在访问站点时有效,但规范失败,并显示有关路由的RuntimeError不可用:
RuntimeError:
In order to use #url_for, you must include routing helpers explicitly. For instance, `include Rails.application.routes.url_helpers
Run Code Online (Sandbox Code Playgroud)
因此,我Rails.application.routes.url在我的规范中包含了spec_helper-file,甚至包括ApplicationHelper它本身,都无济于事.
编辑:我正在通过spork运行测试,也许这与它有关并导致问题.
在使用Spork运行时,如何包含这些路线助手?
为了减少冗余代码,我有一些throw辅助方法:
protected static X ThrowInvalidOperation(string operation, X a, X b) {
throw new InvalidOperationException("Invalid operation: " + a.type.ToString() + " " + operation + " " + b.type.ToString());
}
Run Code Online (Sandbox Code Playgroud)
用法:
public static X operator +(X a, X b) {
if (...) {
return new X(...);
}
return ThrowInvalidOperation("+", a, b);
}
Run Code Online (Sandbox Code Playgroud)
问题:因为运算符+必须总是返回一个值,所以我通过ThrowInvalidOperation返回一个值并使用它来调用它来修复它returnThrowInvalidOperation("+", a, b);
有许多不满 - 一个是因为我不能从返回不同类型的方法中调用它.
我希望有一种方法可以将辅助函数标记为"始终抛出异常",因此编译器会停止跟踪返回值.
问:我有什么可能做到这一点?
对于实现 Comparable 接口的对象 a 和 b,我想避免使用类似的代码
if (a.compareTo(b) > 0) {
...
}
Run Code Online (Sandbox Code Playgroud)
相反,我正在寻找类似的辅助方法
if (a.isGreaterThan(b)) {
...
}
Run Code Online (Sandbox Code Playgroud)
这对我有很大帮助,因为我不必总是查找 compareTo(T o) 的返回值的定义:
当此对象小于、等于或大于指定对象时,返回一个负整数、零或正整数。
真正有用的是5种不同的方法:
Instead of use potential helper method:
a.compareTo(b) < 0 a.isLessThan(b)
a.compareTo(b) <= 0 a.isLessOrEqualTo(b)
a.compareTo(b) == 0 a.isEqualTo(b)
a.compareTo(b) >= 0 a.isGreaterOrEqualTo(b)
a.compareTo(b) > 0 a.isGreaterThan(b)
Run Code Online (Sandbox Code Playgroud)
JDK 或其他提供此类功能的库中是否有类似的辅助方法?
似乎我的帮助文件 app/helpers.php 根本不起作用。当我尝试调用其中的函数时,我只是收到Call to undefined function App\Http\Controllers\test()错误,即使它具有与另一个有效项目完全相同的设置(我认为?)。
所有似乎可以帮助其他人解决这个问题的方法对我来说不起作用。即,将文件添加到composer.json,转储自动加载等。
帮助文件的内容:
<?php
function test()
{
dd(":(");
}
Run Code Online (Sandbox Code Playgroud)
从控制器调用该函数:
public function test()
{
test();
}
Run Code Online (Sandbox Code Playgroud) 使用 Pug 过滤器和 jsTransformer-handlebars 模块,我试图将一些把手代码插入到一个将使用本地人的 pug 模板中。然而,由于 Pug 在编译时渲染过滤器,我们不能使用模板 locals。所以我想知道让 jsTransformer 在浏览器上可用是否会解决这个问题,如果是,如何打包 jsTransformer-handlebars 模块并使其在浏览器上可用。这是问题的概要(有点长,我很抱歉。我想提供大量细节以确保问题清晰):
我有以下通常用于车把的辅助函数):
//helpers.js
module.exports = {
truncate: (str, len) => {
//code
return str;
},
stripTags: input => input.replace(/<(?:.|\n)*?>/gm, ''),
formatDate: (date, format) => moment(date).format(format),
select: function (selected, options) {
return options.fn(this).replace(new RegExp(` value="${selected}"`), '$&selected="selected"').replace(new RegExp(`>${selected}</option>`), 'selected="selected"$&');
},
};
Run Code Online (Sandbox Code Playgroud)
helpers.js 在 app.js 中被导入并且函数被设置为应用程序变量。除了 select() 之外,它们都可以在 pug 中使用。
//sets application locals so I can call these functions from a pug template
app.locals = {
truncate,
stripTags,
formatDate,
select, …Run Code Online (Sandbox Code Playgroud)