Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6093565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:34:28+00:00 2026-05-23T12:34:28+00:00

I am trying to DRY up a bunch of forms code that has a

  • 0

I am trying to DRY up a bunch of forms code that has a repeating set of fields appearing at the end of each form. I wrote a helper that wraps around the form_for rails helper. But I’m starting to get lost in all the different scopes that are flying around…

My helper goes something like this:

def simple_form_helper(record_or_name_or_array, *args, &proc)
    options = ... # overriding some options, not relevant
    form_for(record_or_name_or_array, *(args << options.merge(:option => "blah")) , &proc)

    # i wish to access  &proc and append the call to render 
    # to within &procs scope (to access block local variable)
    concat render('shared/forms/submit')     # this obv does not work

end

in shared/forms/_submit.erb i have bunch of fields and submit buttons that are common to a bunch of models. So I want this to be rendered from within form_for’s scope so that there is access to f.

 f.text_field :foo
 f.hidden_field :bar
 f.submit "Save"

The idea is to use it like so in the views:

simple_form_helper :object do |f|
  f.text_field :name
  f.text_field :description
  f.text_field :other_field
  # want common fields and submit button appended here

  # I could just call render("shared/forms/submit") here
  # but that does not seem very DRY. Or am I too unreasonable?
end

So it functions like the good old form_for: makes a form for some :object with fields specific to it. And then appends the partial with fields that are common across a bunch of models.

Is there a way to accomplish that? Perhaps, there’s a better way?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:34:29+00:00Added an answer on May 23, 2026 at 12:34 pm

    pretty sure this would work

    def simple_form_helper(record_or_name_or_array, *args)
       options = ... # overriding some options, not relevant
       form_for(record_or_name_or_array, *(args << options.merge(:option => "blah"))) do |f|
           yield f if block_given?
           concat f.text_field :foo
           concat f.hidden_field :bar
           concat f.submit "Save"  
       end
    end
    

    and you can also call simple_form_helper :object without a block if you don’t need to add any fields

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to DRY up this initial verbose code: def planting_dates_not_nil? !plant_out_week_min.blank? ||
I am trying to DRY up my code a bit so I am writing
I've been trying to dry up the following regexp that matches hashtags in a
I am trying to DRY up some code by moving some logic into the
I've got a repeated code block that initializes a few variables in a bunch
I am trying to DRY up some code in HAML, but seem to have
Trying to route: scope :shortcut do resources :text_elems end Using basic scaffold with form
I'm trying to keep my specs DRY by creating a shared example group that
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to make a make generic select control that I can dynamically add elements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.