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 5943769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:26:34+00:00 2026-05-22T16:26:34+00:00

I am currently working on a rails app that has a common pattern on

  • 0

I am currently working on a rails app that has a common pattern on fields throughout many forms and many fields of the same form. This is the pattern:

<%= simple_form_for(@evaluation) do |f| %>
  <%= f.error_notification %>

  <div class="toggle">

    <div class="toggle_selector">
      <%= f.input :was_answered, :as => :radio, :collection => [:yes, :no, '?'] %>
    </div>

    <div class="toggle_content">
      <%= f.input :name %>
      <%= f.input :answer %>
      <%= f.input :score %>
    </div>

  </div>

  <div class="actions">
    <%= f.button :submit %>
  </div>
<% end %>

There is a main div tag that surrounds two other divs. The toggle_content div is made visible or not, using javascript, based on the value from the input that is in toggle_selector div.

I tried to extract a couple helpers that would take the inputs as parametes and output the desired output:

def toggling_field_for form, selector, &block                            
  content_tag :div, :class => 'toggle' do                                
    %{#{toggle_selector form, selector}                                  
      #{toggle_content &block if block_given?}                           
    }.html_safe                                                          
  end                                                                    
end                                                                      

private                                                                  

def toggle_selector form, selector                                       
    content_tag :div, :class => 'toggle_selector' do                     
      form.input selector, :as => :radio, :collection => [:yes, :no, '?']
    end                                                                  
end                                                                      

def toggle_content                                                       
  content_tag :div, :class => 'toggle_content' do                        
    yield if block_given?                                                
  end                                                                    
end            

However, this will only work if i only have one input on the toggle_content div, because when i use the yield on the block of inputs, only the last one will be output.

Anyone could give a hint on a better solution to refactor these to be able to accept more than one input on the toggle_content div?

Edited: Problem solved using the capture helper method instead of just yield. The code ended up like this on the helper:

def toggling_field_for form, selector, &block
  content_tag :div, :class => 'toggle' do
    %{#{toggle_selector form, selector}
      #{toggle_content &block}
    }.html_safe
  end
end

private

def toggle_content &block
  content_tag :div, capture(&block), :class => 'toggle_content'
end

And called like this on each form:

    <%= toggling_field_for f, :was_answered, do %>
        <%= f.input :name %>
        <%= f.input :answer %>
        <%= f.input :score %>
    <% end %>

Thanks, mosch!

  • 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-22T16:26:35+00:00Added an answer on May 22, 2026 at 4:26 pm

    With Rails 3, your code should work with a slight modification: Try changing

    def toggle_content                                                       
      content_tag :div, :class => 'toggle_content' do                        
        yield if block_given?                                                
      end                                                                    
    end
    

    to

    def toggle_content(&block)
      content_tag :div, :class => 'toggle_content' do                        
        capture(&block) if block_given?
      end                                                                    
    end
    

    I am not sure, but you can probably remove the block_given? clause in that case.

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

Sidebar

Related Questions

A rails app I am developing currently has about 35 migrations up to this
I'm working on a rails app that has a whole bunch of before filters
I'm currently working with a rails 3.0.x app that's recently been upgraded from rails
Hello Ruby/Rails/Merb developers! Im currently working on a web project that will have a
Background Currently, I am working on a Rails application. I have different products that
My Rails 3 app has a UserAction object that I use to store information
I have the following relation in my rails app: genre - has many -
I'm working on a rails app that integrates with other systems (which can't be
I have a partial in my rails app that loads the vote form (It's
Hi Currently i am working on a rails project . where i have to

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.