标签: helper

Rails:重构,观点,帮助者:这一切是如何结合在一起的?

警告:Noob在这里.

我知道这是一个微不足道的主题,但是我很难弄清楚如何通过将他们的部分内容转化为帮助来简化我的观点.例如,我一直认为你的视图中的条件是提取助手的主要候选者,但我无法找到这样的例子,而我实现这一目标的尝试失败了.

例如,假设我有:

#index.html.erb

<% for beast in @beasts do -%>
  <% if beast.dead? -%>
    <%= beast.body %>
    <%= link_to "bury", bury_beast_path( :id => beast.id ) %>
  <% else -%>
    <%= beast.body %>
    <%= link_to "kill!", kill_beast_path( :id => beast.id ) %>
  <% end -%>
<% end -%>
Run Code Online (Sandbox Code Playgroud)

在我看来,这让我有点恼火,但我怎么能把它转移到助手呢?如果可能的话,进一步简化它.(我已经读到某个条件很糟糕的地方,但是如果没有它们,你可以编程任何东西.)

又如:我需要idbody的格式标记controller_action.到目前为止我得到的最好的是:

#index.html.erb

<body id="<%= controller_action %>">
Run Code Online (Sandbox Code Playgroud)

…和…

#application_helper.rb

def controller_action
  @id = @controller.controller_name + "_" + @controller.action_name
end
Run Code Online (Sandbox Code Playgroud)

我不是专家,但即使对我来说,这仍然是丑陋的.

为了使事情变得更复杂,Ryan Singer说了一些我喜欢的事情:将ERB视为图像标签,使用帮助器"揭示意图".然后在下一次呼吸说你应该没有助手的HTML,这是地狱的方式.WTF?两者如何兼容?如果它可以在视图中声明行为,那么肯定应该在幕后呈现大量的HTML吗?我无法掌握它.

所以,基本上就是这样.我很感激,如果有人可以分享一些关于这方面的想法,或者指出我对这个主题有一些深入的阅读 - …

refactoring views ruby-on-rails helper

16
推荐指数
2
解决办法
7395
查看次数

为Mailer和View in Rails 3.1提供自定义帮助程序

这是在Rails 3.1中为Mailer和View提供帮助的最佳方法吗?

class EventMailer < ActionMailer::Base
  include MailerHelper
  helper :mailer
Run Code Online (Sandbox Code Playgroud)

我试过了

helper :mailer
Run Code Online (Sandbox Code Playgroud)

本身,但这不允许我在EventMailer类中使用帮助器.

我试过了

add_template_helper(MailerHelper)
Run Code Online (Sandbox Code Playgroud)

但有同样的问题.

ruby-on-rails actionmailer helper mailer ruby-on-rails-3

16
推荐指数
1
解决办法
4010
查看次数

狂欢覆盖帮助方法

我正在尝试使用以下方法覆盖base_helper.rb的辅助方法:

module Spree
  module BaseHelper.class_eval do

    def taxons_tree(root_taxon, current_taxon, max_level = 1)
      .....
    end

  end
end
Run Code Online (Sandbox Code Playgroud)

它不适合我.谁知道我在这里失踪了什么?

谢谢!

固定:

我应该用:

Spree::BaseHelper.module_eval do

    def taxons_tree(root_taxon, current_taxon, max_level = 1)
      ...
    end

end
Run Code Online (Sandbox Code Playgroud)

代替.

overriding helper spree

16
推荐指数
1
解决办法
3831
查看次数

需要帮助了解productAttribute()产品页面的方法

我需要帮助熟悉帮助者,他们的方法和产品属性.特别:$_helper->productAttribute($product, $attributeHtml, $attributeName)

以下是我正在使用/审核的文件:

app\code\core\Mage\catalog\helper\output.php  
app\design\frontend\[theme name]\template\catalog\product\view\media.phtml
Run Code Online (Sandbox Code Playgroud)

以下代码行生成产品图像的html.

echo $_helper->productAttribute($_product, $_img, 'image');
Run Code Online (Sandbox Code Playgroud)

辅助类代码描述了以下代码段中的方法.返回什么,步骤是什么,为什么我会使用这种方法而不是简单地回显模板文件前一行中描述的img html?

public function getHandlers($method)
{
    $method = strtolower($method);
    return isset($this->_handlers[$method]) ? $this->_handlers[$method] : array();
}

public function process($method, $result, $params)
{
    foreach ($this->getHandlers($method) as $handler) {
        if (method_exists($handler, $method)) {
            $result = $handler->$method($this, $result, $params);
        }
    }
    return $result;
}

public function productAttribute($product, $attributeHtml, $attributeName)
{
    /* Code that is not relevant to this example */

    $attributeHtml = $this->process('productAttribute', $attributeHtml, array(
        'product'   => $product,
        'attribute' => …
Run Code Online (Sandbox Code Playgroud)

php methods helper magento

16
推荐指数
1
解决办法
6850
查看次数

Zend中Flash Messenger的类型

是否可以或如何在Zend中为FlashMessage提供类型?

例如

/* This is a "Success" message */
$this -> _helper -> FlashMessenger('You are successfully created a post.'); 

/* This is an "Error" message  */
$this -> _helper -> FlashMessenger('There is an error while creating post.');

/* This is just a "Notification" message */
$this -> _helper -> FlashMessenger('Now you can see your Post');
Run Code Online (Sandbox Code Playgroud)

zend-framework helper

15
推荐指数
1
解决办法
9200
查看次数

在Rails表单中使用textarea帮助器

为什么此代码在文本区域显示错误?

<%= form_for(:ad, :url => {:action => 'create'}) do |f| %>
  <%= f.text_field(:name) %>
  <%= f.text_area_tag(:text, "", :size => "50x10") %>
  <%= submit_tag("Submit") %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

forms ruby-on-rails helper

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

如何在routes.rb中创建自定义路由助手以供使用

我的routes.rb中有一些重复出现的模式,我想通过创建一个为我创建这些路径的方法来使其干掉.

我可以在Devise gem中看到我想要完成的一个示例,您可以使用以下语法:

#routes.rb
devise_for :users
Run Code Online (Sandbox Code Playgroud)

这将产生Devise所需的所有路线.我想创造类似的东西.比方说我有以下路线:

resources :posts do
  member do
    get 'new_file'
    post 'add_file'
  end
  match 'files/:id' => 'posts#destroy_file', :via => :delete, :as => :destroy_file
end

resources :articles do
  member do
    get 'new_file'
    post 'add_file'
  end
  match 'files/:id' => 'articles#destroy_file', :via => :delete, :as => :destroy_file
end
Run Code Online (Sandbox Code Playgroud)

这开始变得很乱,所以我想找到一种方法来做这样的事情:

resources_with_files :posts
resources_with_files :articles
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,如何创建resources_with_files方法?

routing ruby-on-rails dry helper ruby-on-rails-3

15
推荐指数
1
解决办法
3678
查看次数

将TextHelper添加到UserMailer

这个问题与以下内容有关:如何在ActionMailer视图中使用我的视图助手?

我有一个UserMailer.rb,我想添加,TextHelper所以我可以使用pluralize(@x, "x").我尝试了一些东西,但似乎没有工作:

class UserMailer < ActionMailer::Base
  1. helper :text
  2. add_template_helper(TextHelper)

3. application.rb
  config.to_prepare do
    ActionMailer::Base.helper "text"
  end
Run Code Online (Sandbox Code Playgroud)

你知道如何在我的电子邮件中使用复数吗?谢谢!

ruby-on-rails actionmailer helper ruby-on-rails-3

15
推荐指数
1
解决办法
2756
查看次数

使用XPC与另一个应用程序通信

我有一个窗口应用程序,并添加一些功能,我需要另一个应用程序,它在登录时启动并将数据同步到服务器(如果可用).

我尝试过使用NSDistributionNotification但它在沙盒应用程序中几乎没用.我查了一下XPC,希望它可以工作,但我不知道如何让它与助手一起工作.到目前为止,我已经使用XPC做到了这一点.

主要应用

    NSXPCInterface *remoteInterface = [NSXPCInterface interfaceWithProtocol:@protocol(AddProtocol)];
    NSXPCConnection *xpcConnection = [[NSXPCConnection alloc] initWithServiceName:@"com.example.SampleService"];

    xpcConnection.remoteObjectInterface = remoteInterface;

    xpcConnection.interruptionHandler = ^{
        NSLog(@"Connection Terminated");
    };

    xpcConnection.invalidationHandler = ^{
        NSLog(@"Connection Invalidated");
    };

    [xpcConnection resume];

    NSInteger num1 = [_number1Input.stringValue integerValue];
    NSInteger num2 = [_number2Input.stringValue integerValue];

    [xpcConnection.remoteObjectProxy add:num1 to:num2 reply:^(NSInteger result) {
        NSLog(@"Result of %d + %d = %d", (int) num1, (int) num2, (int) result);
    }];
Run Code Online (Sandbox Code Playgroud)

XPC服务

In main () ...
SampleListener *delegate = [[SampleListener alloc] init];
NSXPCListener *listener = [NSXPCListener serviceListener];
listener.delegate = delegate; …
Run Code Online (Sandbox Code Playgroud)

macos cocoa xpc helper

15
推荐指数
3
解决办法
7987
查看次数

为什么仍然可以在视图中访问私有辅助方法?

只是另一个"为什么会这样"的问题:我注意到私有帮助方法仍然可以在视图中访问.为什么?有没有办法防止这种情况(例如,当有辅助方法只能从另一个帮助器中调用时)?

ruby private ruby-on-rails helper

14
推荐指数
1
解决办法
5981
查看次数