I'm judging from your response that you would you argue that using a
Rails view helper inside another view helper is wrong then? I would
say this isn't crazy its just trying to DRY up a small form (just a
button) I am using in lots of different places.
I found the solution in the end.
module MyHelper
def button_thing_attempt_2
with_output_buffer(form_for(Button.new) do |f|
concat(f.submit 'Push')
end)
end
I'm judging from your response that you would you argue that using a
Rails view helper inside another view helper is wrong then? I would
say this isn't crazy its just trying to DRY up a small form (just a
button) I am using in lots of different places.
I found the solution in the end.
module MyHelper
def button_thing_attempt_2
with_output_buffer(form_for(Button.new) do |f|
concat(f.submit 'Push')
end)
end
end
I think it would be more conventional to put the form in a partial to
achieve the re-usability.
By the way it is considered bad form (!) to access a model directly
from the view or view helper. It is better in the controller to say
@button = Button.new
then use @button in the view.
Without opening a debate on the issue. I find the whole bad form,
conventions as interesting as it is frustrating. I need to re-use this
button helper in many views several times over for several different
buttons. Say...
which would require calling initialize on Fungi, Fruit, Tree objects
within the helper. If you assigned an instance variable in each
controller action for everytime you used the helper you'd actually
make re-using the code in many different places more awkward. Not to
mention if the number and type of buttons was also dynamic.
Conventions are a good building block, but on occasion you may need to
stray from them to make life easier.
Do you know of any 'Bad Form vs Good Form' Rails articles, I'd be
interested in ready some.
Without opening a debate on the issue. I find the whole bad form,
conventions as interesting as it is frustrating. I need to re-use this
button helper in many views several times over for several different
buttons. Say...
which would require calling initialize on Fungi, Fruit, Tree objects
within the helper.
No! Never initialize model objects in the helper or view. The helper
and view should *always* get their model objects from the controller.
If you assigned an instance variable in each
controller action for everytime you used the helper you'd actually
make re-using the code in many different places more awkward.
Then you can use a before_filter.
Not to
mention if the number and type of buttons was also dynamic.
Easy. Set that all in the controller. If you're having trouble with a
particular case, please post details.
Conventions are a good building block, but on occasion you may need to
stray from them to make life easier.
Not until you fully understand how to work with them. And the
convention in Rails MVC architecture is that it is *never* appropriate
for the model and view/helper to talk to each other. The controller
*must* mediate.
Do you know of any 'Bad Form vs Good Form' Rails articles, I'd be
interested in ready some.