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

  • SEARCH
  • Home
  • 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 8604853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:40:47+00:00 2026-06-12T02:40:47+00:00

I have a collection with predefined questions and a user whom should answer all

  • 0

I have a collection with predefined questions and a user whom should answer all these questions. So the view should be something like:

= form_for @user do |f|
  -#some code and then...
    %table.table
      - @questions.each do |q|
        %tr
          %td=q.name
          %td
            %input-# Here the answer of the user for the question 'q'

So the view should show too each of the user answers for each question. My problem is that in first instance there shouldn’t be any answer, so I couldn’t use something like:

= form_for @user do |f|
  -#some code and then...
    %table.table
      - @user.answers.each do |answer|
        = f.fields_for :answers, answer do |a|
          %tr
            %td= answer.question.name
            %td=a.number_field :result

In this case there won’t be any answers so nothing will show. Even if there would be an answer I must to show all the questions to the user. I could just pre-initialize the answers with empty values, but I don’t want to have a bunch of records just with empty values(most of these questions maybe never will be answered by the user). Another thing is that the questions has an order and I don’t know how is gonna be sorted with the @user.answers.each loop (yeah, I could modify the query, so it could be sorted, but I would be tweaking too much for something…simple?). In general the main loop should be @questions.each and not @user.answers.each.

I’ve been thinking in some nasty ways to do that, like creating the fields manually, several if/else conditions, but I’m hoping that rails has a clean way to do that. Somebody has crossed with a problem like this before? There’s no other way to do that but create all of these with custom helpers?. Thanks in advance

Update

At the end, thanks to @saverio answer, I left it as follows:

%table.table
  - @questions.each_with_index do |q, i|
    %tr
      %td= q.name
      %td
        - answer = @user.answers.detect{|a| a.question.try(:id) == q.id}
        = number_field_tag "user[answers_attributes][#{i}][result]", (answer && answer.result)
        = hidden_field_tag "user[answers_attributes][#{i}][id]", (answer && answer.id)
        = hidden_field_tag "user[answers_attributes][#{i}][question_id]", q.id

In the controller, with the next line, was enough to clean all the empty values:

params[:user][:answers_attributes].delete_if{|k,v| v[:result].blank? && v[:id].blank?}
  • 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-06-12T02:40:48+00:00Added an answer on June 12, 2026 at 2:40 am

    Loop over @questions, and at each step show a field for the corresponding answer. If an answer has been provided, show it as preloaded text

    = form_for @user do |f|
      -#some code and then...
        %table.table
          - @questions.each do |q|
            - answer = @user.answers.detect {|a| a.question.name == q}
            %tr
              %td= q.name
              %td= text_field_tag "user[answers][#{q.id}]", (answer && answer.text)
    

    In the controller you’ll have to parse the params[:user][:answers], which will be a hash from the question’s ID to the provided answer.

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

Sidebar

Related Questions

for example I have collection foo of documents like that: {tag_cloud:[{value:games, count:10}, {value:girls, count:500}]}
I have collection of div tags in the form tag, like this: <form ...>
I have collection of documents Users User { Status: ACTIVE, Login: { UserName: login,
I have Collection List<Car> . How to compare each item from this collection with
Lets say I have collection of documents with specified longitude and latitude. type is
I'm planning to have collection of items stored in a TCollection. Each item will
I have Class and Student objects. Both have collection of another as property. Which
We have a collection of entities of type called Unit , collection of entities
I have a collection that use as a cache. How can I limit the
I have a collection of items populated to a drop down menu: string myUserName

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.