Tag: 助手

通过Rails中的Polymorphic Type查找所有内容?

有没有办法在Rails中找到特定多态类型的所有多态模型? 因此,如果我有Group,Event和Project都具有以下声明: has_many :assignments, :as => :assignable 我可以这样做: Assignable.all …要么 BuiltInRailsPolymorphicHelper.all(“assignable”) 那样就好了。 编辑: …这样Assignable.all返回[Event, Group, Product] (类数组)

在Rails控制器中使用清理

我试图在控制器中调用sanitize 。 这是我尝试过的: class FooController < ApplicationController include ActionView::Helpers::SanitizeHelper # … end 但是,我收到此错误: undefined method `white_list_sanitizer’ for FooController:Class 我四处搜索,人们建议切换include行以包含ActionView::Helpers ,但这会导致此错误: undefined method `url_for’ for nil:NilClass 呼叫sanitize的正确方法是什么? 我正在使用Rails 2.3.5。

Rails用链接截断帮助器作为省略文本

我有很长的描述,我想截断使用truncate helper。 所以我用的是: truncate article.description, :length => 200, :omission => ‘ …’ 问题是我想使用更多作为可点击链接,所以理论上我可以使用这个: truncate article.description, :length => 200, :omission => “… #{link_to(‘[more]’, articles_path(article)}” 省略文本被视为不安全,因此它被转义。 我试图使它成为html_safe,但它不起作用,而不是链接[更多]我的浏览器仍然显示该链接的HTML。 有没有办法强制截断打印遗漏链接而不是遗漏文本?

Rails f.check_box设置已选中/未选中的值

所以我在rails中有一个带有复选框的表单助手; 我希望该复选框在选中或取消选中时将值设置为“thatvalue”或“thisvalue”; 我还没有找到任何地方如何设置它 f.check_box :field 我发现了类似的东西 但它不起作用,因为我还在我的标签中设置了:class和:style,所以有类似的东西 “checkbox”, :style => “display:none;” %> 错误并告诉我错误的参数数量(4个为5) 所以现在我必须在我的控制器中“破解”它,并根据我的复选框是0还是1来设置我的字段…这非常糟糕。 任何的想法?

Helper中的动态路径

我正在尝试为我的管理链接创建一个帮助方法。 在很多视图中我都有代码 “You’re a Noob”, :method => :delete %> 仅在登录时显示这些内容。 我想在他们的位置做这样的事情 并将当前项传递给应用程序帮助程序方法 def admin_links(m) if current_user a = “#{link_to “edit” edit_m_path(m)}” a << "#{link_to "new" new_m_path}" a < “Your a Noob”, :method => :delete}” end end 或类似的东西。

rails:fields_for选择

在我看来,我使用fields_for来显示关系表的表单数据。 但是,此表单的一部分将包含可供选择的列表。 我看到有form_for和fields_for助手的label,text_field,text_area帮助器,它们将填充已经填充的模型对象所需的信息……但是选择列表帮助器会做同样的事情呢? 当我具有一对多关系时,这将特别有用,因为fields_for遍历已经在模型对象中的每个项目并使用索引显示它。 有这样的事吗?

Rails:没有复数的路由给出了奇怪的帮助

我正在使用此设置获得一个奇怪的命名助手: 在config / routes.rb中我有: Qtl::Application.routes.draw do resources :qtl_table do collection do get ‘search’ end end … end rake routes输出: search_qtl_table_index GET /qtl_table/search(.:format) {:action=>”search”, :controller=>”qtl_table”} qtl_table_index GET /qtl_table(.:format) {:action=>”index”, :controller=>”qtl_table”} POST /qtl_table(.:format) {:action=>”create”, :controller=>”qtl_table”} new_qtl_table GET /qtl_table/new(.:format) {:action=>”new”, :controller=>”qtl_table”} edit_qtl_table GET /qtl_table/:id/edit(.:format) {:action=>”edit”, :controller=>”qtl_table”} qtl_table GET /qtl_table/:id(.:format) {:action=>”show”, :controller=>”qtl_table”} PUT /qtl_table/:id(.:format) {:action=>”update”, :controller=>”qtl_table”} DELETE /qtl_table/:id(.:format) {:action=>”destroy”, […]

如何添加一个调用辅助方法的mocha期望?

我正在将一个方法从一个控制器转移到一个帮手; 现在将从视图中调用该方法。 以前,在我的控制器中,我有 def show @things = gather_things end 在我的function测试中,我有 test “show assigns things” do get :show assert_equal GATHERED_THINGS, assigns(:things) end 现在, gather_things生活在帮助器中并从视图中调用。 我有一个帮助器的unit testing,它确保它返回正确的值,但我希望我的function测试断言它被调用。 我试过了 test “show calls gather_things” do @controller.expects(:gather_things) get :show end 但这不起作用。 我应该打电话给什么expects(:gather_things) ?

我在控制器中的帮助方法

我的应用程序应呈现html,以便在用户单击ajax-link时进行回答。 我的控制器: def create_user @user = User.new(params) if @user.save status = ‘success’ link = link_to_profile(@user) #it’s my custom helper in Application_Helper.rb else status = ‘error’ link = nil end render :json => {:status => status, :link => link} end 我的帮手: def link_to_profile(user) link = link_to(user.login, {:controller => “users”, :action => “profile”, :id => user.login}, :class […]

在Rails 3中放置时间格式规则的位置?

我发现自己重复输入我定义的许多strftime。 看过Ryan Bates的railscasts ep 32/33(我想),我在Time.now.to_s中为to_s方法创建了一个自定义选项,这样我就可以做Time.now.to_s(:sw),其中:sw是我的自定义方法,例如检索“2010年9月23日,下午5:00”。 但问题是,我不知道把#sw的定义放在哪里。 它应该在初始化文件夹中的文件中吗? 或者它应该进入application.rb? 谢谢!