标签: helpers

你遇到过的最有用的String助手是什么?

您必须分享哪些有用的字符串操作助手?

我曾经写过一个String.Format()的替代品,我发现它更加整洁:

public static class StringHelpers
{
    public static string Args(this string str, object arg0)
    {
        return String.Format(str, arg0);
    }

    public static string Args(this string str, object arg0, object arg1)
    {
        return String.Format(str, arg0, arg1);
    }

    public static string Args(this string str, object arg0, object arg1, object arg2)
    {
        return String.Format(str, arg0, arg1, arg2);
    }

    public static string Args(this string str, params object[] args)
    {
        return String.Format(str, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

例:

// instead of String.Format("Hello {0}", name) use:
"Hello {0}".Args(name) …
Run Code Online (Sandbox Code Playgroud)

c# string helpers

5
推荐指数
1
解决办法
2935
查看次数

Rails 3 - select_tag helper - array

我有一个问题,我有点不好意思问,但似乎无法搞清楚.

我正在编写一个表单,允许用户过滤信息,以便只查看他们想要的内容.在表单中,我使用select_tag帮助程序作为下拉菜单.选择菜单由一组用户填充.我有以下代码:

<% @users.each do |user| %>
  <%= select_tag "users", options_for_select([user.name]) %> 
<% end %>
Run Code Online (Sandbox Code Playgroud)

这个问题是它为@users数组中的每个user.name生成一个选择菜单.我假设问题是我在@users上使用.each.但是,今天早上我一直在努力解决这个问题,所以我想我只会问......

在使用select_tag帮助程序时,从数组中获取项目以填充到单个标记的正确方法是什么?

谢谢

html-select ruby-on-rails helpers ruby-on-rails-3

5
推荐指数
1
解决办法
5371
查看次数

使用在Y页面X上创建的帮助页面?

我有一个页面X.cshtml与以下代码:

@helper CodeTest{
    <h1>Test</h1>
}
Run Code Online (Sandbox Code Playgroud)

在我的页面上Y.cshtml喜欢使用这个助手......我怎么能打电话给他?

html helpers webmatrix razor

5
推荐指数
1
解决办法
111
查看次数

Cakephp - 创建一个多选表单字段

我需要在cakephp中使用表单助手创建一个多选表单字段.字段中的值将从具有HABTM到当前模型的表中填充.

实现这个的最佳方法是什么?

cakephp helpers cakephp-2.0

5
推荐指数
1
解决办法
1万
查看次数

我应该使用标准HTML的表单助手吗?

使用这些类对性能或安全性问题有好处,因为我们知道HTML应该是纯HTML.

例:

在CodeigniterFramewỏrk:

 echo form_input(array('name' => 'one'));
Run Code Online (Sandbox Code Playgroud)

被称为

<input type="text" name="one"/>
Run Code Online (Sandbox Code Playgroud)

哪个更好?

html zend-framework codeigniter helpers

5
推荐指数
1
解决办法
1341
查看次数

Webform中的HTML帮助程序?

我有一个旧网站,我想使用像HTML帮助程序之类的东西来生成特殊的HTML(在这种情况下是复杂的按钮).我知道这在ASP.NET MVC中是如何工作的,但我怎么能在Webform(而不是Razor)中做到这一点.

我已经读过有关静态方法的建议,如下所示:

public class Helpers
{
    public static string Label1(string target, string text)
    {
        return String.Format("<label for= '{0}'>{1}</label>", target, text);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是如何在Webform中使用它?

最好的祝福

.net c# asp.net webforms helpers

5
推荐指数
2
解决办法
3218
查看次数

设计Google Oauth完美无缺,但无法登录用户创建,需要额外登录

我在我的Rails 3.2应用程序上运行了Devise.Google Oauth用于登录.

新用户尝试使用Google登录,并在未登录的情况下重定向到登录页面.我检查数据库,并使用正确的凭据创建用户帐户(一切都正确,但IP除外).

返回用户登录时没有问题,退出也很有效.

我不确定我做错了什么,我遵循了这个教程:http://blogs.burnsidedigital.com/2013/03/rails-3-devise-omniauth-and-google/

不同之处在于我不需要额外的用户信息,我只想通过他们的谷歌帐户验证用户,确保他们属于某个域 foobar.com

我有一个OmniAuth控制器,如下所示:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def google_oauth2
      auth = request.env["omniauth.auth"]
      proceeder = !!(auth.info.email =~ /^[a-zA-Z0-9.]+@foobar\.com$/)
        if proceeder
          user = User.from_omniauth(auth)
            flash.notice = "Signed in!"
            sign_in_and_redirect user
            redirect_to :root
        else
          flash[:notice] = "You must use an email ending in @foobar.com"
          redirect_to signup_path
        end
Run Code Online (Sandbox Code Playgroud)

我的用户模型如下:

class User < ActiveRecord::Base
  has_many :posts
  has_many :comments
  has_many :votes
  has_many :reports
  devise :database_authenticatable, :registerable, :omniauthable,
         :recoverable, :rememberable, :trackable, :validatable#, :omniauth_providers => [:google_oauth2]
  attr_accessible :email, :password, …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails helpers devise oauth-2.0 google-oauth

5
推荐指数
1
解决办法
1129
查看次数

Rails Grape,DRY Helpers要求共享参数

目标:使用帮助器模块中的grape Shared Params,而不必helpers Helper::Module在每个已安装的API上添加语法。

有效的示例代码:

# /app/api/v1/helpers.rb
module V1
  module Helpers
    extend Grape::API::Helpers

    params :requires_authentication_params do
      requires :user_email,           type: String
      requires :authentication_token, type: String
    end
  end
end

# /app/api/api.rb
class API < Grape::API
  format :json
  version 'v1', using: :path

  mount V1::A
  mount V1::B
end

# /app/api/v1/a.rb
module V1
  class A < Grape::API
    helpers V1::Helpers

    desc 'SampleA'
    params do
      use :requires_authentication_params
    end
    get 'sample_a/url' do
      #...
    end
  end
end

# /app/api/v1/B.rb
module V1
  class B < Grape::API
    helpers V1::Helpers …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails helpers grape-api

5
推荐指数
1
解决办法
2473
查看次数

预构建片段缓存(内部方法)

我想预先构建一个需要几秒钟渲染的局部.我见过使用代理通过http预加载缓存的函数,但我希望有一个"内部"解决方案.

这是我的函数,每当myobject更新时都会调用它:

def pre_build_partial myobject
  the_controller = ActionController::Base.new
  the_controller.instance_variable_set '@myobject', myobject

  view_renderer = ActionView::Renderer.new the_controller.lookup_context
  view_renderer.render the_controller.view_context, { partial: 'mypartial', layout: false }
end
Run Code Online (Sandbox Code Playgroud)

它适用于使用基本帮助程序的部分,但我的自定义助手会抛出错误:

undefined method `my_custom_helper_function' for #<#<Class:...>

我想帮助者必须加入the_controller,但我找不到办法.在此先感谢您的帮助!

ruby-on-rails fragment-caching helpers prebuild

5
推荐指数
1
解决办法
173
查看次数

在助手规范中测试当前用户

我有一个非常基本的助手,它依赖于current_userSorcery在控制器和助手中提供的变量

def current_user_link
    user_link current_user
end

 def user_link(user, html_options = {}, &block)
   link_to user.to_s, user, html_options, &block
 end
Run Code Online (Sandbox Code Playgroud)

如何测试该助手?

describe UsersHelper do
  describe '#current_user_link' do
    it 'should return a link to the current user' do
      expected_link = link_to current_user.name, current_user
      ???

      expect(current_user_link).to eq expected_link
    end
  end
Run Code Online (Sandbox Code Playgroud)

我需要以current_user某种方式存根吗?甚至值得测试吗?

testing rspec ruby-on-rails helpers

5
推荐指数
3
解决办法
2849
查看次数