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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:23:47+00:00 2026-06-03T17:23:47+00:00

The title might be a little weird but let me explain. I have User,

  • 0

The title might be a little weird but let me explain.

I have User, Course, and Subscription models. User has_many :courses, :through => :subscription and Course has_many :users, :through => :subscription. Subscription belongs_to both. That part, I understand completely. I’m using the subscription model to store the state of the relationship (completed, failed, dropped, active, etc) between user and course.

I also have this code which creates the relationship:
@subscription = Subscription.create!(:course_id => @course.id, :user_id => current_user.id)
As of now, I don’t have a Subscriptions controller since they will be managed mainly through the course views (but I’m not set on one way or another, I just want to do it right).

What I don’t understand is how/where to implement the action of a user subscribing to a course. Should it be a separate action in the Course controller with a route that you link to or is there another, better way to do it? I imagine AJAX would be one way of doing it as well, but I have no idea how to implement that.

I’ve looked through Michael Hartl’s rails tutorial, done some google searching over the past couple days, and even browsed the source code for Diaspora (since the action I’m trying to do is similar to friending someone, though the associations are different), but I can’t seem to wrap my head around it.

  • 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-03T17:23:48+00:00Added an answer on June 3, 2026 at 5:23 pm

    Well, I figured out how to do it.

    In my subscriptions controller, I made the create method which look like this:

    def create
        @course = Course.find(params[:course_id])
        Subscription.create!( :user_id => current_user.id, :course_id => params[:course_id])
        redirect_to @course 
        flash[:notice] = "You're now subscribed to #{@course.name}! WooHoo!"
    end
    

    After that, I can use the button_to helper in my views to create a simple form with one button that points to the create action on my subscriptions controller:

    <%= button_to "Subscribe!", subscriptions_path(:course_id => @course.id) %>

    It’s also necessary, of course, to add the appropriate actions to routes.rb:

    resources :subscriptions, :only => [:create, :update]

    It can definitely be cleaned up, but for the time being, it works nicely.

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

Sidebar

Related Questions

the title might be a little bit confusing, let me explain, ;) I have
Okay so my title might be a little confusing, but let me explain my
The title might seem unsettling, but let me explain. I'm facing an interesting challenge,
Title might be a bit confusing, so let me explain. I have a website
The title might not explain my problem fully but I wasn't sure how to
Please excuse (or improve the title) but I have a silly little problem that's
Hi i know the Title might sound a little confusing but im reading in
I suppose the title might be a little misleading, but I couldn't think of
Ok this might sound a little vague from the title, but that's because I
I know that the title might sound a little contradictory, but what I'm asking

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.