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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:08:06+00:00 2026-06-17T06:08:06+00:00

I am trying to get a better handle on Rail’s nested resources, and made

  • 0

I am trying to get a better handle on Rail’s nested resources, and made a test app with models School and Class. In my routes.rb file, I have:

resources :schools do
   resources :classes
end

With the following models relationship for School and Class:

class School < ActiveRecord::Base
  attr_accessible: name
  has_many :classes
end

and

class Class < ActiveRecord::Base
  attr_accessible: name, school_id
  belongs_to :school
end 

I am having difficulty getting the school_id associated with the posts created under an URL like /schools/1/posts/new. More precisely, I would like to define a helper method like current_school that could take the first half of the URI where it contains the school_id, to allow me to write functions in the controller like current_school.posts.all that will automatically pull all the posts associated with school_id = what is in the URL. Thanks!

*Edit

Here is what I have in ClassController:

class ClassesController < ApplicationController

  def index
    @classes = current_school.classes.all
  end

  def new
    @class = current_school.classes.build
  end

  def create
    @class = current_school.classes.build(params[:post])
    if @class.save
      redirect_to root_path #this will be further modified once I figure out what to do
    else
      redirect_to 'new'
    end
  end

  private

  def current_school
    @current_school ||= School.find(params[:school_id])
  end

end 

And in the new.html.erb file:

  <div class="span6 offset3">
    <%= form_for([@school, @class]) do |f| %>

      <%= f.label :name, "class title" %>
      <%= f.text_field :name %>

      <%= f.submit "Create class", class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
  • 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-17T06:08:07+00:00Added an answer on June 17, 2026 at 6:08 am

    When you nest your resources, you get several helper methods for free as explained here. The method you’re looking for would be written as one of:

    new_school_class_path(@school)
    new_school_class_path(@school_id)
    

    While your classes index page would be:

    school_classes_path(@school)
    school_classes_path(@school_id)
    

    In your ClassesController, you would do something like:

    def index
      @classes = current_school.classes
    end
    
    def new
      @class = current_school.classes.build
    end
    
    private
    def current_school
      @current_school ||= School.find(params[:school_id])
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a better handle on what it really means for a
Trying to get a better handle on how django database relationships are handled. Any
I'm trying to get a better handle on using sockets asynchronously. According to this
I am trying to get better about minimizing any unnecessary markup and using optimal
I'm a C programmer and trying to get better at C++. I want to
I am trying to get a better working knowledge of JavaScript. So, I have
I'm trying to get a better understanding than I've been able to so far...it's
Just trying to get diff to work better for certain kinds of documents. With
I'm trying to determine if there's a better way to handle replication than the
I'm trying to get better at handling exceptions but I feel like my code

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.