我正在尝试执行以下操作:
module ApplicationHelper
class PModuleHelper
include ActionView::Helpers::TagHelper
def heading(head = "", &block)
content = block_given? ? capture(&block) : head.to_s
content_tag :h3, content, :class => :module_header
end
end
def getmh
PModuleHelper.new
end
end
Run Code Online (Sandbox Code Playgroud)
为方法heading或块提供字符串(或符号).
在视图中:
<% mh = getmh %>
<%= mh.heading :bla %> // WORKS
<%= mh.heading do %> // FAILS
test 123
<% end %>
Run Code Online (Sandbox Code Playgroud)
(注意这getmh只是为了这个例子,PModuleHelper我的应用程序中的一些其他进程返回,所以不需要对此进行评论或建议制作heading正常的辅助方法,而不是类方法)
不幸的是我总是收到以下错误:
wrong number of arguments (0 for 1)
Run Code Online (Sandbox Code Playgroud)
与亚麻布的capture(&block)电话.
如何使用capture自己的助手类?
我希望我的客户部分是https,例如注册,登录,个人详细信息,订单详细信息等.我已经设置了包含https方案的路线(我可以通过https访问客户部分)但我不能强制我的链接使用https:
<a class="register" href="<?php echo $this->url('customer/default', array('controller' => 'registration', 'action' => 'index'), array('scheme' => 'https', 'force_canonical' => true,)); ?>">Register</a>
Run Code Online (Sandbox Code Playgroud)
我得到了什么: http://myproject.dev/customer/registration
我想要的是: https://myproject.dev/customer/registration
它似乎使用当前的方案 - 所以如果我在http上,那么网址是http,如果我在https上,那么网址是https.
我如何强制$this->url一直使用该https方案?
'router' => array(
'routes' => array(
'customer' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/customer',
'scheme' => 'https',
'defaults' => array(
'__NAMESPACE__' => 'Customer\Controller',
'https' => true,
'controller' => 'Index',
'action' => 'index',
),
),
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => …Run Code Online (Sandbox Code Playgroud) 我读过collection_check_boxes但我不明白如何设置检查值.我有以下型号:
class Objective < ActiveRecord::Base
has_many :indicators
has_many :objective_children, class_name: "Objective", foreign_key: "parent_id"
def objective_ids
objective_children.collect{|o| o.id}
end
def objective_ids= objectives_ids
objectives_ids.each do |id|
objective_children << Objective.find(id)
end
end
end
Run Code Online (Sandbox Code Playgroud)
编辑视图:
<%= form_for(@objective) do |f| %>
<%= f.collection_check_boxes :objective_ids, Objective.all, :id, :name %>
<%= f.submit %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
html复选框没问题,但我不知道如何设置值objective.我被尝试定义objective_ids= objectives_ids但没有任何反应.
在控制器中:
class ObjectivesController < ApplicationController
def objective_params
params.require(:objective).permit(:name, :code, :description, :objective_ids)
end
end
Run Code Online (Sandbox Code Playgroud)
编辑
日志文件说Unpermitted parameters: perspective_id, objective_ids
checkbox ruby-on-rails view-helpers strong-parameters ruby-on-rails-4
基本上,我有很多看起来像这样的代码:
link_to t('.profile'), business_path(@business), class: '#{'active' if current_page? business_path(@business)}'
Run Code Online (Sandbox Code Playgroud)
这不是很干.
我想知道是否有人知道修改link_to帮助程序本身的好方法,以自动将"活动"类添加到当前页面的所有链接.
如果它有帮助,我愿意使用HAML或SLIM.
我想调用一个javascript函数,该函数将以用户对确认框的响应为条件.
例如,我有以下锚点:
<%= link_to 'Sign Out', destroy_session_path, confirm: 'Are you sure that you would like to sign out?', method: :delete %>
Run Code Online (Sandbox Code Playgroud)
我应该放弃不引人注目的javascript并将我自己的jQuery回调绑定到我的锚点吗?
或者是否有一些神奇的方法将回调函数传递给Rails url帮助器?我没有在文档中看到任何关于回调的讨论.
unobtrusive-javascript view-helpers link-to link-to-remote ruby-on-rails-3.2
我试图在zf2中创建一个简单的服务,我可以在viewhelper中使用它
步骤1.我在src/Application/Service/Service1.php中创建了一个类,如下所示
namespace Application\Service;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class Service1 implements ServiceLocatorAwareInterface
{
public function __construct()
{
}
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
}
public function getServiceLocator()
{
}
}
Run Code Online (Sandbox Code Playgroud)
Step2我在module.php文件中设置如下.
public function getServiceConfig()
{
return array(
'factories' => array(
'Application\Service\Service1' => function ($sm) {
return new \Application\Service\Service1($sm);
},
)
);
}
public function onBootstrap($e)
{
$serviceManager = $e->getApplication()->getServiceManager();
$serviceManager->get('viewhelpermanager')->setFactory('Abc', function ($sm) use ($e) {
return new \Application\View\Helper\Abc($sm);
});
}
Run Code Online (Sandbox Code Playgroud)
Step3最后我在我的视图中帮助src/Application/View/Helper/Abc.php测试()方法就像这样,II注释这行$this->sm->get('Application\Service\Service1');没有错误,必须有一些我在服务中缺少的东西?
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper; …Run Code Online (Sandbox Code Playgroud) 我的模型中有一个类方法,我需要从一个视图助手访问一个方法.目前我包括include TalkHelper,但我仍然得到NoMethodError.
我试图通过在C++中使用系统调用在特定页面/主题上打开.chm文件(Windows帮助文件).
我可以通过以下代码成功打开.chm文件到起始页面,但是如何在帮助文件中打开.chm文件到特定页面/主题?
system("start c:/help/myhelp.chm");
Run Code Online (Sandbox Code Playgroud)
PS:我知道系统是邪恶的/不鼓励的,但系统部分与我传递的.chm文件(它将指定我要打开的页面)的命令行参数并不真正相关,我试图确定.
我想知道之间的区别是什么ValueFor,直到最近我才知道,以及DisplayFor我用来显示值的差异.
我建立了一个测试项目,在其中我创建了一个具有2个属性的Model:
public class TestModel
{
[Display(Name = "Montant")]
[DisplayFormat(DataFormatString = "{0:C2}")]
public Decimal Amount
{
get;
set;
}
[Display(Name = "Date d'achat")]
[DataType(DataType.Date)]
public DateTime Date
{
get;
set;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的结果:
TestModel model = new TestModel
{
Amount = 1234.333M,
Date = DateTime.Today.AddDays(-10)
};
@Html.DisplayFor(x => x.Amout) => "1 234,33 €"
@Html.ValueFor(x => x.Amount) => "1234,333"
@Html.DisplayFor(x => x.Date) => "23/03/2014"
@Html.ValueFor(x => x.Date) => "23/03/2014 00:00:00"
Run Code Online (Sandbox Code Playgroud)
从我所看到的,没有使用的优势ValueFor而不是@Model.PropertyName
在寻找答案时,我偶然发现了这个问题,其中最受欢迎的答案虽然没有被选为最佳答案,但却给出了与我不同的结果. …
我有一个网络应用程序,我也有一个非常复杂的博客类型.对于这个博客,除了自制的标记语言之外,我还使用RedCarpet作为标记语言,这非常有用.
在我自制的标记语言中,我从应用程序中调用了产品视图和其他部分内容.我在两个不同的模型中使用它:BlogPost和Article.
例如,博客文章可能是这样的:
@blog_post.unprocessed_content = "Today I would like to show you this **cool** product that we offer: [[PRODUCT#ID:123]]."
Run Code Online (Sandbox Code Playgroud)
[[PRODUCT#ID:123]]是我自己的标记语言,很酷的是RedCarpet.我使用ApplicationHelper中的render_content方法,如下所示:
processed_content = render_content(@blog_post.unprocessed_content)
Run Code Online (Sandbox Code Playgroud)
哪个会输出
processed_content = "Today I would like to show you a <strong>cool</strong> product that we offer: <h3>Apple</h3><img src="apple.jpg"><p>Apple is a nice fruit.</p>. Price: 1 USD."
Run Code Online (Sandbox Code Playgroud)
"apple"部分是从视图部分获取的.
ApplicationHelper中的方法使用例如: - render partials/blog_post/product_item_with_pic - RedCarpet标记
我以标记/未处理状态编写所有文章/博客文章,但在我发布并加载了render_content()时预处理此内容是完全合理的:before_save.
基本上,我想使用:来自BlogPost和Article 模型的 before_save,但后来我遇到了尝试从模型中做帮助的东西的问题,这一切都变得混乱.
我试着用:
ApplicationController.helpers.render_content(@blog_post.unprocessed_content)
Run Code Online (Sandbox Code Playgroud)
但后来它找不到像/ blog_post/product_item_with_pic这样的视图部分.感觉就像我会继续碰到这样的问题.
现在,我有一个非常丑陋的解决方案(可行),并且在加载视图时在视图中完成预处理.基本上,在admin :: blog_post #show中我调用render_content然后执行保存.是的,这很难看.