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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:52:00+00:00 2026-05-31T21:52:00+00:00

Objective Initialize a new addition problem for users to solve when they visit /task

  • 0

Objective

Initialize a new addition problem for users to solve when they visit /task. They input their answer, and the form submits back to /task. The new action should initialize the problem, since users cannot edit the problem, only provide an answer. The create action should update the table addition_tasks in the column answer with the user’s submitted answer.

Problem

As I currently have it, the task is created and saved successfully in the new action, but fails to update with the create action.

Model

# == Schema Information
#
# Table name: addition_tasks
#
#  id            :integer         not null, primary key
#  first_addend  :integer
#  second_addend :integer
#  sum           :integer
#  answer        :integer
#  correct       :boolean
#  created_at    :datetime        not null
#  updated_at    :datetime        not null
#

class AdditionTask < ActiveRecord::Base

  attr_accessible :answer

  belongs_to :user
end

Routes

  match 'task', to: 'addition_task#new'
  match 'task', to: 'addition_task#create'

Controller

class AdditionTaskController < ApplicationController

  respond_to :html, :js

  def new
    @task = AdditionTask.new
    @task = new_task(@task)
    @task.save
    session[:task_id] = @task.id
  end

  def create
    @task = AdditionTask.find(session[:task_id])
    @task.answer = params[:addition_task][:answer]
    respond_with :new
  end

  private

  def new_task(task)
    @task.first_addend = 1 + Random.rand(98)
    @task.second_addend = 1 + Random.rand(98)
    @task.sum = @task.first_addend + @task.second_addend
    @task
  end
end

View

%h1 Problem
:markdown
  Please answer the question below. You may press "Enter" from within the answer field, instead of clicking the button, in order to receive a new question.

#task-container
  .addend
    =@task.first_addend
  .operator
    +
  .addend
    =@task.second_addend
  .operator
    \=
  .answer
    =simple_form_for @task, url: task_path, defaults: {input_html: {class: 'span2'}}, html: { class: 'form-inline' } do |f|
      =f.input :answer, label: false
      =f.submit :submit, class: 'btn-primary btn-large pull-right', value: 'Answer'

%div
  =@task.sum
  • 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-31T21:52:01+00:00Added an answer on May 31, 2026 at 9:52 pm

    Siwei’s answer and lee’s answer both made excellent points.

    Thanks to some folks in #rubyonrails, I moved the code from create to update. I also changed my routes, as Siwei suggested, and saved the object, as lee suggested. However, I used @obj.save! in order to get some helpful errors if something went wrong.

    New Routes

      resources :users
      resources :addition_tasks, path: 'task', only: [:new, :update]
    
      root to: 'site#home'
      match 'signup',  to: 'users#new'
      match 'signin',  to: 'sessions#new'
    

    New Controller

    class AdditionTasksController < ApplicationController
    
      respond_to :html, :js
    
      def new
        @task = AdditionTask.new
        @task = new_task(@task)
        @task.save!
        session[:task_id] = @task.id
      end
    
      def update
        @task = AdditionTask.find(session[:task_id])
        @task.answer = params[:addition_task][:answer]
        @task.save!
        render action: :new
      end
    
      private
    
      def new_task(task)
        @task.first_addend = 1 + Random.rand(98)
        @task.second_addend = 1 + Random.rand(98)
        @task.sum = @task.first_addend + @task.second_addend
        @task
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to objective-c and having a problem to initialize an object
I am relatively new to Objective-C and now I have a problem in my
I'm a beginner at Objective-C and am trying to initialize an NSMutableArray with objects
Objective: My script will download a remote file upon form submission, since the file
Objective C is new for me. How can I change the font of a
i am new to objective c programming I want to call a function within
Very new to Objective-C and Xcode and working my way through modifying the DrillDownApp
I'm new to Objective-C and iPhone development and have been using Apress' Beginning iPhone
I'm new to Objective C. I am trying to define Constants which have behavior
Completely new to Objective-C, trying to find out when I need to alloc and

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.