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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:04:04+00:00 2026-05-17T20:04:04+00:00

I have been working with rails3 and have been trying to create a simple

  • 0

I have been working with rails3 and have been trying to create a simple survey application but some issues have come up. I can create questions just fine but when it comes to creating a survey that people can take I come accross some issues. For one the answers aren’t being assigned a :user_id or :survey_id. The following is a copy of my controllers, models and views.

Survey Controller

def take
  @survey = Survey.find(params[:id])
  @questions = @survey.questions
  @answers = @questions.map{|q| q.answers.build}
  @answers.each do |a|
    a.survey_id = @survey.id
    a.user_id = current_user.id
  end

  respond_to do |format|
    format.html #
  end
end

def submit
  @survey = Survey.find(params[:id])

  respond_to do |format|
    if @survey.update_attributes(params[:survey])
      format.html { redirect_to(@survey, :notice => 'Survey was successfully submitted.')}
      format.xml  { head :ok }
    else
      format.html { render :action => "edit" }
      format.xml  { render :xml => @survey.errors, :status => :unprocessable_entity }
    end
  end
end

Survey Model

class Survey < ActiveRecord::Base
  belongs_to :user
  has_many :questions, :dependent => :destroy
  has_many :answers, :through => :questions, :dependent => :destroy
  validates_presence_of :name, :user_id
  accepts_nested_attributes_for :questions, :allow_destroy => true
end

Questions Model

    class Question < ActiveRecord::Base
     belongs_to :survey
     has_many :answers
     validates_presence_of :question_string, :question_type
     accepts_nested_attributes_for :answers, :allow_destroy => true
    end

Answers Model

    class Answer < ActiveRecord::Base
     belongs_to :question
     belongs_to :user
     belongs_to :survey
    end

Take View

    %h1 Take the survey
    - semantic_form_for @survey, :url => { :action => "submit" } do |s|
     - if @survey.errors.any?
        %p= pluralize(@survey.errors.count, "error") + " prohibited this survey from being saved"
        #survey_errors
         - @survey.errors.full_messages.each do |err_msg|
            %p= err_msg
     #survey_details.header
     = s.fields_for :questions do |q|
        = q.fields_for :answers do |a|
         = render 'answer_fields', :s => a

     .actions
        = s.commit_button

Questions Partial

    #questions_form.fields
     = s.input :question_string, :label => "Question: "
     = s.input :question_type, :as => :radio, :collection => [1,2,3,4], :label => "Question Type:"
     = s.hidden_field :_destroy
     = link_to_function  "Remove Question", "remove_fields(this)"

Answers Partial

    #answers_form.fields
     = s.label :answer_string, "Answer"
     = s.text_field :answer_string
     = s.hidden_field :question_id

Routes.rb

    resources :surveys do
        get 'take', :on => :member
        put 'submit', :on => :member
    end
  • 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-17T20:04:04+00:00Added an answer on May 17, 2026 at 8:04 pm

    try adding another hidden field with the user id in your answers partial.

    s.hidden_field :user_id, :value => x
    

    That is how I have done hidden fields, you are just putting the variables in there. I have encountered merge problems by not passing it as key value pair.


    What are you using? HAML, I don’t really understand how that is working.

    This is all you need for everything to work.

    Survey model

    has_many :answers
    accepts_nested_attributes_for :answers, :allow_destroy => true
    

    The controller doesn’t need additional code

    The view needs fields_for

     <% f.fields for :answers do |f| %>
         <%= f.hidden_field :user_id, :value => current_user.id %>
     <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working with Struts for some time, but for a project I
I have been working on some legacy C++ code that uses variable length structures
I have been working with relational databases for sometime, but it only recently occurred
I have been working on a wrapper for a COM object that can only
I have been working on a web services related project for about the last
I have been working with Visual Studio (WinForm and ASP.NET applications using mostly C#)
I have been working with a string[] array in C# that gets returned from
We have been working with CVS for years, and frequently find it useful to
I have been working as a native C++ programmer for last few years. Now
I have been working on a childish little program: there are a bunch of

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.