我有一些(或所有)控制器需要的Ruby方法.我试过把它们放进去/app/helpers/application_helper.rb.我已经将它用于视图中使用的方法.但是控制器看不到那些方法.是否有其他地方我应该放他们或我需要以不同方式访问这些帮助方法?
使用最新的稳定Rails.
ruby ruby-on-rails view-helpers ruby-on-rails-3 ruby-on-rails-3.2
我想在视图中提供csv链接,我将csv生成代码放入ApplicationHelper.但是我收到了这个错误:
undefined method `send_data' for #<#<Class:0x0000010151c708>:0x0000010151a070>
Run Code Online (Sandbox Code Playgroud)
引用这个:
send_data content, :type => "text/plain",
:filename => filename,
:disposition => 'attachment'
Run Code Online (Sandbox Code Playgroud)
如果我将csv代码放在控制器中它工作正常.我希望使用帮助器来避免为每个我想为csv选项提供csv选项的控制器定义路由(我有一堆).如何send_data为助手提供(和其他必要的方法)?
我发现了一些文章解决了引擎内的助手问题,这些问题不适用于消费(父)应用程序.为了确保我们都在同一页上,让我们说我们有这个:
module MyEngine
module ImportantHelper
def some_important_helper
...do something important...
end
end
end
Run Code Online (Sandbox Code Playgroud)
如果您查看"隔离引擎的助手"(L293)中的rails引擎文档,它会说:
# Sometimes you may want to isolate engine, but use helpers that are defined for it.
# If you want to share just a few specific helpers you can add them to application's
# helpers in ApplicationController:
#
# class ApplicationController < ActionController::Base
# helper MyEngine::SharedEngineHelper
# end
#
# If you want to include all of the engine's helpers, you can use #helpers …Run Code Online (Sandbox Code Playgroud) ruby-on-rails view-helpers helpers rails-engines ruby-on-rails-3.1
我有一个视图助手方法,它通过查看request.domain和request.port_string来生成一个url.
module ApplicationHelper
def root_with_subdomain(subdomain)
subdomain += "." unless subdomain.empty?
[subdomain, request.domain, request.port_string].join
end
end
Run Code Online (Sandbox Code Playgroud)
我想用rspec测试这个方法.
describe ApplicationHelper do
it "should prepend subdomain to host" do
root_with_subdomain("test").should = "test.xxxx:xxxx"
end
end
Run Code Online (Sandbox Code Playgroud)
但是当我使用rspec运行时,我得到了这个:
Run Code Online (Sandbox Code Playgroud)Failure/Error: root_with_subdomain("test").should = "test.xxxx:xxxx" `undefined local variable or method `request' for #<RSpec::Core::ExampleGroup::Nested_3:0x98b668c>`
任何人都可以帮我弄清楚我该怎么做才能解决这个问题?如何模拟此示例的"请求"对象?
有没有更好的方法来生成使用子域的URL?
提前致谢.
我从ZF(1.9.7)开始,我想从我所有项目之间共享的库中使用View Helpers.但我找不到如何将其目录添加到帮助程序路径.当我把它们放在应用程序的助手路径中时,我的牧师会罚款.
这是错误,我找到了ZF助手的路径,以及应用程序的路径.
object(ArrayObject)#71 (3) {
["exception"]=>
object(Zend_Loader_PluginLoader_Exception)#70 (6) {
["message:protected"]=>
string(151) "Plugin by name 'Voo' was not found in the registry; used paths:
Zend_View_Helper_: Zend/View/Helper/;C:/ZendStd/www/applis/VOO4_PROJECTX/views\helpers/"
["string:private"]=>
string(0) ""
["code:protected"]=>
int(0)
["file:protected"]=>
string(89) "C:\Program Files\Zend\ZendServer\share\ZendFramework\library\Zend\Loader\PluginLoader.php"
["line:protected"]=>
int(401)
Run Code Online (Sandbox Code Playgroud)
最好的问候
塞德里克
目前正在将网站从之前的模板转换为Angular.在我们之前使用的模板化过程中,我们能够调用辅助方法来正确显示数据.例如:
<script type="text/javascript">
$.views.helpers({
parseDate: function (jsonDate) {
if (jsonDate != null) {
var newDate = Utils.PrettyDate(Utils.ConvertJsonDateToJsDate(jsonDate));
return newDate;
}
}
});
</script>
<div class="post-info">
<span class="posted-date">Posted {{ :~parseDate(CreatedDate) }}</span>
 | 
<span>{{ :ReplyCount }} Replies</span>
</div>
Run Code Online (Sandbox Code Playgroud)
这非常好.试图找出一种方法来使用与Angular相同类型的功能进行模板化.有可能做类似的事情吗?如果是这样的话?
我有一个用AJAX调用的控制器(发送JSON数据),所以我不使用视图.
我需要使用personnal 视图助手来格式化我的数据,但是在我的控制器中.
那可能吗 ?
或许我做错了(也许我应该有一个观点,但是如何使用JSON)?
在重构期间,复制HAML模板的一部分并将其粘贴到帮助程序的代码是非常方便的.目前在这种情况下1)我必须从头开始重写该部分视图2)我必须使用详细的语法,如content_tag或haml_tag.
我知道可以使用HAML systax定义部分内容,这些内容将作为帮助程序.虽然1)对我来说,为每个小的微小函数创建一个单独的文件是不方便的2)部分的调用语法非常详细.
理想情况下,我希望我的*_helper类看起来像这样:
- def some_helper(*its_args)
.some_class
= some_ruby_expression
%some_tag#some_id
- another_expression do
etc
Run Code Online (Sandbox Code Playgroud)
或者至少像这样:
define_haml_helper :some_helper, [:arg1, :arg2], %{
.some_class
= some_ruby_expression
%some_tag#some_id
- another_expression do
etc
}
Run Code Online (Sandbox Code Playgroud)
有插件可以解决我的问题吗?
或者,您可以描述如何将HAML片段重构为可重用元素(帮助者/函数/部分/构建器/等)?
class My_View_Helper_Gender extends Zend_View_Helper_Abstract
{
public function Gender()
{
//
}
}
"The class method (Gender()) must be named identically to the concliding part
of your class name(Gender).Likewise,the helper's file name must be named
identically to the method,and include the .php extension(Gender.php)"
(Easyphp websites J.Gilmore)
Run Code Online (Sandbox Code Playgroud)
我的问题是: 一个视图Helper可以包含多个方法吗?我可以从我的帮助器中调用其他视图助手吗?
谢谢
卢卡
默认的created_at日期保持打印为MySQL格式:2015-06-12 09:01:26 我想以自己的方式打印它12/2/2017,以及将来的其他格式.
一个文件调用DataHelper.php并存储在/app/Helpers/DateHelper.php- 它看起来像这样
<?php
namespace App\Helpers;
class DateHelper {
public static function dateFormat1($date) {
if ($date) {
$dt = new DateTime($date);
return $dt->format("m/d/y"); // 10/27/2014
}
}
}
Run Code Online (Sandbox Code Playgroud)
能够在我的刀片视图中调用它
DateHelper::dateFormat1($user->created_at)
Run Code Online (Sandbox Code Playgroud)
我不知道下一步该做什么.
在php Laravel 5中创建自定义帮助函数的最佳实践是什么?
view-helpers ×10
php ×4
ruby ×2
angularjs ×1
controller ×1
haml ×1
helpers ×1
javascript ×1
laravel ×1
laravel-5 ×1
refactoring ×1
rspec ×1
rspec2 ×1
templates ×1