form_for and saving models

Greetings Ladies and Gents,

I am learning rails and am wondering about a best practice? Currently I am trying to save specific tasks for a project. So tasks has a belongs_to :project and project does has_many :tasks. Well when creating a new task I need to store to project_id in the tasks table.

Currently I can set the @tasks.project_id = @projects.id in the TasksController create method or I can set the @task.project_id = @project.id in the TasksController new method and set a hidden_field in the form_for for project_id for task.

Both ways seem unsexy, but its the best way I found. Is there a better way? This is a nested resource also.

Current code:

<% form_for [:project, @task] do |form| -%>

    <fieldset>         <legend>Project Info</legend>         <% fields_for :project do |p| %>             <%= p.text_field :name, :disabled => :true %>         <% end %>     </fieldset>

    <fieldset>         <legend>Task</legend>         <%= render :partial => 'form', :object => form %>     </fieldset>     <p>         <%= submit_tag 'Create' %>     </p>

<% end -%>

Thanks.

Assuming you use the hidden field, then in create, you can do

@project = Project.find(params[:project] @project.tasks << Task.create(params[:task])

I believe this works, but it's air code. In any event, the spirit of it is to create the task scoped by the project it belongs to rather than manually setting the id.

Make sense?