Topic: Newbie Question: Using relationships to automatically assign parent.

Hi all,

Just started Ruby and Rails a few weeks back, and I ran into a problem which seems like it's a newbie issue. Here's what I'm trying to do:

I'm writing a simple website for a teacher who wants to be able to display her courses and assignments for each course. I have the code to create the courses working, but I'm not sure how to add an assignment to a course and have it automatically child itself to that course without manually putting it into a field.

So, I have this model:

class Course < ActiveRecord::Base
  has_many :assignments
  ...
end

and

class Assignment < ActiveRecord::Base
  belongs_to :course
end

Then I have a link_to object in the view that lists the courses:
<% for course in @courses %>
    <%= link_to course.name , :action => 'edit', :id => course.id %>
    <%= link_to "show description", :action => 'show' %>
    <%= link_to "delete", {:action => 'delete', :id => course},
                             :confirm => "Are you sure?",
                            :post => true %>
    <%= link_to "Add an Assignment", :action => 'add_assignment', :id => course.id %>

This is kinda where I get stuck. From here it goes to a form where you can add a title and description for a course, but I don't know how to send it back to the controller to make it a child of the course that you want to add it to without typing it into another field (the assignments table has a course_id field).

Any help you might have would be wonderful. Like I said -- newbie question.

Thanks!

Re: Newbie Question: Using relationships to automatically assign parent.

What I usually do is set the course_id when creating the assignment at the beginning, then pass this along as a hidden field in the form. Like this:

# in controller
def add_assignment
  @assignment = Assignment.new(:course_id => params[:id])
end

# in view
<%= hidden_field :assignment, :course_id %>


This will then be passed along automatically to the next action, so when you create a new assignment with params[:assignment] it will have the correct course_id. Does that answer your question?

Railscasts - Free Ruby on Rails Screencasts

Re: Newbie Question: Using relationships to automatically assign parent.

Awesome. It looks like it worked. Thanks, that one was stumping me. I kept trying to find different ways to pass the value of the course :id to the assignment's :course_id.

Much appreciated.

Re: Newbie Question: Using relationships to automatically assign parent.

By the way, you may want to move the assignment-specific methods (like add_assigment) to a separate "Assigments" controller. This way it can have its own new, create, edit, update and destroy actions so you don't have to clutter up the Courses controller with these actions.

Railscasts - Free Ruby on Rails Screencasts

Re: Newbie Question: Using relationships to automatically assign parent.

Thanks for the suggestion. I was just thinking something similar. I'll give it a try, and I'll no doubt run into some problems, so I'll be back!

Re: Newbie Question: Using relationships to automatically assign parent.

I have a very similar situation, but am having trouble implementing this suggestion.

I have a list of categories and then the child stories that fall under them. I want to add a link so you can add a story into a specific category (this link would move a user to the story 'show' view, but would default to the corresponding category. Here's the way I have it now:

<% for category in @categories %>

<b><%= h(category.section) %></b>
     <span class="list-actions">
        <%= link_to 'Edit', :action => 'edit', :id => category %> |
        <%= link_to 'Delete', { :action => 'destroy', :id => category },
        :confirm => "Are you sure?",
        :post => true %> |
        <span class="highlight"><%= link_to 'New Story in This Section', :controller => "story_admin", :action => "new" %></span><br />

        <% for story in category.stories %>
         <span class="listing"><b><%= link_to story.title, :controller => "story_admin", :action => "show", :id => story.id %><br /></span>
        <% end %>
        <br />
</td>
  </tr>
<% end %>


Now the thing I want to draw your attention to is the  link_to 'New Story in This Section' link, which moves a user to the right view and everything, but I'm not specifying to pass the category id to the view so it defaults to a blank category, forcing the user to choose one, which is bad UI.

Ideas how to change that? I'm pretty sure it's mostly outlined in the responses above, but I can't quite get them to work.

Thanks!

Re: Newbie Question: Using relationships to automatically assign parent.

Why not pass the category_id through the link_to?

<%= link_to 'New Story in This Section', :controller => "story_admin", :action => "new", :category_id => category %>

Then you can fetch the id with params[:category_id] in the controller.

Railscasts - Free Ruby on Rails Screencasts

Re: Newbie Question: Using relationships to automatically assign parent.

OK bear with me here. I can see that the category_id is posting in the URL now, so I feel like I'm one step away. but when I put the params where I think it should go, it doesn't update category in the collection_select drop-down:

def new
    @story = Story.new(:category_id => params[:id])

Re: Newbie Question: Using relationships to automatically assign parent.

Try this:

@story = Story.new(:category_id => params[:category_id])

Railscasts - Free Ruby on Rails Screencasts

Re: Newbie Question: Using relationships to automatically assign parent.

blam. that worked like a charm