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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:00:15+00:00 2026-06-01T09:00:15+00:00

Rails noob here. I have a rails application with (in this example) three tables.

  • 0

Rails noob here. I have a rails application with (in this example) three tables. Users, Machines, and Tests.

Users have many Machines and Tests.

Machines belong to Users.

Tests belong to Machines.

When I create a new test, I want the field test.machine_id to automatically be set to the id of the machine that owns that test. I have been able to create a field with a drop-down menu showing all machines owned by current_user, but I don’t want the user to have to set this field manually.

Tests can only be created by accessing a “create new test” link on a Machine Show page.

*For example, User 1 has Machines 4 and 5. When viewing Machine 5’s Show page, I want to create test 10. I want test(10).machine_id to be set to 5 without a user having to enter this manually.*

In my tests_controller.rb file, I have the following:

 def new
@test = Test.new
@machines = current_user.machiness.all

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @test }
end
end


 def create
@test = Test.new(params[:test])

respond_to do |format|
  if @test.save
    format.html { redirect_to(@test, :notice => 'Test was successfully created.') }
    format.xml  { render :xml => @test, :status => :created, :location => @test }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @test.errors, :status => :unprocessable_entity }
  end
end

end

I imagine I need something like:

def create
@test = current_machine.tests.build(params[:test])
...
end

…but I don’t think current_machine is an actual object.

Tests can only be created by accessing a “create new test” link on a Machine Show page.

Any suggestions?

  • 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-01T09:00:17+00:00Added an answer on June 1, 2026 at 9:00 am

    Consider using a nested resource for your Test model. In your routes file, you would set up your resources like this:

    resources :machines do
      resources :tests
    end
    

    This will make the route for a new Test look like /machines/:machine_id/tests/new. So now your link to the new Test page would look something like

    <%= link_to "New Test", new_machine_tests_path(@machine) %>
    

    and your new action in TestsController would be something like

    def new
      @test = Test.new
      @machine = Machine.find(params[:machine_id])
      ...
    end
    

    Finally, the form for your nested Test resource would be something like

    <%= form_for [@machine, @test] do |f| %>
    ...
    

    That sets up the form to post to a path that will automatically include your machine_id, something /machines/123/tests.

    So in the create action of your TestsController you can do something like

    def create
      @test = Test.new(params[:test])
      @test.machine_id = params[:machine_id]
      ...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Noob here. I have a rails app that has users and postings. I need
Noob question for Ruby on Rails- here's an example of my situation: If I
Rails noob here. I am using carrierwave in a rails 3 app so users
I have three tables: Users Questions User_Questions 1.user questions has the columns user_id, question_id,
Rails noob here looking for some advice/direction on something. I have several Pages and
javascript noob here. So I'm trying to use this s3 uploader jQuery example here
I have two tables: projects and supplies. Projects have many supplies, like this: has_many
I am a Rails noob and have a question. I have a feed aggregator
This is a noob question: How do I add gems to my rails app
Rails 3.1 I'll simplify my application to get at my question. I have two

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.