helpers

Rails: how can I access the request object outside a helper or controller?

烂漫一生 提交于 2019-12-23 08:07:49
问题 In my application_helper.rb file I have a function like this: def internal_request? server_name = request.env['SERVER_NAME'] [plus more code...] end This function is needed in controllers, models, and views. So, I put this code in a utility function file in the lib/ directory. However, this did not work: I got complaints about request not being defined. How can I access the request object in a file in the lib/ directory? 回答1: The simplest thing that would seem to work here is to pass the

Using link_to in a class in a Rails helper

只愿长相守 提交于 2019-12-21 09:06:35
问题 I have a rails helper using the structore below, but when I use it I get the message undefined method 'link_to' The helper is arranged as: module MyHelper class Facet def render_for_search link_to("Value", params) end end class FacetList attr_accessor :facets def initialize #Create facets end def render_for_search result = "" facets.each do |facet| result << facet.render_for_search end result end end end 回答1: This is because within the Class Facet you don't have access to the template binding

Rails 3 nested resources short name?

早过忘川 提交于 2019-12-21 04:41:06
问题 I'm in the process of upgrading a Rails 2.3 app to Rails 3. In the Rails 2.3 router, it was possible to set a :name_prefix of nil on nested resources to get a shorter name. The actual URL would still be fully qualified, but the code could use a shorter name. E.g.,: map.resources :sites do |site| site.resources :groups, :as => :groups, :controller => :url_groups, :name_prefix => nil, :member => { :clone => :post } do |group| group.resources :tests, :as => :tests, :controller => :test_runs,

How do I test helpers in Rails?

半腔热情 提交于 2019-12-20 10:17:36
问题 I'm trying to build some unit tests for testing my Rails helpers, but I can never remember how to access them. Annoying. Suggestions? 回答1: In rails 3 you can do this (and in fact it's what the generator creates): require 'test_helper' class YourHelperTest < ActionView::TestCase test "should work" do assert_equal "result", your_helper_method end end And of course the rspec variant by Matt Darby works in rails 3 too 回答2: You can do the same in RSpec as: require File.dirname(__FILE__) + '/..

Dynamic Paths in Helper

你。 提交于 2019-12-20 08:58:01
问题 I'm trying to create a helper method for my admin links. In quite a few views I have the code <% if current_user %> <%= link_to "Edit", edit_model_path(model) %> <%= link_to "New", new_model_path %> <%= link_to "Delete", model, :confirm => "You're a Noob", :method => :delete %> <% end %> that only display these when logged in. I would like to do something like this in their place <%= admin_links(model) %> and pass the current item into the application helper method def admin_links(m) if

Rails: Routing without plurals gives strange helpers

♀尐吖头ヾ 提交于 2019-12-20 06:18:51
问题 I am getting a strange named helpers with this setup: In config/routes.rb I have: Qtl::Application.routes.draw do resources :qtl_table do collection do get 'search' end end ... end rake routes outputs this: 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

Zend Framework 2 custom view helpers - cross modules

*爱你&永不变心* 提交于 2019-12-19 11:22:11
问题 In Zend Framework 2, how can I have one view helper available to multiple modules? What I want is to have some general functions, like algorithmic functions, that can be reused by multiple modules. I'm currently on ZF 2 2.0.3 Thanks. 回答1: Its simple, put it in one of your modules. You can use classes and functions from other modules in ZF2. Because modules in ZF2 aren't much more than just namespaces, with some classes in them. So, if you create a view helper with the name \Module\View\Helper

How do I add a mocha expectation that a helper method will be called?

余生长醉 提交于 2019-12-19 10:05:26
问题 I'm moving a method from a controller into a helper; the method will now be called from the view. Previously, in my controller I had def show @things = gather_things end and in my functional test I had test "show assigns things" do get :show assert_equal GATHERED_THINGS, assigns(:things) end now, gather_things lives in the helper and is called from the view. I have a unit test for the helper which makes sure that it returns the right values, but I want my functional test to assert that it

Is it possible to nest helpers inside the options hash with handlebars?

◇◆丶佛笑我妖孽 提交于 2019-12-18 10:59:14
问题 For instance, is there a way to nest my " i18n " helper inside another helper's hash variable? {{view "SearchView" placeholder="{{t 'search.root'}}" ref="search" url="/pages/search" className='home-search' polyfill=true}} 回答1: Update: Handlebars now supports subexpressions, so you can just do: {{view "SearchView" (t 'search.root')}} 回答2: Your scenario is not directly supported, but there a couple of workarounds you can use. The handlebars helpers are just javascript code, so you can execute

where to put time format rules in Rails 3?

戏子无情 提交于 2019-12-18 03:04:10
问题 I am finding myself repeating typing many strftime which I defined. Having watch Ryan Bates's railscasts ep 32/33( I think), I created a custom option for the to_s method as in Time.now.to_s, so that I can do Time.now.to_s(:sw), where :sw is my custom method, to retrieve "23 Sep 2010, 5:00PM" for example. But the problem is, I don't know where to put #sw's definition. Should it be in a file in in the initializer folder? Or should it go in application.rb? Thanks! 回答1: I have a file config