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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:11:29+00:00 2026-05-28T15:11:29+00:00

Here’s my problem. If I go to Projects#edit , I am unable to change

  • 0

Here’s my problem. If I go to Projects#edit, I am unable to change the course it is assigned to. If I attempt to create a new course for it, or choose from an existing one, I get the following error:

Couldn't find Course with ID=23 for Project with ID=62
app/controllers/projects_controller.rb:49:in `update'

{...
"project"=>{"title"=>"Sup",
"due_date"=>"2012-01-23",
"course_id"=>"27",                        # THIS IS THE ID OF THE NEW COURSE I WANT
"course_attributes"=>{"name"=>"Calc I",   # THIS IS THE OLD ONE, I don't need this. Why is it passing this?
"number"=>"MATH102",
"user_id"=>"3",
"id"=>"23"},
"description"=>"bla bla bla"},
"commit"=>"Update Project",
"user_id"=>"3",
"id"=>"62"}

So I can see that it’s trying to pass in the course_attributes, but it’s not actually setting the new course_id. I don’t understand why course_attributes is being passed, the other form is blank, and the course_attributes being passed, are the attributes of the old course. I want to set the course_id to be the course_id being passed (27 in this case).

ProjectsController

def new
  @project  = @user.projects.new
  @courses = @user.courses    # Used to populate the collection_select
  @project.build_course       # I was informed I need to use this to get the form_for to work
end

def edit
  @project = Project.find(params[:id])
  @courses = @user.courses    # Used to populate the collection_select
end

def update
  @project = Project.find(params[:id])
  @courses = @user.courses

  if @project.update_attributes(params[:project])
    flash[:notice] = 'Project was successfully updated.'
    redirect_to user_projects_path(@user)
  else
    render :edit
  end
end

Line 49 is the call to update_attributes.

Other Information

project.rb

belongs_to :user
belongs_to :course

attr_accessible :course_id, :course_attributes
accepts_nested_attributes_for :course

course.rb

belongs_to :user
has_many :projects

user.rb

has_many :projects
has_many :courses

So a project has a course_id in the database. I’m currently creating or choosing an existing course on the fly on the Projects#new page. Here’s my form for that. I use a JavaScript toggle to alternate between the collection_select and the two text_fields.

projects/new.html.haml

= form_for [@user, @project] do |f|
  # This is shown by default
  = f.collection_select :course_id, @courses, :id, :name, { prompt: true }

  .hidden # This is hidden by default and shown using a toggle
    = f.fields_for :course do |builder|
      = builder.text_field :name, class: 'large', placeholder: 'Ex: Calculus I'
      = builder.label :number, 'Number'
      = builder.text_field :number, class: 'new_project_course_number'
      = builder.hidden_field :user_id, value: current_user.id

Now, if I am on the new project page, and I attach it to a course by choosing an existing course, it will work correctly. The Project will be created, and the course_id will be set correctly.

If I am on the new project page, and I create a course by using my JavaScript toggle, then filling out the Course Name and Course Number, and click Create, then it will also work. The Course will be created, and the Project will be created with the correct course_id.

Sorry for the lengthy post, but I wanted to provide all information I could. Thanks!

UPDATE 1

routes.rb

resources :users do
  resources :projects do
    collection do
      get 'completed'
      match 'course/:course_number' => 'projects#course', as: 'course'
    end
  end

  resources :courses
end
  • 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-28T15:11:30+00:00Added an answer on May 28, 2026 at 3:11 pm

    Assuming that your Projects#edit form is similiar to your Projects#new

    This is creating the course_attributes in params

      .hidden # This is hidden by default and shown using a toggle
        = f.fields_for :course do |builder|
          = builder.text_field :name, class: 'large', placeholder: 'Ex: Calculus I'
          = builder.label :number, 'Number'
          = builder.text_field :number, class: 'new_project_course_number'
          = builder.hidden_field :user_id, value: current_user.id
    

    That’s because if a user has a current course it will create fields for every course.

    If you want to be able to build a new course on the fly in edit and new change this line to:

        = f.fields_for :course, @project.build_course(:user => @user) do |builder|
    

    This will build a new course regardless of whether you’re in edit or new. (Also you can remove @project.build_course in your controller this way.)

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

Sidebar

Related Questions

Here is my problem : I have a post controller with the action create.
Here's a problem I ran into recently. I have attributes strings of the form
Here's a coding problem for those that like this kind of thing. Let's see
Here's an interesting problem. On a recently installed Server 2008 64bit I opened IE
Here's my procedure: DROP PROCEDURE IF EXISTS `couponExpires`$$ CREATE DEFINER=`root`@`localhost` PROCEDURE `couponExpires`(IN couponID BIGINT,
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here my problem: @Assert\Regex( * pattern=/^[A-Za-z0-9][A-Za-z0-9\]*$/, * groups={creation, creation_logged} * ) I'm using the
Here is my query: select word_id, count(sentence_id) from sentence_word group by word_id having count(sentence_id)
Here is my query: SELECT * FROM [GeoName] WHERE ((-26.3665122100029-Lat)*(-26.3665122100029-Lat))+((27.5978928658078-Long)*(27.5978928658078-Long)) < 0.005 ORDER BY
Here's a query that works fine: SELECT rowid as msg_rowid, a, b, c FROM

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.