Questions tagged [helpermethods]
85 questions
385
votes
7 answers
Can Rails Routing Helpers (i.e. mymodel_path(model)) be Used in Models?
Say I have a Rails Model called Thing. Thing has a url attribute that can optionally be set to a URL somewhere on the Internet. In view code, I need logic that does the following:
<% if thing.url.blank? %>
<%= link_to('Text', thing_path(thing))…
![](../../users/profiles/32137.webp)
Aaron Longwell
- 7,963
- 5
- 19
- 10
27
votes
6 answers
Organizing c# project helper or utility classes
What are some best practices for where you should have helper classes in a .NET project? Referring to classes separate from business layer stuff, but presentation and app stuff like appSetting config managers and other code that would sometimes be…
![](../../users/profiles/406059.webp)
spaghetticowboy
- 678
- 1
- 7
- 22
24
votes
6 answers
Where do I put helper methods for ActionMailer views?
I have a method that takes an array of strings and joins them so they do something like this:
>> my_arr
=> ["A", "B", "C"]
>> and_join(my_arr)
=> "A, B, and C"
Which I'd like my mailer to have access to so I can output some information into an…
![](../../users/profiles/43792.webp)
aarona
- 32,176
- 39
- 119
- 171
21
votes
5 answers
Why is using static helper methods in Java bad?
I'm asking because I'm trying to use a mocking framework (Mockito) which does not allow you to mock static methods. Looking into it I've found quite a few blog posts saying that you should have as few static methods as possible, but I'm having…
![](../../users/profiles/796106.webp)
odiggity
- 4,059
- 6
- 30
- 40
18
votes
9 answers
Unit test helper methods?
I have classes which previously had massive methods so I subdivided the work of this method into 'helper' methods.
These helper methods are declared private to enforce encapsulation - however I want to unit test the big public methods. Is it good…
![](../../users/profiles/225814.webp)
Aly
- 14,299
- 42
- 109
- 181
8
votes
3 answers
Render partial from helper_method
Ok so I have a helper method in the application controller:
def run_test(test_name)
#computation stuff
render :partial => test_name
end
And I call it like so in views:
<%= run_test("testpartial") %>
and it renders ok with only 1 (although...…
![](../../users/profiles/482255.webp)
Msencenb
- 5,407
- 11
- 48
- 81
7
votes
1 answer
How do I write methods that insert rspec examples?
In a bunch of rspec rails unit specifications I do something like:
describe Foo do
[:bar, :baz].each do |a|
it "should have many #{a}" do
Foo.should have_many(a)
end
end
end
For cleaner code I'd rather do something like:
describe…
![](../../users/profiles/383888.webp)
Mori
- 25,288
- 10
- 60
- 69
3
votes
1 answer
Question on using or equals (||=) in application controller
I have seen the or equals ||= often used in application controller methods to set a variable if it doesn't exist. The most recent in Railscasts 270. But I have a question.. take for example this helper method
def current_user
@current_user ||=…
![](../../users/profiles/482255.webp)
Msencenb
- 5,407
- 11
- 48
- 81
3
votes
2 answers
Rails helper method with link_to options as optionals
I'm trying to create a helper method that can have optional arguments for link_to method. My intention is to create a helper method for different cases:
# Typical case #1 (can have any number of extra arguments)
<%= toolbar_item('google.com',…
![](../../users/profiles/1686561.webp)
Fernando
- 185
- 9
3
votes
3 answers
Private helper method to capture wildcard type for generic methods
The following code does not compile in Eclipse. It says "The method putHelper(List,int,E) in the type Abc is not applicable for the arguments (List <.capture#8-of extends E>",int,E)"
private void putHelper(List list, int i, E value) {
…
![](../../users/profiles/1710765.webp)
shujin
- 181
- 11
3
votes
3 answers
User Helper Method - grabbing users whose ID doesn't match current user ID
trying to write a helper method with 3 basic conditions. adding the 3rd one, which is trying to take the current user out of the results, is resulting in an error.
def male_soccer_players
return User.where(:gender => "male", :soccer => true,…
![](../../users/profiles/2597568.webp)
brad
- 1,645
- 2
- 15
- 22
3
votes
3 answers
Helper methods to be used in Controllers AND Views in Rails
I understand I can put a helper method in a Helper class inside the helper folder in Rails. Then that method can be used in any view. And I understand I can put methods in the ApplicationController class and that method can be used in any…
![](../../users/profiles/326389.webp)
at.
- 45,606
- 92
- 271
- 433
3
votes
1 answer
How to Pass ActiveRecord Objects as Parameters to Helper Methods in Rails
Many helper methods, such as redirect_to, link_to, and url_for, can take an ActiveRecord object as a parameter instead of a hash that specifies the controller and action. I've seen the parameter passed different ways in different documentation. It…
![](../../users/profiles/730086.webp)
whiny_nil
- 83
- 1
- 7
3
votes
1 answer
reflection with stacktrace / stackframe to get both methods name and parameters
while writing the question subject I came across
some other allmost related post ...leading to MSDN
http://msdn.microsoft.com/en-us/library/system.reflection.parameterinfo.aspx
but i couldn't manage to extract the bit of code i needed
i just learnd…
![](../../users/profiles/1329184.webp)
LoneXcoder
- 2,071
- 5
- 35
- 70
2
votes
1 answer
Including Custom Helper Method in Ruby Gem
I've been attempting to add a helper method to my ruby gem for use with Rails 3.
Here is an example of what I am attempting to achieve:
module MyHelper
def my_method
render :text => "Hello World!"
end
end
I've tried prepending MyHelper.rb…
user414381