helper_method 很简单:它使部分或全部控制器的方法可用于视图.
什么是helper?是否相反,即它将辅助方法导入文件或模块?(也许是名字helper和helper_method相似.他们可能更愿意share_methods_with_view和import_methods_from_view)
我有一个带功能的模块.它位于/lib/contact.rb中:
module Contact
class << self
def run(current_user)
...
end
end
end
Run Code Online (Sandbox Code Playgroud)
我想访问模块中的'users_path'之类的URL帮助程序.我怎么做?
在rails应用程序中,在哪种情况下你会使用partial,你什么时候会使用帮助器?我发现两者非常相似,因为它们代表了标记片段.
围绕这个有约定吗?使用它们的'轨道方式'是什么?
谢谢!
在我的ApplicationController中,我有一个定义为辅助方法的方法:
helper_method :some_method_here
我正在使用Rails3和RSpec2
我有以下代码:
<%= image_tag iterator.image.url(:small), :class => "img_preview" %>
Run Code Online (Sandbox Code Playgroud)
但呈现的HTML显示:
<img src="/actives/hotels/13/small/clean_wave.jpg?1317675452" alt="Clean_wave">
Run Code Online (Sandbox Code Playgroud)
为什么"class"属性不存在?
谢谢!
有谁知道我为什么得到
undefined method `my_method' for #<MyController:0x1043a7410>
Run Code Online (Sandbox Code Playgroud)
当我从ApplicationController子类中调用my_method("string")时?我的控制器看起来像
class MyController < ApplicationController
def show
@value = my_method(params[:string])
end
end
Run Code Online (Sandbox Code Playgroud)
和我的帮手
module ApplicationHelper
def my_method(string)
return string
end
end
Run Code Online (Sandbox Code Playgroud)
最后,ApplicationController
class ApplicationController < ActionController::Base
after_filter :set_content_type
helper :all
helper_method :current_user_session, :current_user
filter_parameter_logging :password
protect_from_forgery # See ActionController::RequestForgeryProtection for details
Run Code Online (Sandbox Code Playgroud) @model Customer
@Html.Partial("_UserProfile", (UserProfile)Model.UserProfile)
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我收到此错误:
The model item passed into the dictionary is of type 'Customer', but this dictionary requires a model item of type 'UserProfile'.
Run Code Online (Sandbox Code Playgroud)
部分视图_UserProfile是强类型的.
我希望能够编辑这些字段.有什么建议?
我们似乎从网页中抽象了许多逻辑方式并创建了"帮助"类.可悲的是,这些课程听起来都是一样的,例如
ADHelper,(Active Directory)AuthenicationHelper,SharePointHelper
其他人是否有大量具有此命名约定的类?
请参阅以下课程
public class Parent {
private String name;
private int age;
private Date birthDate;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
假设我已经创建了一个父对象,如下所示
Parent parent = new Parent();
parent.setName("A meaningful name");
parent.setAge(20);
Run Code Online (Sandbox Code Playgroud)
请注意,根据上面的代码,birthDate属性为null.现在我想从父对象复制到另一个非空属性.就像是
SomeHelper.copyNonNullProperties(parent, anotherParent);
Run Code Online (Sandbox Code Playgroud)
我需要它,因为我想更新anotherParent对象而不用空值覆盖其非null.
你知道这样的帮手吗?
我接受最少的代码作为答案是否没有帮助
问候,