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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:45:13+00:00 2026-05-20T19:45:13+00:00

I’m trying to write an app in rails 3 and I’m having some trouble

  • 0

I’m trying to write an app in rails 3 and I’m having some trouble figuring out the routes and controllers for a test that I want the user to take. The basic requirements for this app are:

  1. Users, Tests and Questions are all in separate models.
  2. A User has_many Tests. A Test has_many Questions
  3. Provide a link on the user_profile page to /test/new to create the test record.
  4. Provide a link on /test/new to /test/:id/part1 (where :id is the test_id) so that the user can complete the first part of the test. Questions will be retrieved from the db and presented on this page.
  5. Provide a link on /test/:id/part1 to /test/:id/part2 so that the user can complete the second part of the test. Again, questions are retrieved from the db.
  6. Provide a link on /test/:id/part2 to submit the test and return to the user’s profile.

I’ve completed the models, which even pass their tests, so I think I have finished parts 1 and 2.

user.rb

Class User < ActiveRecord::Base
  has_many :tests
end

test.rb

Class Test < ActiveRecord::Base
  belongs_to :user
  has_many :questions
end

question.rb

Class Question < ActiveRecrod::Base
  belongs_to :test
end

My issues start when I try to put these models together using routes and controllers.

routes.rb

resources :users

resources :tests do
  member do
    post 'part1'
    post 'part2'
  end
end

users/show.html.erb

<%= link_to "Start The Test", new_test_path %>

tests/new.html.erb

<%= link_to "Part 1", part1_test_path(@test) %>

tests_controler.rb

class TestsController < ApplicationController
  def new
    @test = Test.new(current_user)
  end

  def part1
    # still just a stub
  end
end

I’m getting this error when I click on the link to take Part 1 of the test:

No route matches {:action=>"part1", :controller=>"tests", :id=>#<Test id: nil, taken_at: nil, user_id: nil, created_at: nil, updated_at: nil>}

Any help on this would be greatly appreciated.

  • 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-20T19:45:13+00:00Added an answer on May 20, 2026 at 7:45 pm

    By defining a member of the routes it’s expecting an existent test, ie. one which is saved and has an id.

    e.g.

    part1_test_path = /test/123/part1
    

    What you need is a collection route.

    resources :tests do
      collection do
        post 'part1'
      end
      member do
        post 'part2'
      end
    end
    

    e.g.

    part1_test_path = /test/part1
    

    edit

    Suggested solution:

    resources :test, :path_names => { :new => 'part_1', :edit => 'part_2' } *1
    
    def new 
      @test = Test.new
    
    #new view
    form_for @test do
      ...
    
    def create
      @test = Test.new params[:test]
      if @test.save
        redirect_to edit_test_path @test
    
    def edit
      @test = Test.find params[:id]
    
    #edit view
    form_for @test do
    
    def update
      @test = Test.find params[:id]
      if @test.update_attributes params[:test]
        redirect_to test_path @test 
    
    
    def show # test results
      @test = Test.find params[:id]
      if @test.incomplete *2
        redirect_to edit_test_path @test
    

    *1 See rails guide on routing. This will give you urls like this

    test/part1
    test/123/part2

    You should put all of your validation in the model; your requirements of test data. Conditional validation will be required, depending on whether it’s a new_record? or not ie if you’re at part 1 or 2.

    *2
    add a method to your model which checks test completeness.

    def incomplete
      self.some_test_field.blank?
    

    Let me know if you don’t understand anything.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.